Log4j provides a detailed introduction to common configuration instructions

  • 2020-04-01 01:10:10
  • OfStack

Many diaosi think log4j is not good to use, because it does not output to the IDE console, haha, in fact, is the fault of the configuration, below see how I step by step configuration to the console, log4j output level and output mode I believe diaosi know, here is not to say more, directly look at the code comments:

# All, TRACE, DEBUG, INFO, WARN, ERROR, FITAL, OFF
Global project log configuration
Log4j. RootLogger = ERROR
All logs under the specified package (output only logs under the project com package)
Log4j.logger.com = DEBUG, Yin, dailly_rolling_file
Specifies that the output level is not globally managed
Log4j.additivity.com = false

Output to the console
Log4j. Appender. Yin = org. Apache. Log4j. ConsoleAppender
Log4j. Appender. Yin) layout = org). Apache log4j. PatternLayout
Log4j. Appender. Yin. Layout. ConversionPattern = % n % d % [p] [m] % % % [c] m [L] % % n ()

Output to file
Log4j. Appender. The file = org. Apache. Log4j. FileAppender
. Log4j appenders. File. The file \ = C: \ \ logs \ \ log. The log
Log4j. Appenile. File. Append = true
Log4j. Appender. File. Layout = org.. Apache log4j. PatternLayout
Log4j. Appender. File. Layout. ConversionPattern - d = % {MM: yyyy - MM - dd HH \ \ : ss, SSS} % [c] - [p] % % m % n

Output to size scroll file (100 indicates number of backup files)
Log4j. Appender. Rolling_file = org. Apache. Log4j. RollingFileAppender
Log4j. Appender. Rolling_file. Threshold = ERROR
. Log4j appenders. Rolling_file. File \ = C: \ \ logs \ \ log_rolling log
Log4j. Appenile. Rolling_file. Append = true www.jb51.net
Log4j. Appenile. Rolling_file. MaxFileSize = 10 KB
Log4j. Appenile. Rolling_file MaxBackupIndex = 100
Log4j. Appender. Rolling_file. Layout = org.. Apache log4j. PatternLayout
Log4j. Appender. Rolling_file. Layout. ConversionPattern - d = % {MM: yyyy - MM - dd HH \ \ : ss, SSS} % [c] - [p] % % m % n

Output to a file that scrolls by date
Log4j. Appender. Dailly_rolling_file = org. Apache. Log4j. DailyRollingFileAppender
. Log4j appenders. Dailly_rolling_file. File \ = C: \ \ logs \ \ log_daily_rolling log
Log4j. Appender. Dailly_rolling_file. DatePattern =. - dd yyyy - MM
Log4j. Appender. Dailly_rolling_file. Layout = org.. Apache log4j. PatternLayout
Log4j. Appender. Dailly_rolling_file. Layout. ConversionPattern - d = % {MM: yyyy - MM - dd HH \ \ : ss, SSS} % [c] - [p] % % m % n

Related articles: