How do I setup log4j properties so that each threa

2019-01-14 13:09发布

问题:

I have multiple instances of a thread class running at any given time. I have log4j setup to be used for logging needs.

I need a way to setup log4j so that every instance of my thread class outputs its log in a different log file.

Here is what I have done ( in pseudo code)

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);
    }

}

Though the above creates a file for every thread I instantiate, It does not output anything to those files. Any help is much appreciated.

回答1:

It's not outputting anything to the files because the correct syntax for setting up a logger is:

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


回答2:

set up your log4j.properties file like this: suppose that you have 2 thread callded ThreadCheckEsportazioni and 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

in each thread you must set:

private static Logger log = Logger.getLogger(ThreadCheckEsportazioni.class);

for ThreadCheckEsportazioni and

private static Logger log = Logger.getLogger(ThreadAllineaCodiciTrasportoBDN.class);

for thread ThreadAllineaCodiciTrasportoBDN