Advantage of log4j

后端 未结 8 2152
余生分开走
余生分开走 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:30

    my favorites (not all)

    • Ability to set parameters of logging in config, without recompiling
    • Ability to set the way log is written (from text file to SMTP sender)
    • Ability to filter by severity
    0 讨论(0)
  • 2021-02-13 19:31
    • logging (Document historical business events that occur, you can check old logs)
    • track the application (project flow)
    • debugging the application (Detailed information what occurs in a method at granular level //data, value and all inside methods)
    • error handling (information about specific error that occur)
    0 讨论(0)
  • 2021-02-13 19:34

    Log4j offers the ability to rotate your log files based on size and delete them based on quantity (logrotate), so your servers don't fill up their disks. Personally I think that is one of the more valuable features in Log4j.

    Also Log4j is popular and understood by many developers. The last three companies I've worked at have all used Log4j in most projects.

    0 讨论(0)
  • 2021-02-13 19:39

    Actually, you should look into the slf4j facade these days, as it allows you to use {}-placeholders for the most concise statements. You can then use the appropriate logging framework behind slf4j to handle the actual treatment of your log statements. This could be log4j or the slf4j-simple which just prints out all of INFO, WARN and ERROR, and discards the rest.

    The crucial observation you need to make is that the WRITING of log statements is done when the code is written, and the DECISION of what is needed is done when the code is deployed, which may be years after the code was written and tested. System.out.println requires you to physically change your code to get rid of them, which is unacceptable in a rigid write-test-deploy cycle. IF the code changes, it must be retested. With slf4j you just enable those you want to see.

    We have full logging in the test phase, and rather verbose logging in the initial period of a production deployment, after which we go down to information only. This gives us full information in a scenario where debugging a case is very rarely possible.

    You might find this article I wrote interesting. The target audience is beginning Java programmers, with my intention of giving them good habits from the start. http://runjva.appspot.com/logging101/index.html

    0 讨论(0)
  • 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 );
    
    0 讨论(0)
  • 2021-02-13 19:46

    Levels, formatting, logging to multiple files... A logging framework (even if it's java.util.logging) is really beneficial if there's a chance anything may go wrong while your code is running.

    0 讨论(0)
提交回复
热议问题