First, we define a path that can dynamically find the project at runtime. The reason for this is to configure the log4j output file path as you wish.
<context-param> <param-name>webAppRootKey</param-name> <param-value>amt.root</param-value></context-param>
Then you need to define the path of the project log configuration file and the log4j listener
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:conf/log4j.xml</param-value> </context-param> <!--Define LOG4J Listener--> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
In this way, the configuration of the web.xml file is completed, and then the specific configuration file of the log configuration file:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration> <!-- Output log information to the console --> <appender name="ConsoleAppender"> <!-- Set the style of log output --> <layout> <!-- Set the format of log output --> <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n" /> </layout> <!-- Set the output level of filters--> <filter> <!-- Set the minimum level of log output --> <param name="levelMin" value="INFO" /> <!-- Set the maximum level of log output --> <param name="levelMax" value="ERROR" /> <!-- Set the xxx of log output, default is false --> <param name="AcceptOnMatch" value="true" /> </filter> </appender> <!-- Export log information to a file, and configure how long it takes to generate a new log information file --> <appender name="rollingFile"> <!-- Set the full path name of the log information output file --> <param name="File" value="${amt.root}/logs/spdbData.log" /> <!-- Set the log to rollback once an hour, which will generate a new log file --> <param name="DatePattern" value="'_'yyyy-MM-dd-HH'.log'" /> <!-- Set the style of log output --> <layout> <!-- Set the format of log output --> <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss:SS}] [%-5p] [method:%l]%n%m%n%n" /> </layout> </appender> <!-- Note: 1: When additionity="false", the configuration in root fails, and the default inheritance mechanism is not followed. 2: The name in the logger is very important. It represents the form of the logger package and has a certain inclusion relationship. Experiments show that 2-1: When the name of the defined logger has the same name, only the last one can correctly print the log 2-2: When the corresponding logger contains the inclusion relationship, for example: name=test.log4j.test8 and name=test.log4j.test8.UseLog4j, then the situation of 2-1 is the same 2-3: The name of the logger means that all the loggers containing this name follow the same configuration. The inclusion relationship in the value of the name refers to the name of the logger! Pay attention! 3: Intersection between the level defined in the logger and the level defined in the filter in the appender 4: If the levelMin > levelMax defined in the appender is defined in the appender, the log information cannot be printed-> <!--Specify the logger setting, the addition indicates whether the default inheritance mechanism is followed-> <logger name="test.log4j.test8.UseLog4j_" addition="false"> <level value ="ERROR"/> <appender-ref ref="rollingFile"/> </logger> <!-- Settings of the root logger --> <root> <level value ="info"/> <appender-ref ref="ConsoleAppender"/> <appender-ref ref="rollingFile"/> </root></log4j:configuration>The project structure is as follows:
After everything is ready, once the project is started, you can find your daily or hourly log files at the specified output location. The log configuration file can also expand the size of each file and rollback matters when a file error occurs. I will not go into details here.
The above article is based on the web project log to specify the output file location configuration method. I hope it can give you a reference and I hope you can support Wulin.com more.