Advantage of log4j

后端 未结 8 2141
余生分开走
余生分开走 2021-02-13 18:57

What\'s the advantage of log4j over set System.out and System.err to output to a log file?

8条回答
  •  深忆病人
    2021-02-13 19:40

    Take a look and you will understand the power of log4j :

    log4j.properties I used once for a project :

    # ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
    
    # No appenders for rootLogger 
    log4j.rootLogger=OFF
    
    folder=..
    prefix=
    fileExtension=.log
    htmlExtension=${fileExtension}.html
    datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
    layout=%d{${datestamp}} ms=%-4r [%t] %-5p %l %n%m %n%n
    
    # myLogger logger
    log4j.logger.myLogger=ALL, stdout, infoFile, infoHtml, errorFile
    
    # stdout 
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=${layout}
    # infoFile 
    log4j.appender.infoFile=org.apache.log4j.FileAppender
    log4j.appender.infoFile.File=${folder}/${prefix}_info${fileExtension}
    log4j.appender.infoFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.infoFile.layout.ConversionPattern=${layout}
    # infoHtml 
    log4j.appender.infoHtml=org.apache.log4j.FileAppender
    log4j.appender.infoHtml.File=${folder}/${prefix}_info${htmlExtension}
    log4j.appender.infoHtml.layout=org.apache.log4j.HTMLLayout
    log4j.appender.infoHtml.layout.Title=Logs
    log4j.appender.infoHtml.layout.LocationInfo=true
    # errorFile 
    log4j.appender.errorFile=org.apache.log4j.FileAppender
    log4j.appender.errorFile.File=${folder}/${prefix}_error${fileExtension}
    log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.errorFile.layout.ConversionPattern=${layout}
    
    # APPENDERS SETTINGS
    log4j.appender.stdout.Threshold = ALL
    log4j.appender.infoFile.Threshold = INFO
    log4j.appender.infoHtml.Threshold = INFO
    log4j.appender.errorFile.Threshold = WARN.
    

    To change the variables in your java code you can do :

    Loading Configuration

    Log4j will automatically load the configuration if it is stored in a file called "log4j.properties" and is present on the classpath under "" (e.g. WEB-INF/classes/log4j.properties).

    I don't like that approach and prefer to load the configuration explicitly by calling:

    PropertyConfigurator.configure( Config.ETC + "/log4j.properties" ); This way I can reload the configuration at any time as long as my application is still running. I like to add a button to an administrative jsp, "Reload Log4J".

    Dynamic Log File Location

    Many people complain that Log4j forces you to hard-code the location where your logs will be kept. Actually, it is possible to dynamically choose the log-file location, especially if you use the ${log.dir} property substitution technique above. Here's how:

    String dynamicLog = // log directory somehow chosen... 
    Properties p = new Properties( Config.ETC + "/log4j.properties" ); 
    p.put( "log.dir", dynamicLog ); // overwrite "log.dir" 
    PropertyConfigurator.configure( p );
    

提交回复
热议问题