2016-02-10 81 views
0

我無法使用log4j將日誌插入到AUDIT表中。我能夠附加到控制檯但不能添加到數據庫。請檢查這三個文件:無法使用log4j在數據庫中插入數據

  1. log4j.xml
  2. Auditlogger(包裝類)
  3. TestAudit.java

我沒有得到任何解決方案。

的log4j.xml

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 
    <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
     <param name="Target" value="System.out"/> 
     <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%d %-5p [%t] %C{3} (%F:%L) - %m%n" /> 
      <!-- value="%-5p %c{1} - %m%n" --> 
     </layout> 
    </appender> 
    <appender name="DB" class="org.apache.log4j.jdbcplus.JDBCAppender"> 
     <layout class="org.apache.log4j.PatternLayout"> 
      <param name="URL" value="jdbc:postgresql://*.*.*.*:1234/DB"/> 
      <param name="driver" value="org.postgresql.Driver"/> 
      <param name="user" value="------"/> 
      <param name="password" value="*******"/> 
      <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')"/> 
     </layout> 
    </appender> 
    <appender name="NULL" class="org.apache.log4j.varia.NullAppender"></appender> 
    <category name="com.ultimatix.cis.AuditLogger"> 
     <priority value="DEBUG"></priority> 
     <appender-ref ref="console"></appender-ref> 
     <appender-ref ref="DB"></appender-ref> 
    </category> 
    <root> 
     <priority value="off"></priority> 
     <appender-ref ref="NULL"></appender-ref> 
    </root> 

AuditLogger.java

public class AuditLogger { 
    private static String FQCN = "com.ultimatix.cis.AuditLogger"; 

    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

public class TestAudit { 
    public static void main (String args[]) { 
     Logger.getLogger(TestAudit.class).debug("Before audit"); 
     AuditLogger.info("ashutosh","accesed", "User edit page"); 
     Logger.getLogger(TestAudit.class).debug("after audit"); 
    } 
} 
+0

的XML被打破:你包佈局標籤內的JDBC-追加程序的參數標籤。 – Fildor

+0

但你也可能會覺得這很有趣:http://stackoverflow.com/a/1366684/982149 – Fildor

回答

0

我敢肯定,你必須改變

<appender name="DB" class="org.apache.log4j.jdbcplus.JDBCAppender"> 
      <layout class="org.apache.log4j.PatternLayout"> 
      <param name="URL" value="jdbc:postgresql://*.*.*.*:1234/DB"/> 
      <param name="driver" value="org.postgresql.Driver"/> 
      <param name="user" value="------"/> 
      <param name="password" value="*******"/> 
      <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')"/> 
    </layout> 
</appender> 

<appender name="DB" class="org.apache.log4j.jdbcplus.JDBCAppender"> 
      <param name="URL" value="jdbc:postgresql://*.*.*.*:1234/DB"/> 
      <param name="driver" value="org.postgresql.Driver"/> 
      <param name="user" value="------"/> 
      <param name="password" value="*******"/> 
      <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')"/> 
      <layout class="org.apache.log4j.PatternLayout"> </layout> 
</appender> 
相關問題