3 回答
TA贡献1790条经验 获得超9个赞
我在这里可能会很晚,但我认为我的回答可以帮助其他人寻找答案。如果这是正确的,请接受这个作为答案!
如果您创建了一个具有如下注释的自定义附加程序:
@Plugin(name = "MyCustomAppender", category = "Core",
elementType = "appender", printObject = true)
public final class MyCustomAppenderImpl extends AbstractAppender {
// other code for the plugin....
}
关于配置 Appenders的 log4j2 手册指出:
“使用特定的附加程序插件名称或附加程序元素和包含附加程序插件名称的类型属性配置附加程序”
这意味着typefor appender 应该是 Appender Plugin 的Name属性值。
在上述情况下是MyCustomAppender ( appender.identifierName.type=MyCustomAppender)
因此,为此工作的属性文件配置应该是:
(注意:我添加了一个标准输出(控制台)附加程序,只是为了显示与 OOTB 附加程序的使用相关性/相似性,以及 RootLogger 和自定义记录器的 2 个示例用法)
# 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)
TA贡献1893条经验 获得超10个赞
您没有包含包裹信息
试试下面的配置。
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
添加回答
举报