How do I setup log4j properties so that each thread outputs to its own log file?

后端 未结 2 500
独厮守ぢ
独厮守ぢ 2020-12-05 20:17

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 ins

相关标签:
2条回答
  • 2020-12-05 21:01

    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

    0 讨论(0)
  • 2020-12-05 21:03

    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");
    
    0 讨论(0)
提交回复
热议问题