加载中...

log4j简单案例


package log4j;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jG {
// private static Logger log =Logger.getLogger(Log4jG.class);
 private static Logger log=LogManager.getLogger(Log4jG.class.getName());
 private static Logger log1=LogManager.getLogger("myTest1");
 private static Logger log2=LogManager.getLogger("myTest2");
 private static Logger log3=LogManager.getLogger("myTest3");
 public static void main(String[] args){

//  BasicConfigurator.configure();
  PropertyConfigurator.configure("c:\\log4j.properties");
  log.debug("debug message");
  log.info("info message");
  log.error("error message");

  
  log1.debug("test1  debug");
  log1.info("test1  info message");
  log1.error("test1  error message");
  
  log2.debug("test2  debug");
  log2.info("test2  info message");
  log2.error("test2  error message");
  
  
  log3.debug("test3  debug");
  log3.info("test3  info message");
  log3.error("test3  error message");

 }
}

log4j.properties

log4j.rootLogger=INFO,A
log4j.appender.A=org.apache.log4j.FileAppender
log4j.appender.A.file=c:/rootlog.txt
log4j.appender.A.layout=org.apache.log4j.PatternLayout

log4j.logger.myTest1= DEBUG, test1
log4j.appender.test1=org.apache.log4j.FileAppender
log4j.appender.test1.File=c:/test1.log
log4j.appender.test1.layout=org.apache.log4j.PatternLayout
log4j.appender.test1.layout.ConversionPattern=%d %p [%c] - %m%n

  

log4j.logger.myTest2= DEBUG, test2
log4j.appender.test2=org.apache.log4j.FileAppender
log4j.appender.test2.File=c:/test2.log
log4j.appender.test2.layout=org.apache.log4j.PatternLayout
log4j.appender.test2.layout.ConversionPattern=%d %p [%c] - %m%n



log4j.logger.myTest3= DEBUG, R
log4j.appender.R=org.apache.log4j.RollingFileAppender 

log4j.appender.R.File= C:/rolling.log

# Control the maximum log file size 

log4j.appender.R.MaxFileSize=2KB 

# Archive log files (one backup file here) 

log4j.appender.R.MaxBackupIndex=1 

log4j.appender.R.layout=org.apache.log4j.PatternLayout 

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

log4j1.2.17:http://logging.apache.org/log4j/1.2/download.html

log4j1.2.17  API:http://logging.apache.org/log4j/1.2/apidocs/


还没有评论.