本文介绍了Log4j2-如何将XML配置转换为JDBC附加程序的.properties格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将log4j2.xml配置片段转换为log4j2.properties格式?

How do I convert this log4j2.xml config fragment to log4j2.properties format?

我无法在maven + netbeans项目中使用XML格式,因为我根本无法获取log4j2来解析和响应log4j2.xml文件-不管我将其放在项目中的什么地方,log4j2都会将其忽略.

I cannot use XML format in my maven + netbeans project as I simply cannot get log4j2 to parse and respond to a log4j2.xml file - no matter where I place it in my project, it is ignored by log4j2.

然而,对主/资源中的log4j2.properties进行了解析并做出了响应,所以我-HAVE-使用.properties ...:

However log4j2.properties in main/resource IS parsed and responded to so I -HAVE- to use .properties...:

 <JDBC name="databaseAppender" tableName="LOGGING.APPLICATION_LOG">
   <ConnectionFactory class="log4j_tutorial.ConnectionFactory"
    method="getDatabaseConnection" />
   <Column name="EVENT_DATE" isEventTimestamp="true" />
   <Column name="LEVEL" pattern="%level" />
   <Column name="LOGGER" pattern="%logger" />
   <Column name="MESSAGE" pattern="%message" />
   <Column name="THROWABLE" pattern="%ex{full}" />
  </JDBC>

我正在通过官方的Apache Maven log4j构件使用log4j2 2.10.0.

I'm using log4j2 2.10.0 via the official Apache Maven log4j artifacts.

-correct- log4j2.properties配置与上面的配置100%等效是什么?

What would the -correct- log4j2.properties config be to be 100% equivalent to the above?

我差不多要花两天的时间来使JDBC附加程序正常工作,所以是时候寻求帮助了,方法.

I'm close to spending two days straight just on getting a JDBC appender working, so it is time to ask for help, methinks.

我当前的log4j2.properties文件可以很好地输出到文件和控制台:

My current log4j2.properties file which works fine an outputs to file and console:

appender.file.type = File
appender.file.name = fileAppender
appender.file.fileName = verdi.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{YYYY-mm-dd HH:mm:ss.SSS} %-5p %c{1} - %m %n

appender.out.type = Console
appender.out.name = out
appender.out.layout.type = PatternLayout
appender.out.layout.pattern = %d{YYYY-mm-dd HH:mm:ss.SSS} %-5p %c{1} - %m %n

rootLogger.level = INFO
rootLogger.appenderRef.file.ref = fileAppender
rootLogger.appenderRef.out.ref = out

我实际上只是想在log4j2.properties文件中添加一个JDBC附加器-数据库表已经创建,并且我已经按照文档实现了ConnectionSource接口,并且它由一个连接池的DataSource支持,例如文档.但是,正式的Apache文档非常模糊,并且很少提供确切的细节,尤其是在配置方面.

I literally just want to add a JDBC appender in the log4j2.properties file - the db table is already created, and I've implemented the ConnectionSource interface as per the docs, and it is backed by a connectionpooled DataSource, as per the docs. The official Apache docs however are very vague and extremely sparse with exact specifics especially as regards configuration.

我找不到关于指定时JDBC附加程序中应该包含哪些元素的正式列表,以及如何在log4j2.properties文件中指定上述明显的XML层次结构.我可以找到的所有有关JDBC附加程序配置的示例仅是-ALWAYS- log4j2.xml/基于XML的示例.

I can find no official list of what elements should be in a JDBC appender when specified, and how the XML hierarchy apparent in the above must be specified in a log4j2.properties file. All examples I can find of JDBC appender configuration are -ALWAYS- log4j2.xml / XML based examples only.

还是我应该放弃,而是花几天/周的时间尝试使log4j XML配置正常工作?

Or should I give up and rather spend a few days / weeks trying to get log4j XML configuration working?

谢谢!

推荐答案

我试图做同样的事情,并且真的很惊讶在任何地方都找不到用于JDBCAppender属性配置的示例.

I was trying to do the same thing, and am really surprised to find no examples anywhere for JDBCAppender properties configuration.

我确实发现了什么,因为我在OSGI下使用PAX日志记录,所以我可以通过设置org.ops4j.logging.log4j2.config.file来引用XML配置文件.完成该工作后,我现在回过头来找出属性方法,所以我认为这是您想要的:

What I did discover, since I'm using PAX logging under OSGI, I can refer to an XML config file by setting org.ops4j.logging.log4j2.config.file. Having got that to work, I've now gone back and figured out the properties approach, so I think this is what you are looking for:

appender.file.type = File
appender.file.name = fileAppender
appender.file.fileName = verdi.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{YYYY-mm-dd HH:mm:ss.SSS} %-5p %c{1} - %m %n

appender.out.type = Console
appender.out.name = out
appender.out.layout.type = PatternLayout
appender.out.layout.pattern = %d{YYYY-mm-dd HH:mm:ss.SSS} %-5p %c{1} - %m %n

appender.db.type = Jdbc
appender.db.name = dbAppender
appender.db.tableName = LOGGING.APPLICATION_LOG
appender.db.cf.type = ConnectionFactory
appender.db.cf.class = log4j_tutorial.ConnectionFactory
appender.db.cf.method = getDatabaseConnection
appender.db.col1.type = Column
appender.db.col1.name = EVENT_DATE
appender.db.col1.isEventTimestamp = true
appender.db.col2.type = Column
appender.db.col2.name = LEVEL
appender.db.col2.pattern = %level
appender.db.col3.type = Column
appender.db.col3.name = LOGGER
appender.db.col3.pattern = %logger
appender.db.col4.type = Column
appender.db.col4.name = MESSAGE
appender.db.col4.pattern = %message
appender.db.col5.type = Column
appender.db.col5.name = THROWABLE
appender.db.col5.pattern = %ex{full}

rootLogger.level = INFO
rootLogger.appenderRef.file.ref = fileAppender
rootLogger.appenderRef.out.ref = out
rootLogger.appenderRef.db.ref = dbAppender

由于我使用的是DataSource而不是ConnectionFactory,因此我没有尝试过这种确切的配置,但是我认为这在理论上应该可行.希望对您有帮助.

I haven't tried this exact config, since I'm using a DataSource rather than a ConnectionFactory, but I think that this should work in theory. I hope it helps.

如果需要数据源而不是连接工厂,则按要求添加数据源定义行:

Add datasource definition lines as requested if datasource is required rather than connection factory:

appender.db.datasource.type = DataSource
appender.db.jndiName = osgi:service/dsName

这篇关于Log4j2-如何将XML配置转换为JDBC附加程序的.properties格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:34