# Define the root logger with appender DB and STDOUT log4j.rootLogger = DEBUG, DB,STDOUT # Define the DB appender log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender # Set JDBC URL #log4j.appender.DB.URL=jdbc:mysql://localhost/DBNAME log4j.appender.DB.URL=jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC&characterEncoding=utf-8 # Set Database Driver #log4j.appender.DB.driver=com.mysql.jdbc.Driver # mysql8.0驱动 log4j.appender.DB.driver=com.mysql.cj.jdbc.Driver # Set database user name and password #log4j.appender.DB.user=user_name log4j.appender.DB.user=root #log4j.appender.DB.password=password log4j.appender.DB.password=root # Set the SQL statement to be executed. #log4j.appender.DB.sql=INSERT INTO LOGS VALUES('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m') log4j.appender.DB.sql=insert into logs(user_id,date_time,logger,level,message) values ('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m') # Define the layout for file appender log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.DB.sql=insert into logs(user_id,date_time,logger,level,message) values ('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m') # Define the layout for file appender log4j.appender.DB.layout=org.apache.log4j.PatternLayout # log4j.appender.DB.layout.ConversionPattern=insert into logs(user_id,date_time,logger,level,message) values ('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')
和
1 2 3 4 5
#log4j.appender.DB.sql=insert into logs(user_id,date_time,logger,level,message) values ('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m') # Define the layout for file appender log4j.appender.DB.layout=org.apache.log4j.PatternLayout log4j.appender.DB.layout.ConversionPattern=insert into logs(user_id,date_time,logger,level,message) values ('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')
publicclassLog4jExample { /* Get actual class name to be printed on */ staticLoggerlog= Logger.getLogger(Log4jExample.class.getName());
publicstaticvoidmain(String[] args)throws IOException, SQLException { log.debug("Hello this is a debug message"); log.info("Hello this is an info message"); } }