Work, we often want to keep the system running log, we check the system for operation purposes, but because
Debug
the log volume level too we do not want to retain, can be developed, we want to go to see theDebug
level of the log. So the question is, in addition to writing two sets of configuration files or modify configuration files when you deploy, there are other ways to solve this problem at the same time it?
The answer is: Of course. Honestly, bloggers do not quite understandlog4j
the detailed configuration that is the meaning, but this does not prevent the recording of today's article. Lecturing ~!
# ------------------- 1 -------------------
log4j.rootLogger = debug,info,warn,error,stdout
# ------------------- 2 -------------------
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%nmethod:%l%n%m%n
# ------------------- 3 -------------------
log4j.appender.info = org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File = E:/logFile/log_info.log
log4j.appender.info.Append = true
log4j.appender.info.Threshold = INFO
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern = [ %p ] %d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - %m%n
# ------------------- 4 -------------------
log4j.appender.warn = org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.File = E:/logFile/log_warn.log
log4j.appender.warn.Append = true
log4j.appender.warn.Threshold = WARN
log4j.appender.warn.layout = org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern = [ %p ] %d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - %m%n
# ------------------- 5 -------------------
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = E:/logFile/log_error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = [ %p ] %d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - %m%n
The first part of the Detailed
This section is to define the level of logging to record default there are eight levels from low to high as follows: ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<OFF
.
ALL
It includes all levels, including Custom Level.TRACE
Lower than DEBUG event information is generally not available.DEBUG
Debug level, mainly for the development process run print some information for debugging applications are very helpful.INFO
Message on the coarse-grained level to highlight the process of running the application. Print some of your interesting or important information, this information may be used in some important production environments running output, but not abuse, avoid printing too many logs.WARN
The output potential of the case can go wrong, which is the output of a warning message.ERROR
Output error occurred does not affect the information system continues to run, you do not want too many log output, you can use this level.FATAL
Output serious errors that will cause the system to terminate.OFF
The highest level for close all logging.
I can see from the code, in the first part I define debug,info,warn,error
the four default levels and a stdout
custom level. As a result, we just put a custom level stdout
is set to the contents printed to the console, but will info,warn,error
be set to save the contents to a log file, console output encoding can be realized debug
generation after logging level deployment run info,warn,error
level logging. (Development environment will generate info,warn,error
the level of the log file)
The second part of the Detailed
The output to the console log Custom Level. eg:
[ERROR] 2019-06-14 12:02:16,002 method:com.process.FlightTrainTask.taskCycle(FlightTrainTask.java:24)
Detailed remaining portion
In addition to generating the remaining part of the contents of the different log file name and log level, the rest are projects. Therefore, the following explanation is an example of the third portion.
log4j.appender.info
This attribute defines the log file generated in the form of a day.
log4j.appender.info.File
This attribute defines the path and file name of the generated log file.
log4j.appender.info.Append
This attribute defines the new log information has been incrementally added to the existing log file.
log4j.appender.info.Threshold
This attribute defines the log level to be generated.
log4j.appender.info.layout
This attribute defines the log format as a custom format.
log4j.appender.info.layout.ConversionPattern
This attribute defines the log format.
Learn more log4j
using the configuration method: https://blog.csdn.net/dyz4001/article/details/81103863