我无法使用AUDIT
将日志插入log4j
表。我能够附加到控制台但不能附加到数据库。请检查这三个文件:
log4j.xml
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 :(得分: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>