Log4J写入到SQL SERVER数据库中

时间:2022-09-21 19:08:07

定义: log4j.properties

log4j.rootCategory=INFO,A3
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy MMM dd HH:mm:ss,SSS}%m%n

log4j.logger.Java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout,logfile,A3 


#定义A3输出到数据库
log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.A3.URL=jdbc\:sqlserver\://localhost\:1433;DatabaseName\=zhaochi_1
log4j.appender.A3.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
log4j.appender.A3.user=sa
log4j.appender.A3.password=pwterp

#定义A3的布局和执行的SQL语句
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=INSERT INTO log4j(createDate,thread,grade,class,message) values('%d','%t','%-5p','%c','%m')

新建名为: log4j 的数据库, 字段如下:

createDate  varchar(50) Checked
thread  varchar(450)    Checked
grade   varchar(450)    Checked
class   varchar(500)    Checked
message varchar(MAX)    Checked
logId   int Unchecked   Unchecked