ssh整合log4j(控制台,本地文件,数据库)

jopen 9年前

1.web.xml中添加:

<context-param>      <param-name>log4jConfigLocation</param-name>      <param-value>WEB-INF/classes/log4j.properties</param-value>  </context-param>  <context-param>      <param-name>log4jRefreshInterval</param-name>      <param-value>6000</param-value>  </context-param>  <context-param>      <param-name>webAppRootKey</param-name>      <param-value>webApp.root</param-value>  </context-param>  <listener>      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  </listener>

2.log4j.properties

# Set root logger level to error  log4j.rootLogger=INFO, Console, File, DATABASE    ###### Console appender definition #######    # All outputs currently set to be a ConsoleAppender.  log4j.appender.Console=org.apache.log4j.ConsoleAppender  log4j.appender.Console.layout=org.apache.log4j.PatternLayout  log4j.appender.Console.layout.ConversionPattern=%-5p [%d{yy.MM.dd HH:mm:ss}][%c]: %m%n    ###### File appender definition #######  log4j.appender.File=org.apache.log4j.DailyRollingFileAppender  log4j.appender.File.File=${catalina.home}/webapps/项目名称/WEB-INF/logs/log4j.log  log4j.appender.File.layout=org.apache.log4j.PatternLayout  log4j.appender.File.layout.ConversionPattern=%-5p [%d{yy.MM.dd HH:mm:ss}][%c]: %m%n    ###### DATABASE appender definition #######  log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender  log4j.appender.DATABASE.URL=jdbc:oracle:thin:@x.x.x.x:1521:x  log4j.appender.DATABASE.driver=oracle.jdbc.driver.OracleDriver  log4j.appender.DATABASE.user=xxx  log4j.appender.DATABASE.password=xxx  log4j.appender.DATABASE.sql=INSERT INTO tb_loginfo_shwr (log_date, log_thread, log_level, log_class,log_message,log_user) values ('%d', '%t', '%p', '%l', '%m', '%X{user}')   log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout    ##Hibernate log begin##   log4j.logger.org.hibernate=info  log4j.logger.org.hibernate.SQL=debug  log4j.logger.org.hibernate.HQL=debug

住:获取用户名,应写log4j的拦截器。

来自: http://my.oschina.net/chenhao901007/blog/373671