Log4j 在ssh下的配置
Posted imaye
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Log4j 在ssh下的配置相关的知识,希望对你有一定的参考价值。
配置pom.xml
安装必要的jar包
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
配置log4j.properties
### set log levels ###
log4j.rootLogger = info , INFO ,WARN, DEBUGD , ERROR
### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### 输出到日志文件 ###
log4j.appender.WARN = org.apache.log4j.DailyRollingFileAppender
log4j.appender.WARN.File = ${log4jdir}/logs/warn/log_info.log
log4j.appender.WARN.Append = true
log4j.appender.WARN.Threshold = DEBUG ## 输出DEBUG级别以上的日志
log4j.appender.WARN.layout = org.apache.log4j.PatternLayout
log4j.appender.WARN.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出到日志文件 ###
log4j.appender.INFO = org.apache.log4j.RollingFileAppender
log4j.appender.INFO.File = ${log4jdir}/logs/info/log_info.log
log4j.appender.INFO.Append = true
log4j.appender.INFO.Threshold = INFO ## 输出DEBUG级别以上的日志
log4j.appender.INFO.MaxFileSize = 1MB
log4j.appender.INFO.layout = org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.INFO.MaxBackupIndex = 10
log4j.appender.INFO.Encoding = UTF-8
### 输出到日志文件 ###
log4j.appender.DEBUGD = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUGD.File = ${log4jdir}/logs/debug/log.log
log4j.appender.DEBUGD.Append = true
log4j.appender.DEBUGD.Threshold = DEBUG ## 输出DEBUG级别以上的日志
log4j.appender.DEBUGD.layout = org.apache.log4j.PatternLayout
log4j.appender.DEBUGD.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.DEBUGD.Encoding = UTF-8
### 保存异常信息到单独文件 ###
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File = ${log4jdir}/logs/error/error.log
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.ERROR.Encoding = UTF-8
配置Webxml 添加ServletContextListener
<listener> <listener-class>com.log.log4jInit</listener-class> </listener>
添加监听类log4jInit
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class log4jInit implements ServletContextListener { public static final String log4jdirkey = "log4jdir"; public void contextDestroyed(ServletContextEvent servletcontextevent) { System.getProperties().remove(log4jdirkey); } public void contextInitialized(ServletContextEvent servletcontextevent) { String log4jdir = servletcontextevent.getServletContext().getRealPath("/"); //System.out.println("log4jdir:"+log4jdir); System.setProperty(log4jdirkey, log4jdir); } }
以上是关于Log4j 在ssh下的配置的主要内容,如果未能解决你的问题,请参考以下文章