Home > Back-end >  How to specify log4j2 log info output to a file, the error output to a file?
How to specify log4j2 log info output to a file, the error output to a file?

Time:09-27

I tried a lot of online storage, it is not a separate store a level to a file, but the current level and above the level of storage,

As shown in the following:


My profile is as follows:

& lt; ? The XML version="1.0" encoding="utf-8"?>
& lt; ! - the level of logging and prioritization: OFF & gt; FATAL & gt; The ERROR & gt; WARN & gt; The INFO & gt; The DEBUG & gt; TRACE & gt; ALL - & gt;
& lt; ! Behind, the Configuration of the status, this is used to set the log4j2 within information output, can not set, when set to trace, you will see all detail inside the log4j2 output - & gt;
& lt; ! -- monitorInterval Log4j can automatically detect modify configuration files and reconfigure itself, set the interval of seconds - & gt;
& lt; The configuration status="WARN" monitorInterval="30" & gt;
& lt; ! - to define all appenders - & gt;
& lt; Appenders>
& lt; ! - the output console configuration - & gt;
& lt; The console name="console" target="SYSTEM_OUT & gt;"
& lt; ! - the format of the output log - & gt;
& lt; PatternLayout pattern="[% d {HH: mm: ss: SSS}] [p] % % l - % m % n"/& gt;
& lt;/console>
& lt; ! - the file will print out all the information, the log every time to run the program will automatically to empty, is determined by append attributes, this also pretty useful, suitable for temporary test - & gt;
& lt; The File name="log" fileName="log/test. The log" append="false" & gt;
& lt; PatternLayout pattern="% d {HH: mm: ss. The SSS} % % 5 level class {36} xEx MSG % % M - L % % % n"/& gt;
& lt;/File>
& lt; ! - this will print out all the info and the level of information, each size than the size, the size of the size of the log will automatically deposit by month - year established under the folder and compression, as archive - & gt;
& lt; RollingFile name="RollingFileInfo" fileName="log/info. The log"
FilePattern="log/$${date: yyyy - MM}/info - % d - the dd} {yyyy - MM - % i.l og" & gt;
& lt; ! -- the console output level and above the level of information (onMatch), other direct refused (onMismatch) - & gt;
& lt; ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/& gt;
& lt; PatternLayout pattern="[% d {HH: mm: ss: SSS}] [p] % % l - % m % n"/& gt;
& lt; Policies>
& lt; TimeBasedTriggeringPolicy/& gt;
& lt; SizeBasedTriggeringPolicy size="100 MB/& gt;"
& lt;/Policies>
& lt;/RollingFile>
& lt; RollingFile name="RollingFileWarn fileName"="the log/warn the log"
FilePattern="log/$${date: yyyy - MM}/warn - % d - the dd} {yyyy - MM - % i.l og" & gt;
& lt; ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/& gt;
& lt; PatternLayout pattern="[% d {HH: mm: ss: SSS}] [p] % % l - % m % n"/& gt;
& lt; Policies>
& lt; TimeBasedTriggeringPolicy/& gt;
& lt; SizeBasedTriggeringPolicy size="100 MB/& gt;"
& lt;/Policies>
& lt; ! - DefaultRolloverStrategy properties such as is not set, the default for up to seven files under the same folder, here set up 20 - & gt;
& lt; DefaultRolloverStrategy Max="20"/& gt;
& lt;/RollingFile>
& lt; RollingFile name="RollingFileError fileName"="the log/error log"
FilePattern="log/$${date: yyyy - MM}/error - % d - the dd} {yyyy - MM - % i.l og" & gt;
& lt; ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/& gt;
& lt; PatternLayout pattern="[% d {HH: mm: ss: SSS}] [p] % % l - % m % n"/& gt;
& lt; Policies>
& lt; TimeBasedTriggeringPolicy/& gt;
& lt; SizeBasedTriggeringPolicy size="100 MB/& gt;"
& lt;/Policies>
& lt;/RollingFile>
& lt;/appenders>
& lt; ! Only defines the logger - and then define the logger, and introduce the appender, appender will only take effect - & gt;
& lt; Loggers>
& lt; ! - to filter out spring and mybatis useless DEBUG information - & gt;
& lt; The logger name="org. Springframework" level="INFO" & gt; & lt;/logger>
& lt; The logger name="org. Mybatis" level="INFO" & gt; & lt;/logger>
& lt; Root level="all" & gt;
& lt; Appender - ref ref="Console"/& gt;
& lt; Appender - ref ref="RollingFileInfo"/& gt;
& lt; Appender - ref ref="RollingFileWarn"/& gt;
& lt; Appender - ref ref="RollingFileError"/& gt;
& lt;/root>
& lt;/loggers>
& lt;/configuration>


I want to achieve a level, a file error only error, only the info inside info, how can I achieve? Who can help me ~

CodePudding user response:

 & lt; ? The XML version="1.0" encoding="utf-8"?> 
& lt; ! - the level of logging and prioritization: OFF & gt; FATAL & gt; The ERROR & gt; WARN & gt; The INFO & gt; The DEBUG & gt; TRACE & gt; ALL - & gt;
& lt; ! Behind, the Configuration of the status, this is used to set the log4j2 within information output, can not set, when set to trace, you will see all detail inside the log4j2 output - & gt;
& lt; ! -- monitorInterval Log4j can automatically detect modify configuration files and reconfigure itself, set the interval of seconds - & gt;
& lt; The configuration status="WARN" monitorInterval="30" & gt;
& lt; ! - to define all appenders - & gt;
& lt; Appenders>
& lt; ! - the output console configuration - & gt;
& lt; The console name="console" target="SYSTEM_OUT & gt;"
& lt; ! - the format of the output log - & gt;
& lt; PatternLayout pattern="[% d {HH: mm: ss: SSS}] [p] % % l - % m % n"/& gt;
& lt;/console>
& lt; ! - the file will print out all the information, the log every time to run the program will automatically to empty, is determined by append attributes, this also pretty useful, suitable for temporary test - & gt;
& lt; The File name="log" fileName="log/test. The log" append="false" & gt;
& lt; PatternLayout pattern="% d {HH: mm: ss. The SSS} % % 5 level class {36} xEx MSG % % M - L % % % n"/& gt;
& lt;/File>
& lt; ! - this will print out all the info and the level of information, each size than the size, the size of the size of the log will automatically deposit by month - year established under the folder and compression, as archive - & gt;
& lt; RollingFile name="RollingFileInfo" fileName="log/info. The log"
FilePattern="log/$${date: yyyy - MM}/info - % d - the dd} {yyyy - MM - % i.l og" & gt;
& lt; ! -- the console output level and above the level of information (onMatch), other direct refused (onMismatch) - & gt;
& lt; ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/& gt;
& lt; PatternLayout pattern="[% d {HH: mm: ss: SSS}] [p] % % l - % m % n"/& gt;
& lt; Policies>
& lt; TimeBasedTriggeringPolicy/& gt;
& lt; SizeBasedTriggeringPolicy size="100 MB/& gt;"
& lt;/Policies>
& lt;/RollingFile>
& lt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related