创建一个示例应用程序,它将日志信息存储到文件以及数据库中。正在写入文件,但无法插入数据库
我正在使用log4j 2.12我已写入以下属性文件以读取日志 声明
status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = /Users/Downloads/debug.log
rootLogger.level = debug
#rootLogger.appenderRefs = database
#rootLogger.appenderRef.database.ref = DB
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = trace
appenders = rolling,console,db
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = /Users/Downloads/Previous/debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling1
logger.rolling1.name = com.abc.common.util
logger.rolling1.level = debug
logger.rolling1.additivity = true
logger.rolling1.appenderRef.rolling.ref = RollingFile
#logger.rolling2.name = org.springframework
#logger.rolling2.level = warn
#logger.rolling2.additivity = false
#logger.rolling2.appenderRef.stdout.ref = STDOUT
#logger.rolling2.appenderRef.rolling.ref = RollingFile
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#DB configuration
appender.db.type = JDBC
appender.db.name = org.apache.log4j.jdbc.JDBCAppender
appender.db.connectionSource.type= DriverManager
appender.db.connectionSource.connectionString = jdbc:mysql://127.0.0.1:8008/
appender.db.connectionSource.username = root
appender.db.connectionSource.password = localadmin
appender.db.connectionSource.driverClassName = com.mysql.cj.jdbc.Driver
appender.db.tableName = error_log
appender.db.columnConfigs[0].type = Column
appender.db.columnConfigs[0].name = id
appender.db.columnConfigs[0].pattern =%X{id}
appender.db.columnConfigs[0].isUnicode =false
appender.db.columnConfigs[1].type = Column
appender.db.columnConfigs[1].name = error_message
appender.db.columnConfigs[1].pattern =%msg
appender.db.columnConfigs[1].isUnicode =false
编写记录器的Java代码
package com.zinier.common.util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ZCustomLogger {
static final Logger logger = LogManager.getLogger(ZCustomLogger.class);
public static void main(String[] args) {
ZCustomLogger log = new ZCustomLogger();
log.addLog("gyana logging");
}
private void addLog(String parameter){
System.out.println("Hello world");
logger.debug("This is debug log");
logger.info("This is INFo log");
logger.warn("This is warn : " + parameter);
logger.error("This is error : " + parameter);
logger.fatal("This is fatal : " + parameter);
}
}
控制台中没有错误消息,但是数据库中没有记录。任何建议都将受到高度赞赏。