1. 程式人生 > >Log4j 在ssh下的配置

Log4j 在ssh下的配置

key ati jini listener out text stdout vax logger

配置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下的配置