审核记录器无法正常工作

时间:2015-01-06 10:09:05

标签: java

我的程序没有给出错误,但是日志文件是空的,即使在数据库中,审计跟踪表中也没有条目

AuditLogger.java

package com.action;

import org.apache.log4j.Level;

import org.apache.log4j.Logger;

import org.apache.log4j.MDC;


public class AuditLogger {

private static String FQCN =  "myapplication.audit.AuditLogger";

public static void debug (String userid, String activity,String activityDetail) 
{
    MDC.put("userid", userid);
    MDC.put("activity", activity);
    Logger.getLogger(AuditLogger.class.getName()).log(FQCN, Level.DEBUG,  activityDetail, null);
    MDC.clear();  
}    
public static void info (String userid, String activity,String activityDetail) 
{
    MDC.put("userid", userid);
    MDC.put("activity", activity);
    Logger.getLogger(AuditLogger.class.getName()).log(FQCN, Level.INFO,  activityDetail, null);
    MDC.clear();
}

}

TestAudit.java

package com.action;

 import org.apache.log4j.Logger;


public class TestAudit {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Logger.getLogger(TestAudit.class).debug("Before audit");
    AuditLogger.info("ashutosh","accesed", "User edit page");
    Logger.getLogger(TestAudit.class).debug("after audit");
}

}

的log4j.xml

   <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
          <param name="File" value="logs/audit.log"/>
          <param name="Append" value="true"/>
          <layout class="org.apache.log4j.PatternLayout">
                 <param name="ConversionPattern"
                       value="%d %-5p [%t] %C{3} (%F:%L) - %m%n"/>
          </layout>
   </appender>

   <appender name="DB" class="org.apache.log4j.jdbc.JDBCAppender">
          <param name="URL" value="jdbc:mysql://localhost:3306/amey"/>
          <param name="driver" value="com.mysql.jdbc.Driver"/>
          <param name="user" value="root"/>
          <param name="password" value="root"/>
          <param name="sql" value="INSERT INTO AUDIT(USER_ID, TIMESTAMP, LEVEL, ACTIVITY, ACTIVITY_DETAIL) VALUES('%X{userid}','%d{yyyy-MM-dd HH:mm:ss.SSS}','%p', '%X{activity}', '%m')"/>

   </appender>

   <appender name="NULL" class="org.apache.log4j.varia.NullAppender"/>

   <category  name="myapplication.audit">
          <priority value="DEBUG"/>
          <appender-ref ref="FILE"/>
          <appender-ref ref="DB"/>
   </category>
   <category  name="myapplication.test">
          <priority value="DEBUG"/>
          <appender-ref ref="FILE"/>
   </category>

   <root>
          <priority value="off"/>
          <appender-ref ref="NULL"/>
   </root>

1 个答案:

答案 0 :(得分:0)

root logger的

优先级off和rootlogger的NullAppender会阻止日志记录。

这就是Nul​​lAppender javadocs所说的:

A NullAppender merely exists, it never outputs a message to any device

这是关闭优先级时的含义:

The OFF has the highest possible rank and is intended to turn off logging.

将其更改为说文件,它将写入文件audit.log ....