简体   繁体   English

自定义 appender 的 log4j2 属性文件

[英]log4j2 properties file for a custom appender

I created a custom appender and it's not getting called when I run my test.我创建了一个自定义 appender,当我运行测试时它没有被调用。 Here's what the properties look like:下面是属性的样子:

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

My appender is called a WindowsEventLogAppender.我的 appender 被称为 WindowsEventLogAppender。 Any idea what's wrong with my properties file?知道我的属性文件有什么问题吗? I see the console test messages but none of the messages from my appender.我看到了控制台测试消息,但没有看到来自我的 appender 的消息。 Right now I'm just doing a System.out.println in my custom appender to verify it's getting called.现在我只是在我的自定义 appender 中执行 System.out.println 来验证它是否被调用。

BTW, I've found lot's of XML examples out there for log4j2 configurations with custom appenders but none for using a properties file for configuration.顺便说一句,我发现了很多 XML 示例,用于带有自定义附加程序的 log4j2 配置,但没有用于使用属性文件进行配置的示例。

Thanks, -Mike谢谢,-迈克

I might be quite late here, but I think my answer can help other people looking for answers.我可能来晚了,但我认为我的回答可以帮助其他人寻找答案。 Please accept this as an answer if this is correct!如果这是正确的,请接受它作为答案!

If you have created a custom appender having annotation like this:如果您创建了一个具有如下注释的自定义 appender:

@Plugin(name = "MyCustomAppender", category = "Core", 
elementType = "appender", printObject = true)
public final class MyCustomAppenderImpl extends AbstractAppender {
  // other code for the plugin....
}

The log4j2 manual about Configuring Appenders states that:有关配置 Appenders的 log4j2 手册指出:

"An appender is configured either using the specific appender plugin's name or with an appender element and the type attribute containing the appender plugin's name" “使用特定的 appender 插件名称或使用 appender 元素和包含 appender 插件名称的 type 属性配置 appender”

Meaning the type for appender should be Appender Plugin's Name attribute value.这意味着 appender 的type应该是 Appender Plugin 的Name属性值。

Which in above case is MyCustomAppender ( appender.identifierName.type=MyCustomAppender )在上面的例子中是MyCustomAppender ( appender.identifierName.type=MyCustomAppender )

So, the Properties file configuration for this to work should be:因此,为此工作的属性文件配置应该是:

(Note : I have added a stdout(console) appender just to show relevance/similarity of usage with OOTB appenders, and 2 example usages with RootLogger and a custom logger) (注意:我添加了一个 stdout(console) appender 只是为了显示与 OOTB appender 的使用相关性/相似性,以及 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)

Try adding the packages property.尝试添加包属性。

Like: packages = com.myCompany喜欢:packages = com.myCompany

You haven't included the package info您尚未包含包裹信息

try the below configuration.试试下面的配置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM