Log4j按级别输出日志到不同的文件

spring中对log的使用:
private static final Log logger = LogFactory.getLog(ContextLoader.class);
private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";
private static final Properties defaultStrategies;
可见:static final中的大写只针对String类型,代表字符串常量,其他类型则一般不用大写。

写法1:方便继承:protected , 不用写类名: this .getClass().getName()

 
  
  
  
  
  1. protected Logger logger = LoggerFactory.getLogger(this.getClass().getName()); 
方法2:在构造函数中初始化
  
  
  
  
  1. protected static Logger logger; 
  2.     CaseToSourcePanamaMojo(){ 
  3.         logger = LoggerFactory.getLogger(getClass().getName()); 
  4.     } 
例子:无配置文件
  
  
  
  
  1. Logger logger = Logger.getLogger(Log4jSample.class); 
  2.         BasicConfigurator.configure(); 
  3.         logger.setLevel(Level.INFO); 
  4.          
  5.         logger.debug("debug msg."); 
  6.         logger.info("info msg."); 
  7.         logger.warn("warn msg."); 
  8.         logger.error("error msg."); 
  9.         logger.fatal("fatal msg."); 
 
 
Log4j按级别输出日志到不同的文件
 

 

 

 

 
<? xml version ="1.0" encoding ="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- #log4j中有5级logger ,#FATAL 0 ,#ERROR 3 ,#WARN 4 ,#INFO 6 ,#DEBUG 7 -->
< log4j:configuration xmlns:log4j ='http://jakarta.apache.org/log4j/' >
<!-- 输出到控制台-->
<!--
<appender name="LOG.Console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
</appender>
-->
<!-- 将级别为DEBUG的信息输出到控制台-->
< appender name ="LOG.DEBUG" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="debug.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="DEBUG" />
< param name ="LevelMax" value ="DEBUG" />
</ filter >
</ appender >
<!-- 将级别为INFO的信息输出到控制台-->
< appender name ="LOG.INFO" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="info.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="INFO" />
< param name ="LevelMax" value ="INFO" />
</ filter >
</ appender >
<!-- 将级别为WARN的信息输出到控制台-->
< appender name ="LOG.WARN" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="warn.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="WARN" />
< param name ="LevelMax" value ="WARN" />
</ filter >
</ appender >
<!-- 将级别为ERROR的信息输出到控制台-->
< appender name ="LOG.ERROR" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="error.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="ERROR" />
< param name ="LevelMax" value ="ERROR" />
</ filter >
</ appender >
<!-- 将级别为FATAL的信息输出到控制台-->
< appender name ="LOG.FATAL" class ="org.apache.log4j.RollingFileAppender" >
< param name ="File" value ="fatal.log" />
< param name ="MaxFileSize" value ="5120KB" />
< param name ="MaxBackupIndex" value ="10" />
< layout class ="org.apache.log4j.PatternLayout" >
< param name ="ConversionPattern" value ="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</ layout >
< filter class ="org.apache.log4j.varia.LevelRangeFilter" >
< param name ="LevelMin" value ="FATAL" />
< param name ="LevelMax" value ="FATAL" />
</ filter >
</ appender >
<!--
<appender name="InitAction" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/dbcon.log"/>
<param name="MaxFileSize" value="5120KB"/>
<param name="MaxFileSize" value="10" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.'log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} - %m%n"/>
<param name="ConversionPattern" value="%d{DATE} [%-5p] %c {%F:%L} - %m%n"/>
</layout>
</appender>
-->
<!-- 设置org.logicalcobwebs包的输出级别为INFO-->
<!--
<category name="org.logicalcobwebs">
<priority value="INFO" />
<appender-ref ref="LOG.Console" />
</category>
-->
< root >
< priority value ="DEBUG" />
<!--
<appender-ref ref="LOG.Console" />
-->
< appender-ref ref ="LOG.DEBUG" />
< appender-ref ref ="LOG.INFO" />
< appender-ref ref ="LOG.WARN" />
< appender-ref ref ="LOG.ERROR" />
< appender-ref ref ="LOG.FATAL" />
</ root >
</ log4j:configuration >
 
 
import junit.framework.Test;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
public class Simpandfile
{
static Logger logger = Logger.getLogger(Test.class);
public static void main(String args[])
{
// 以property方式读到配置文件。
//PropertyConfigurator.configure("simple.properties");
DOMConfigurator.configure("xmllog4jconfig.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

=============================================================================

1、输出类的相对路径,而不是全部长度

<param name="ConversionPattern" value="%d [%X{requestURIWithQueryString}] %-5p %c{2} - %m%n" />

2、ibatis输出sql

      
      
      
      
  1. log4j.rootLogger=debug,stdout 
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
  4. log4j.appender.stdout.layout.conversionPattern=[%5p] %d{mm:ss}/(%F:%M:%L)%n%m%n%n 
  5. log4j.logger.com.ibatis=debug 
  6. log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
  7. log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
  8. log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
  9. log4j.logger.java.sql.Connection=debug 
  10. log4j.logger.java.sql.Statement=debug 
  11. log4j.logger.java.sql.PreparedStatement=debug,stdout 

 

 

 

 
 
 

 

你可能感兴趣的:(log4j,职场,休闲)