For the basic content of the configuration file, please refer to other articles. This article explains a specific file, and those who need it can refer to study
Log4j2. XML file
This code can be copied directly to a file to see that the format is correct
<? The XML version = "1.0" encoding = "utf-8"? > <! -- Log level All < Trace < Debug < Info < Warn < Error < Fatal < OFF- > <! --> <Configuration Status ="WARN"> <Configuration Status ="WARN"> <Properties> <! -- Property defines a variable that applies to the entire configuration --> <! <Property name="gatherPath">/log/TestPath</Property> </Properties> <Appenders> <! --> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout charset="UTF-8" pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t][%X{logId}] %-5level %logger{3} - %msg%n}"/> </Console> <! -- indicates that this node is an asynchronous node, <Async name="Async"> <AppenderRef ref="Console"/> </Async> <! --filePattern is the output format of the file name that determines the hour to generate a log file --> <! -- fileName devicelog. log specifies that the file is written to every time a log is written to the file, and the time is set by CronTriggeringPolicy --> <! Generate a new file with the contents of the file following the pattern set by PatternLayout and then empty devicelog.log and wait for subsequent writes. This is the file scrolling update --> <! --> <RollingRandomAccessFile name="needGatherDeviceLog" fileName="${gatherPath}/deviceLog.log" filePattern="${gatherPath}/deviceLog.log.%d{yyyyMMddHHmm}"> <! PatternLayout pattern="%d{MM-DD HH: MM: ss.sss} [%t][%X{logId}] %-5level %logger{1}-% MSG %n" charset="utf-8"/> <! --CronTriggeringPolicy specifies that logs printed within this time range will be written to the input file at the end of the time range --> <! -- The logs of the next time period will overwrite the logs of the previous time period --> <! If you want to generate a log file in an hour, you should set it to an hour --> <! --> <CronTriggeringPolicy schedule="0 * * * *?" / > <! Logs that meet this rule are automatically deleted --> <! ${gatherDeviceLogPath} jarvis is invalid when deployed online and must use absolute path --> <DefaultRolloverStrategy> <Delete basePath="${gatherPath}" maxDepth="1"> <IfFileName glob="deviceLog.log.*"/> <IfLastModified age="30d"/> </Delete> </DefaultRolloverStrategy> </RollingRandomAccessFile> <! <AppenderRef ref="needGatherDeviceLog"/> </Async> </Appenders> <Loggers> <! -- Without specifying which Logger to use, root will be used for output --> <! <Root level="INFO"> <AppenderRef ref="Async"/> </Root> <! <Logger name="org.hibernate.SQL" level="debug"></Logger> <! --> <Logger name="DEVICE_LOG_LOGGER" additivity="false" level="INFO"> <AppenderRef ref="gatherDeviceLogAsync" /> </Logger> </Loggers> </Configuration>Copy the code
Different usage of two nodes
The root node
By default, the root node outputs logs to the console according to the configuration of the node
Private static final Logger Logger = loggerFactory.getLogger (class name.class); Logger. info(" this will now be displayed on the console ");Copy the code
Custom node
Class with the specified string
private static final Logger OTHER_LOGGER = LoggerFactory.getLogger("DEVICE_LOG_LOGGER"); Other_logger. info(" This printed content is generated in the devicelog.log file according to the configuration ")Copy the code
Custom nodes can be used in projects to save some data to files, and the implementation of log4j2 itself is much more efficient than writing TXT yourself. At the same time there are a variety of file rolling mechanism, it is very convenient to use, basic can meet the needs of business