Log4j: How to configure simplest possible file logging?

后端 未结 4 822
无人及你
无人及你 2020-11-29 20:21

My story:

I want to make a thing which is as simple as a simplest possible log4j logger that logs rows to a file. I have found several examples with some fun

4条回答
  •  借酒劲吻你
    2020-11-29 21:01

    Here's a simple one that I often use:

    # Set up logging to include a file record of the output
    # Note: the file is always created, even if there is 
    # no actual output.
    log4j.rootLogger=error, stdout, R
    
    # Log format to standard out
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=   %5p\t[%d] [%t] (%F:%L)\n     \t%m%n\n
    
    # File based log output
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=owls_conditions.log
    log4j.appender.R.MaxFileSize=10000KB
    # Keep one backup file
    log4j.appender.R.MaxBackupIndex=1
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=   %5p\t[%d] [%t] (%F:%L)\n     \t%m%n\n
    

    The format of the log is as follows:

    ERROR   [2009-09-13 09:56:01,760] [main] (RDFDefaultErrorHandler.java:44)
            http://www.xfront.com/owl/ontologies/camera/#(line 1 column 1): Content is not allowed in prolog.
    

    Such a format is defined by the string %5p\t[%d] [%t] (%F:%L)\n \t%m%n\n. You can read the meaning of conversion characters in log4j javadoc for PatternLayout.

    Included comments should help in understanding what it does. Further notes:

    • it logs both to console and to file; in this case the file is named owls_conditions.log: change it according to your needs;
    • files are rotated when they reach 10000KB, and one back-up file is kept

提交回复
热议问题