idea配置log4j

  • Post author:
  • Post category:其他


Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。

maven依赖

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

log4j配置

在src/mian/resource目录下新建log4j.properties

# Global logging configuration
---root日志设置
#log4j.rootLogger=info,dailyRollingFile,stdout
log4j.rootLogger=debug,stdout,dailyRollingFile
---输出到控制台
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][%-5p][%l][%t] - %m - %n
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c]:%L - %m%n

--输出日志文件(每日自动生成一个日志文件)
log4j.appender.dailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyRollingFile.File=${catalina.base}/logs/allcam/mediaplat.log
log4j.appender.dailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyRollingFile.layout.ConversionPattern=[%d][%-5p][%l][%t] - %m - %n

--jdbc日志
log4j.logger.com.ibatis=ERROR   
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=ERROR   
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=ERROR   
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR
--- 打印sql
log4j.logger.java.sql.Connection=ERROR   
log4j.logger.java.sql.Statement=ERROR   
log4j.logger.java.sql.PreparedStatement=ERROR   
log4j.logger.java.sql.ResultSet=ERROR 
---框架日志
log4j.logger.org.springframework=ERROR
log4j.logger.org.apache.struts2=ERROR
log4j.logger.org.apache.commons=ERROR
log4j.logger.com.mchange=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.com.opensymphony.xwork2=ERROR
log4j.logger.com.opensymphony.xwork2.ActionSupport=ERROR
### set log levels ###
log4j.rootLogger = debug ,  stdout, D ,  E

### 输出到控制台 ###
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.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 保存Debug信息到单独文件 ### ## 只输出ERROR级别以上的日志!!!
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [%p] - %m%n
package example;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HelloWorld {
  public static Log log = LogFactory.getLog(HelloWorld.class);
  public static void main(String[] argv) {
             String test="sdibt";
            log.info("this is info:"+test);
            log.error("this is error:"+test);
            log.debug("this is debug:"+test);
  }
}