如何设置log4j属性,使每个线程输出到自己的日志文件?(How do I setup log4j

2019-06-26 15:06发布

我在任何给定的时间一个线程运行的类的多个实例。 我必须用于记录需要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);
    }

}

虽然上面创建每个线程我实例化一个文件,它不输出任何对这些文件。 任何帮助深表感谢。

Answer 1:

它不输出任何东西的文件,因为设置了一个记录器的正确的语法是:

prop.setProperty("log4j.logger.doSomething"+id,"DEBUG, WORKLOG");


Answer 2:

设置你的log4j.properties文件是这样的:假设你有2个线程callded ThreadCheckEsportazioniThreadAllineaCodiciTrasportoBDN

# 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



文章来源: How do I setup log4j properties so that each thread outputs to its own log file?