我有2个类,LogonPolicyServer和SCE。我需要从LogonPolicyServer到第一个文件(firstFile.log)的日志信息,从SCE到第一个文件的日志信息,从SCE到第二个文件(secondFile.log)的日志错误以及从SCE类到控制台的打印信息和错误,但是当我尝试这样做,LogonPolicyServer的信息也会打印到控制台。 我的log4j.properties:
log4j.rootLogger=INFO, console log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.logger.infoNTS=DEBUG, file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.file=log/spider.cisco.log log4j.appender.file.MaxFileSize=1GB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.logger.errorlog=ERROR, errorFile log4j.appender.errorFile=org.apache.log4j.RollingFileAppender log4j.appender.errorFile.file=log/error.spider.cisco.log log4j.appender.errorFile.MaxFileSize=1GB log4j.appender.errorFile.MaxBackupIndex=10 log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout log4j.appender.errorFile.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.appender.errorFile.Threshold=ERROR
在LogonPolicyServer中,我会尝试:
private static final Logger log = Logger.getLogger("infoNTS"); //connect logger
然后打印信息:
log.debug("Record "+i+":"+record.name+" "+res.getString(3)+" "+res.getString(4));
SCE:
private static final Logger log = Logger.getLogger("infoNTS"); private static final Logger errorLog = Logger.getLogger("errorlog");
然后打印:
try{ api.synchronizePushStart(resultHandler); log.info("Start synchronization for SCE:"+sceIP+" started successfully"); }catch(Exception e){ errorLog.error("Start synchronization for SCE:"+sceIP+" failed!", e); }
有什么问题? UPD:最新的log4j.properties
log4j.rootLogger=FATAL, console log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.logger.infoNTS=DEBUG, file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.file=log/spider.cisco.log log4j.appender.file.MaxFileSize=1GB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.logger.errorlog=ERROR, errorFile log4j.appender.errorFile=org.apache.log4j.RollingFileAppender log4j.appender.errorFile.file=log/error.spider.cisco.log log4j.appender.errorFile.MaxFileSize=1GB log4j.appender.errorFile.MaxBackupIndex=10 log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout log4j.appender.errorFile.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.appender.errorFile.Threshold=ERROR
UPD2: 这有助于:
log4j.rootLogger=OFF, fatalFile log4j.appender.fatalFile=org.apache.log4j.RollingFileAppender log4j.appender.fatalFile.file=log/fatal.log log4j.appender.fatalFile.MaxFileSize=10MB log4j.appender.fatalFile.MaxBackupIndex=10 log4j.appender.fatalFile.layout=org.apache.log4j.PatternLayout log4j.appender.fatalFile.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.logger.infoNTScopy=DEBUG, file log4j.logger.infoNTS=DEBUG, file, console log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.file=log/spider.cisco.log log4j.appender.file.MaxFileSize=1GB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.logger.errorlog=ERROR, errorFile, console log4j.appender.errorFile=org.apache.log4j.RollingFileAppender log4j.appender.errorFile.file=log/error.spider.cisco.log log4j.appender.errorFile.MaxFileSize=1GB log4j.appender.errorFile.MaxBackupIndex=10 log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout log4j.appender.errorFile.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n log4j.appender.errorFile.Threshold=ERROR log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n
在LogonPolicyServer中:
private static final Logger log = Logger.getLogger("infoNTScopy");
但现在我需要禁用/删除root logger。
答案 0 :(得分:1)
尝试将根记录器指定为FATAL
并将日志转移到虚拟文件:
log4j.rootLogger=FATAL, fatalFile
log4j.appender.fatalFile=org.apache.log4j.RollingFileAppender
log4j.appender.fatalFile.file=log/fatal.log
log4j.appender.fatalFile.MaxFileSize=10MB
log4j.appender.fatalFile.MaxBackupIndex=10
log4j.appender.fatalFile.layout=org.apache.log4j.PatternLayout
log4j.appender.fatalFile.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n
并在console appender
中添加log4j.logger.infoNTS
作为:
log4j.logger.infoNTS=DEBUG, file, nConsole
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.file=log/spider.cisco.log
log4j.appender.file.MaxFileSize=1GB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern=%d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n
log4j.appender.nConsole= org.apache.log4j.ConsoleAppender
log4j.appender.nConsole.target=System.out
log4j.appender.nConsole.layout = org.apache.log4j.PatternLayout
log4j.appender.nConsole.layout.ConversionPattern = %d{[dd.MM.yyyy] [HH:mm:ss]} %p [%t] %c (%F:%L) - %m%n