我创建了一个自定义附加程序,运行测试时未调用它。属性如下所示:
name=config
appenders=console, myCustomAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
#appender.console.layout.pattern =%d{HH:mm:ss} [%t] %c{1} [%-5level] - %msg%n
appender.console.layout.pattern=%d{dd-MM-yyyy HH:mm:ss} [%-5p] (%F:%L) - %m%n
appender.myCustomAppender = com.myCompany.logging.log4j.WindowsEventLogAppender
appender.myCustomAppender.name = WindowsEventLogAppender
appender.myCustomAppender.type = WindowsEventLogAppender
rootLogger.level=info
rootLogger.appenderRefs=stdout, myCustomAppender
rootLogger.appenderRef.stdout.ref=STDOUT
我的附加程序称为WindowsEventLogAppender。知道我的属性文件出了什么问题吗?我看到控制台测试消息,但没有来自附加程序的消息。现在,我只是在自定义附加程序中执行System.out.println来验证它是否被调用。
顺便说一句,我发现了很多带有自定义附加程序的log4j2配置的XML示例,但没有一个使用属性文件进行配置的XML示例。
谢谢, -迈克
答案 0 :(得分:1)
我可能来晚了,但我认为我的回答可以帮助其他人寻找答案。如果这是正确的,请接受它作为答案!
如果您创建了一个具有如下注释的自定义 appender:
@Plugin(name = "MyCustomAppender", category = "Core",
elementType = "appender", printObject = true)
public final class MyCustomAppenderImpl extends AbstractAppender {
// other code for the plugin....
}
关于 Configuring Appenders 的 log4j2 手册指出:
<块引用>“使用特定的 appender 插件名称或使用 appender 元素和包含 appender 插件名称的 type 属性配置 appender”
表示 appender 的 type
应该是 Appender Plugin 的 Name
属性值。
在上述情况下是 MyCustomAppender (appender.identifierName.type=MyCustomAppender
)
因此,要使其工作的属性文件配置应为:
<块引用>(注意:我添加了一个 stdout(console) appender 只是为了显示 与 OOTB appender 使用的相关性/相似性,以及 2 个示例 RootLogger 和自定义记录器的用法)
# this packages attribute is important, please put comma seperated package(s) to the
# plugin(s) you have created
packages = com.package.to.your.plugin
# Example: Declare and Define OOTB Console appender, which sends log events to stdout
appender.console.name = stdout
appender.console.type = Console
# Declare and define the custom appender like this
# Note that the "abc" in "appender.abc.type" can be anything
# and the value for "appender.abc.type" should be the same as
# "Name" attribute value given in custom appender plugin which is "MyCustomAppender"
appender.abc.name=arbitrary_name
appender.abc.type=MyCustomAppender
rootLogger.appenderRef.stdout.ref = stdout
rootLogger.appenderRef.abc.ref = arbitrary_name
logger.loggeridentifier.name = com.test.SomeClass
logger.loggeridentifier.appenderRef.stdout.ref = stdout
logger.loggeridentifier.appenderRef.abc.ref = arbitrary_name
# Also note that the value of appenderRef should be the same name given to your
# appender in properties file, which in this case is "arbitrary_name" (as given above)
答案 1 :(得分:0)
尝试添加packages属性。
像:包裹= com.myCompany
答案 2 :(得分:0)
您尚未提供包裹信息
尝试以下配置。
name=config
appenders=console, myCustomAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
#appender.console.layout.pattern =%d{HH:mm:ss} [%t] %c{1} [%-5level] - %msg%n
appender.console.layout.pattern=%d{dd-MM-yyyy HH:mm:ss} [%-5p] (%F:%L) - %m%n
appender.myCustomAppender = com.myCompany.logging.log4j.WindowsEventLogAppender
appender.myCustomAppender.name = WindowsEventLogAppender
appender.myCustomAppender.type = WindowsEventLogAppender
rootLogger.level=info
rootLogger.appenderRefs=stdout, myCustomAppender
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.com.mycompany.example=INFO,STDOUT