设置###
log4j.rootLogger = debug,stdout,appender2
输出信息到控制抬
log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.appender2=org.apache.log4j.jdbc.JDBCAppender log4j.appender.appender2.driver=com.mysql.jdbc.Driver log4j.appender.appender2.URL=jdbc:mysql://localhost:3306/jpress?useUnicode=true&characterEncoding=UTF-8 log4j.appender.appender2.user=root log4j.appender.appender2.password=XXX log4j.appender.appender2.Threshold = error log4j.appender.appender2.sql=insert log4j_mysql (create_time,log) VALUES ('%d{yyyy-MM-dd hh:mm:ss}', '%c %p %m %n') log4j.appender.appender2.layout=org.apache.log4j.PatternLayout
create database log4j_mysql
;
use log4j_mysql
;
CREATE TABLE log4j_mysql
(id
bigint(20) unsigned NOT NULL AUTO_INCREMENT,create_time
datetime NOT NULL,log
varchar(200) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
package com.tools;
import org.apache.log4j.Logger;
public class LogDemo { private static Logger logger = Logger.getLogger(BagUtils.class);
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
logger.info("message ......");
int i = 5 / 0;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("....不能除0" + e.getMessage());
}
}
}