Log4j Log Output Format

Log4j Log Output Format

For me to start working, the only purpose of the output log is to print debugging information for the code. When there are any problems or errors with the project running, you can quickly find the source of the problem by looking at the log (which is also the problem I encountered at the beginning of my company).

Share a common log file (log4j.properties) that comes with the company framework first

log4j.rootLogger=DEBUG, stdout, filelog

//Destination is stdot, output address is console
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} %c{1} - %m%n

//Destination is filelog, output address is file (a new file is generated each day), output level is DEBUG,
//Log file location and log name
//Generate one file per day
//Display format of log information
//Log Information Output Format
log4j.appender.filelog.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} %c{1} - %m%n

1. Composition of Log4j

Loggers, Appenders, and Layout

1.1 Logger

Level limit on log information output

1.2 Appender

Specify where the log will be printed (console/file/generate new files based on days or file size)

1.3 Layout

Display format of log information (a regular log format helps us parse errors more easily)

2. Log level

There are six levels defined in Log4j, TRACE<DEBUG<INFO<WARN<ERROR<FATAL in turn.The log level can be specified in the logger type. When the log is output, only information at a higher level than the level specified in the configuration can be truly output.This reduces some unnecessary log output.

There are four main levels in the log:

  1. DEBUG

    Used at the fine-grained level, very helpful for debugging applications, mainly for printing some running information during development.

  2. INFO

    Used to print information that is interesting or important to the developer and can be used to output some important information about the program's operation in a production environment.

  3. WARN

    (Warning message) Indicates a potential error situation. Some information is not an error message, but the programmer should be given some hints.

  4. ERROR

    Used to indicate that an error event has occurred, but still does not affect the continued operation of the system.Print error and exception information, and use this level if you don't want to output too many logs;

When we set the corresponding level, the logging framework only calls methods greater than or equal to this level.Log4j recommends using only four levels:


3. Log4j Profile

3.1 File Format

log4j supports two profile formats (XML format / properties)

3.2 Properrties Format Configuration

  1. Logger

    log4j.rootLogger = [ level ] , appenderName1, appenderName2, ...

    Level: The level of log output, appenderName: Specifies the destination of the log output (here is not a specific location, just a property)

  2. Log Output Destination

    log4j.appender.appenderName = fully.qualified.name.of.appender.class

    There are five destinations to specify:

    • org.apache.log4j.ConsoleAppender (Console)
    • org.apache.log4j.FileAppender (file)
    • org.apache.log4j.DailyRollingFileAppender (produces one log file per day)
    • org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size)
    • org.apache.log4j.WriterAppender (sends log information in streaming format to any specified place)

    A brief introduction to the third output destination (DailyRollingFileAppender)

    When we specify the destination:


    It can also specify other configurations:

    • Generate a new log file interval:

      For example: log4j.appender.filelog.DatePattern='.'yyyy-MM-dd generates a new log file for each day;

      Of course, you can also specify hourly, weekly, monthly...

      1)'.'yyyy-MM: monthly
      2)'.'yyyy-ww: weekly 
      3)'.'yyyy-MM-dd: Every day
      4)'.'yyyy-MM-dd-a: Twice a day
      5)'.'yyyy-MM-dd-HH: Hourly
      6)'.'yyyy-MM-dd-HH-mm: per minute
  3. Output Information Format


    PatternLayout refers to flexibility in specifying layout patterns;

    log4j.appender.filelog.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} %c{1} - %m%n

    ConversionPattern s means that we output log information in this output format;

4. Practical application

  1. Import log4j dependencies

  2. Create log4j.properties configuration file in the resources directory

    log4j.rootLogger=DEBUG, stdout, filelog
    log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} %c{1} - %m%n
    log4j.appender.filelog.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} %c{1} - %m%n
  3. Create a log object to output log information where you want to log

    [External chain picture transfer failed, the source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-xw8Li00C-163070611) (C:\UsersLeejieDocumentsBlog articlelog4j\2451(1)].jpg)

    [External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-EE3hy8ih-163070613) (C:UsersLeejieDocumentsblog articlelog4j266;46(1)].jpg)

5. Notes

Overwriting the output log information is also a bad performance in your work, because the output log information will be output to the log file of the local disk, which will result in a large number of disk IO reads and writes, which will affect the performance of the server.It is also important to specify an appropriate level of log output, which only outputs important information to the log file.This also facilitates quick viewing of log records;Or log files will take up a lot of disk space over time. You should either clean up the log files in time for a period of time, or you can set the maximum memory for each file and save only the log files for a fixed period of time at a time.

Next article: Introduction to springboot multisource

Tags: log4j

Posted on Sat, 04 Sep 2021 12:32:19 -0400 by littleelmo