使用Java中的log4j2 jdbc附加程序进行自定义日志记录

时间:2019-05-24 06:28:52

标签: java jdbc log4j2 appender

我正在尝试使用log4j2将事件记录在数据库中。具体地说,我为此目的在属性配置文件中使用log4j2 jdbc appender。通过使用默认的log4j2日志级别,一切正常。日志值已成功插入数据库中。 这是我的log4j2属性文件中的初始附加程序:

# JDBC appender
appender.db.type = Jdbc
appender.db.name = databaseAppender
appender.db.tableName = db_name.test
appender.db.cf.type = ConnectionFactory
appender.db.cf.class = com.myproject.ConnectionFactory
appender.db.cf.method = getConnection
appender.db.col2.type = Column
appender.db.col2.name = message
appender.db.col2.pattern = %m
appender.db.col3.type = Column
appender.db.col3.name = category
appender.db.col3.pattern = %M{1}
appender.db.col4.type = Column
appender.db.col4.name = timestamp
appender.db.col4.isEventTimestamp = true
appender.db.col5.type = Column
appender.db.col5.name = log_level
appender.db.col5.pattern = %-5p
appender.db.filter.threshold.type = ThresholdFilter
appender.db.filter.threshold.level = info
appender.db.filter.threshold.onMatch = Accept
appender.db.filter.threshold.onMismatch = Deny

但是随后我尝试在此log4j2网站https://logging.apache.org/log4j/2.x/manual/customloglevels.html

之后创建自定义日志级别
LOG.log(Level.forName("DIAG", 350), "a diagnostic message");

它以定义的日志级别成功记录事件。甚至将数据放入数据库中,因为在我的jdbc附加程序中,阈值级别为info。作为信息intLevel>诊断intLevel。

我的问题是,如果我在我的jdbc附加程序中将阈值级别更改为diag,则根本不起作用,即

appender.db.filter.threshold.level = diag

我的数据库中没有任何内容。所以我的第一个问题是为什么它不起作用,请尽可能提供任何解决方案。

另一件事是,我将DIAG的intLevel抽搐到了10,例如:

LOG.log(Level.forName("DIAG", 10), "a diagnostic message");

现在它正在插入数据库,但还会引发一些错误。

2019-05-24 12:06:00,248 main WARN Error while converting string [diag] to type [class org.apache.logging.log4j.Level]. Using default value [null]. java.lang.IllegalArgumentException: Unknown level constant [DIAG].
at org.apache.logging.log4j.Level.valueOf(Level.java:320)
at org.apache.logging.log4j.core.config.plugins.convert.TypeConverters$LevelConverter.convert(TypeConverters.java:288)
at org.apache.logging.log4j.core.config.plugins.convert.TypeConverters$LevelConverter.convert(TypeConverters.java:284)
at org.apache.logging.log4j.core.config.plugins.convert.TypeConverters.convert(TypeConverters.java:419)
at org.apache.logging.log4j.core.config.plugins.visitors.AbstractPluginVisitor.convert(AbstractPluginVisitor.java:149)
at org.apache.logging.log4j.core.config.plugins.visitors.PluginAttributeVisitor.visit(PluginAttributeVisitor.java:45)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:253)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:964)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:904)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:896)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:896)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:514)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:238)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:250)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:548)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:620)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:637)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:581)
at com.myproject.Main.<clinit>(Main.java:35)

任何帮助将不胜感激,并在此先感谢您。

1 个答案:

答案 0 :(得分:1)

由于您尚未在log4j2.properties文件中定义intLevel,因此添加

.as-console-wrapper { max-height: 100% !important; top: 0; }

其中350是intLevel。

这可以消除错误。