log4j”
- Introduction of log4j.jar package
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- Configure log4j.properties
log4j.rootLogger=INFO,Console,OneFile,errFile
#Output to consoleLog4j.appender.Console=org.apache.log4j.ConsoleAppenderLog4j.appender.Console.encoding=utf-8Log4j.appender.Console.Target=System.outLog4j.appender.Console.layout=org.apache.log4j.PatternLayOutLog4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\: mm\: SS, SSS}][%c]%m%nOutputTo file level infoLog4j.appender.OneFile=org.apache.log4j.RollingFileAppenderLog4j.appender.OneFile.encodIng=utf-8Log4j.appender.OneFile.File=../logs/test-map/info.logLog4j.appender.OneFile.MaxFileSize=10MBLog4j.appender.OneFile.MaxBackupIndex=50Log4j.appender.OneFile.Threshold=INFO only produces more than INFO level.Journal!!!Log4j.appender.OneFile.layout=org.apache.log4j.PatternLayoutLog4j.appender.OneFile.layout.ConVersionPattern=[%p][%d{yyyy-MM-dd HH\: mm\: SS, SSS}][%c]%m%nOutput to file level errorLog4j.appender.errFile=orG.apache.log4j.RollingFileAppenderLog4j.appender.errFile.encoding=utf-8Log4j.appender.errFile.FiLe=../logs/test-map/error.logLog4j.appender.errFile.MaxFileSize=10MBLog4j.appender.errFile.MaxBackUpIndex=50Log4j.appender.errFile.Threshold=ERROR only ERROR logs above!Log4j.appender.errFile.layOut=org.apache.log4j.PatternLayoutLog4j.appender.errFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dD HH\: mm\: SS, SSS}][%c]%m%n
- Usage method
private static final Logger LOGGER = Logger.getLogger(Test.class);
LOGGER.info("message");
slf4j”
- Introducing slf4j.jar
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
-
Configure log4j.properties (through log4j)
-
Usage method
slf4jThe biggest difference from log4j is that slf4j can use placeholders, and the advantage is that string injection can be prevented.
private static final Logger logger = LoggerFactory.getLogger(Test.class);
slf4j integration log4j”
Why integration? I don’t know.
- Introduce Maven dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
note:If you don’t need Maven dependency, you need to import three jar packages log4j.jar, slf4j.jar and slf4j-log4j.jar.
-
Configure log4j.properties (ibid.)
-
You can use log4j, or you can use slf4j.
log4j.properties log content resolution”
- Enter log level level
log4j.rootLogger=INFO,Console,OneFile,errFile
Info is the total output log level; the latter is the identification of the output location.
The log level is five levels, the lower the downgrade is:
FATAL 0
ERROR 3 Serious errors are mainly procedural errors.WARN 4 is a general warning, such as session loss.INFO 6 is a general display of information, such as logout and logout.DEBUG 7 debug for programsinformation
- Location of output log appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
The destination of log output is as follows:
org.apache.log4j.ConsoleAppender(Console)Org.apache.log4j.FileAppender (file)Org.apache.log4j.DailyRollingFileAppender (produces a log file every day).Org.Apache. log4j. Rolling File AppenderOrg.apache.log4j.WriterAppender sends log information in stream format.To any designated place)
- The format of output log is layout.
-
The default is placed under the classpath path, that is, under src/main/resources.
- log4j.propertiesPath change or name change
Need to be specified in web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath*:log4j.properties</param-value>
</context-param>
web container startup loading sequence”:
Citing blog country love https://www.cnblogs.com/jiaguozhilian/p/5819032.html
-
When you start a Web project, the container (such as Tomcat) reads its configuration file web. xml. Reads two nodes: and
-
Next, the container creates a ServletContext (context) that all parts of the Web project will share.
-
The container will be transformed into a key value pair and handed to ServletContext..
-
Class instances in container creation, that is, create monitoring.
-
In the listener, there is a contextInitialized (ServletContextEvent args) initialization method, in which the ServletContext = ServletContextEvent is obtained.getServletContext ();
context-paramThe value = ServletContext.getInitParameter (“context-param’s key”); -
After you get the context-param value, you can do something. Note that your Web project is not fully started at this point. This action will be earlier than all the servlets. In other words, at this point, you are right.
-
For example, you may want to open the database before the project starts.
So here it is. -
This listener is a class that you write yourself. In addition to initializing methods, it also has destroy methods. It is used to release resources before closing an application, such as closing a database connection.
- Example 1:
<!-- Load spring configuration file -->< context-param>< param-name> contextConfigLocation< /param-name≫< param-value> /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml, /WEB-INF/jason-servlet.xml< /param-value>< /context-param>< listener>< listener-class> ORG.springframework.web.context.ContextLoaderListener< /listener-class>< /listener>
- Example 2: customize context-param and customize listener to get this information.
<context-param> <param-name>urlrewrite</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>cluster</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>servletmapping</param-name> <param-value>*.bbscs</param-value> </context-param> <context-param> <param-name>poststoragemode</param-name> <param-value>1</param-value> </context-param> <listener> <listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class> </listener> public class SysListener extends HttpServlet implements ServletContextListener { private static final Log logger = LogFactory.getLog(SysListener.class); public void contextDestroyed(ServletContextEvent sce) { //Used for operation when containers are closed.}/ / used to operate when containers are opened.Public void contextInitialized (ServletContextEvent SCE) {String rootpaTh = sce.getServletContext ().GetRealPath ("/");System.out.println ("-------------rootPath:" +rootpath));If (rootpath = null) {Rootpath = rootpath.replaceAll (",", "/");} else {Rootpath = "/";}If (rootpath.endsWith ("/")) {Rootpath = rootpath + "/";}Constant.ROOTPATH = rootpath;Logger.info ("Application Run Path:" + rootpath);String urlrewrtie = sce.getServletContext ().GetINitParameter ("UrlRewrite");Boolean burlrewrtie = false;If (urlrewrtie = null) {Burlrewrtie =Boolean.parseBoolean (urlrewrtie);}Constant.USE_URL_REWRITE = burlrewrtie;Logger.info ("Use Url"Rewrite: "+ burlrewrtie";Others are slightly...}}* * final output-------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\2009-06-09 21:51:46526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Application Run PatH:D:/tomcat_bbs/webapps/BBSCS_8_0_3/2009-06-09 21:51:46526 [com.laoer.bbscs.web.servlet.SysListenEr]-[INFO]Use Urlrewrite:true2009-06-09 21:51:46526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Cluster:false2009-06-09 21:51:46526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]SERVLET MAPPING:*.bbscs2009-06-09 21:51:46573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]PostStorage Mode:1* * /
context-param” and “init-param”
web.xmlTwo parameters can be defined inside:
- applicationThe parameters in the scope are stored in ServletContext and configured in web.xml as follows:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
- servletThe parameters in the range can only be obtained in the init () method of servlet, and are configured in web.xml as follows:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
In servlet, you can use code separately:
package com.wes.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class MainServlet extends HttpServlet ...{
public MainServlet() ...{
super();
}
public void init() throws ServletException ...{
System.out.println("The next two parameter Param1 is stored in servlet.System.out.println (this.getInitParameter ("Param1"));System.out.println ("the following parameters are stored in ServletContext");System.out.println (getServletContext ().Get)InitParameter ("context/param"));}}
The first parameter is available in the servlet through getServletContext (). getInitParameter (“context / param”).
The second parameter can only be obtained by using this.getInitParameter (“Param1”) in the init () method of servlet.