log4j的日志配置输出到mysql中

设置###

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());
	}
}

}

转载于:https://my.oschina.net/u/946001/blog/920074

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值