Maven project configures dependencies in POM.xml
<! - log - >
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Copy the code
2, the SSM project defaults to write log4j.properties in the Resources directory without displaying the configuration file
3. If the log4j configuration file location and name have been changed and need to be loaded dynamically
The first method: configure in web.xmlCopy the code
<! Sprng loaded Log4j configuration file location -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<! Spring refreshes Log4j configuration file changes every 60 seconds (dynamically changing record levels and policies without restarting the application), in milliseconds.
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<! Log4j configuration listening This method is available in Spring 4.x versions, but has been marked as obsolete in Spring 4.2.1. The Log4jConfigListener must precede the Spring Listener if a version later than Spring 4.2.1 is used. -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Copy the code
The second way: Spring configuration bean class file loadingCopy the code
Second way: First, write Log4j configuration classes
public class Log4jConfig {
private boolean reload = true;
private int interval = 60000;
public Log4jConfig(boolean reload,int interval){
this.reload = reload;
this.interval = interval;
this.loadConfig();
}
public void loadConfig(a){
String log4jPath = Log4jConfig.class.getClassLoader().getResource("conf/log4j.properties").getPath();
// The system automatically reads the configuration file again every 60 seconds to check whether the file is modified
PropertyConfigurator.configureAndWatch(log4jPath, this.interval); }}Copy the code
Second way: Step 2, configure the bean in spring-context.xml
<! Log4j automatic log loading -->
<bean class="zp.com.ws.config.Log4jConfig">
<constructor-arg name="reload" value="true"/>
<constructor-arg name="interval" value="60000"/>
</bean>
Copy the code
4. Slf4j is used in combination
SLF4J(Simple Logging Facade for Java) just defines a set of logging interfaces, but does not provide any implementation. It allows you to use any logging library in the background. SLF4J makes the code independent of any particular logging API. Common Logging frameworks Log4J, LogBack, and java.util.logging. Slf4j is used in conjunction with the project.Copy the code
4.1. Maven project configures dependencies in POM.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
Copy the code
4.2 Use of distinctions in projects
private final static Logger logger = Logger.getLogger(PortalInitController.class); Change to: private final static Logger Logger. = LoggerFactory getLogger ((PortalInitController. Class)); Logger.info (" loginPageSet value loginPageSet() "+ Set); Change to logger.info(" loginPageSet value loginPageSet {} "+ Set);Copy the code