log.properties文件:
LOG_DIR=D://logtest/
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}[%M]:%L - %m%n
### direct messages to file sigma.log###
log4j.appender.train=org.apache.log4j.RollingFileAppender
log4j.appender.train.File=${LOG_DIR}/logs/epserver.log
# Control the maximum log file size
log4j.appender.train.MaxFileSize=10240KB
# Archive log files (one backup file here)u6D4Bu8BD5
log4j.appender.train.MaxBackupIndex=10
log4j.appender.train.layout=org.apache.log4j.PatternLayout
log4j.appender.train.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
### eventlog #####################################################
log4j.logger.logevent1= DEBUG, logevent1
log4j.appender.logevent1=org.apache.log4j.FileAppender
log4j.appender.logevent1.File=${LOG_DIR}/logs/logevent1.log
log4j.appender.logevent1.layout=org.apache.log4j.PatternLayout
log4j.appender.logevent1.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.additivity.logevent1 = false
### eventlog2 #####################################################
log4j.logger.logevent2= DEBUG, logevent2
log4j.appender.logevent2=org.apache.log4j.FileAppender
log4j.appender.logevent2.File=${LOG_DIR}/logs/logevent2.log
log4j.appender.logevent2.layout=org.apache.log4j.PatternLayout
log4j.appender.logevent2.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.additivity.logevent2 = false
你在使用这个时候,如果不同的方法,打印到不同的地方,可以修改
### eventlog2 #####################################################
中那段代码,这样就可以起到作用。
java代码:
import org.apache.log4j.Logger;
public class DemoLog {
private static Logger logevent1 = Logger.getLogger("logevent1");
private static Logger logevent2 = Logger.getLogger("logevent2");
public static void main(String[] args) {
logevent1.info("logevent1");
logevent2.info("logevent2");
}
}
用到的jar包