My Octopress Blog

A blogging framework for hackers.

Log4j Mdc Save Log to Database

| Comments

log4j可以持久化跟踪日志,即保存到数据库详细配置

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

public class AjLogDb {
    private static Logger logger = Logger.getLogger(AjLogDb.class);

    public static void main(String args[]) {
        AjLogDb.logDb("qqqqqqqqqqqqqq", "dfdfdfdfd", "fdsfasfas");
    }

    public static void logDb(String id, String operator, String taskname) {
        MDC.put("id", System.currentTimeMillis());
        MDC.put("orderId", id);
        MDC.put("taskName", taskname);
        MDC.put("operator", operator);
        MDC.put("remark",taskname);
        logger.debug(taskname);
    }
}

问题1:如果报找不到logger(AjLogDb),可以通过以下方法来解决

1
log4j.logger.AjLogDb=DEBUG,DB

Comments