我在任何给定时间都有多个线程类实例。我有log4j设置用于记录需求。
我需要一种方法来设置log4j,以便我的线程类的每个实例都将其日志输出到不同的日志文件中。
这是我所做的(伪代码)
public class doSomething extends Thread {
private Logger d_logger;
public doSomething(int id){
d_logger = Logger.getLogger("doSomething"+id);
String logFileName = "doSomething"+id+".log";
Properties prop = new Properties;
prop.setProperty("doSomething"+id,"DEBUG, WORKLOG");
prop.setProperty("log4j.appender.WORKLOG","org.apache.log4j.FileAppender");
prop.setProperty("log4j.appender.WORKLOG.File", logFileName);
prop.setProperty("log4j.appender.WORKLOG.layout","org.apache.log4j.PatternLayout");
prop.setProperty("log4j.appender.WORKLOG.layout.ConversionPattern","%d %c{1} - %m%n");
prop.setProperty("log4j.appender.WORKLOG.Threshold","INFO");
PropertyConfigurator.configure(prop);
}
public void run(){
d_logger.info("Starting to doSomething number" + id);
}
}
虽然上面为我实例化的每个线程创建了一个文件,但它不会向这些文件输出任何内容。任何帮助深表感谢。
答案 0 :(得分:13)
它没有向文件输出任何内容,因为设置记录器的正确语法是:
prop.setProperty("log4j.logger.doSomething"+id,"DEBUG, WORKLOG");
答案 1 :(得分:0)
设置你的log4j.properties文件,如下所示:
假设您有2个线程调用ThreadCheckEsportazioni
和ThreadAllineaCodiciTrasportoBDN
# imposta a debug il livello del logger di root e lo chiama debug e debugFile
log4j.rootLogger=DEBUG
#logger debug
log4j.logger.it.consorziometis.sispc.exportbdn.job.ThreadAllineaCodiciTrasportoBDN=DEBUG, ThreadAllineaCodiciTrasportoBDN
log4j.logger.it.consorziometis.sispc.exportbdn.job.ThreadCheckEsportazioni=DEBUG, ThreadCheckEsportazioni
# Associa i tipi agli appendere
#log4j.appender.debug=org.apache.log4j.ConsoleAppender
log4j.appender.ThreadCheckEsportazioni=org.apache.log4j.RollingFileAppender
log4j.appender.ThreadAllineaCodiciTrasportoBDN=org.apache.log4j.RollingFileAppender
# Associa un PatternLayout al exportBdn.
log4j.appender.ThreadCheckEsportazioni.layout=org.apache.log4j.PatternLayout
log4j.appender.ThreadCheckEsportazioni.layout.ConversionPattern=[%d][%5p][%c]-> %m%n
log4j.appender.ThreadCheckEsportazioni.File=/apps/consorziometis/procedure_automatiche/ExportBDNService/logs/exportBDN.log
log4j.appender.ThreadCheckEsportazioni.MaxFileSize=10MB
log4j.appender.ThreadCheckEsportazioni.MaxBackupIndex=10
log4j.appender.ThreadCheckEsportazioni.append=true
# Associa un PatternLayout al allineaCodiciTrasporto.
log4j.appender.ThreadAllineaCodiciTrasportoBDN.layout=org.apache.log4j.PatternLayout
log4j.appender.ThreadAllineaCodiciTrasportoBDN.layout.ConversionPattern=[%d][%5p][%c]-> %m%n
log4j.appender.ThreadAllineaCodiciTrasportoBDN.File=/apps/consorziometis/procedure_automatiche/ExportBDNService/logs/allineaCodiciTrasporto.log
log4j.appender.ThreadAllineaCodiciTrasportoBDN.MaxFileSize=10MB
log4j.appender.ThreadAllineaCodiciTrasportoBDN.MaxBackupIndex=10
log4j.appender.ThreadAllineaCodiciTrasportoBDN.append=true
在每个线程中你必须设置:
private static Logger log = Logger.getLogger(ThreadCheckEsportazioni.class);
代表ThreadCheckEsportazioni
和
private static Logger log = Logger.getLogger(ThreadAllineaCodiciTrasportoBDN.class);
用于线程ThreadAllineaCodiciTrasportoBDN