PHP to Java for some time, recently in learning some of spring's components installation, configuration and use. I learned log4j today. As a project log operations component, it saves a lot of effort to log and log transport storage during the project process.
LOG4J is an Apache open source project that uses log4j to control log information delivery destinations for consoles, files, databases, GUI components, even socket servers, NT Event loggers, UNIX Syslog daemons, and more ; Can control the information content and information output format of each log; By defining the level of each log information, we are able to control the log generation process more carefully, even if you do not need to modify the business logic code, restart the Web service, only through a modified configuration file can be implemented to control the log action of the project.
First, the log levels are: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, all, and so on.
Off: Closed log information FATAL: A critical event error that could cause the app to abort: Critical error is mainly a program error WARN: General warning, such as session missing info: General information to display, such as login log out debug: Debug information of the program TRACE: More granular event information than debug all: Open all levels of log
LOG4J provides a range of appender that allow logs to be transported to different places, such as consoles, files, databases, etc.:
- Org.apache.log4j.ConsoleAppender (console)
- Org.apache.log4j.FileAppender (file)
- Org.apache.log4j.jdbc.JDBCAppender (Database)
- Org.apache.log4j.net.SMTPAppender (mail)
- Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
- Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size)
- Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
Combined with the log level, you can send different levels of log information to a different location as a record or as a basis for finding problems afterwards. General information can be stored in the form of files or databases, error messages can be sent to the relevant responsible person or SMS to the relevant responsible person, these in log4j seems easy to fix. At the moment, it has been felt that Java has made it easy for PHP to take a lot of work to fix.
For log4j configuration parameters, please refer to the information on the Web, the following is a brief explanation of how to integrate the log4j as a log operating component in the spring framework to serve the project:
1. Pom.xml introduces the Log4j component dependency package:
1 <Dependency>2 <groupId>Log4j</groupId>3 <Artifactid>Log4j</Artifactid>4 <version>1.2.15</version>5 <Scope>Runtime</Scope>6 </Dependency>
2. Web. XML When you configure the servlet container to start, the log4j configuration is automatically loaded and the components are injected:
1 <Context-param>2 <Param-name>Log4jconfiglocation</Param-name>3 <Param-value>Classpath:conf/log4j.properties</Param-value>4 </Context-param>5 <Context-param>6 <Param-name>Log4jrefreshinterval</Param-name>7 <Param-value>5000</Param-value>8 </Context-param>9 <Listener>Ten <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class> One </Listener>
Here, I put the log4j.properties configuration file into the Conf directory of Classpath and start a watchdog thread to scan the configuration file every 5 seconds, readjust the log policy, implement the change of the log policy without adjusting the logic code. The policy for logging is configured in detail through Log4j.properties, as follows:
# +======================================================================+ #log4j. Rootlogger=${log4j.log.level}, ${log4j.log.target}log4j.addivity.org.apache=true# +=========================================================== ===========+## | [Target]-console# +----------------------------------------------------------------------+# Log4j.appender.console=org.apache.log4j.consoleappenderlog4j.appender.console.threshold=${log4j.log.level} log4j.appender.console.encoding=${log4j.log.encoding}log4j.appender.console.target= system.outlog4j.appender.console.layout=${log4j.log.layout}log4j.appender.console.layout.conversionpattern=${ log4j.log.layout.pattern}# +======================================================================+## | [Target]-file# +----------------------------------------------------------------------+ #log4j. appender.file= Org.apache.log4j.rollingfileappenderlog4j.appender.file.threshold=${log4j.log.level} log4j.appender.file.encoding=${log4j.log.encoding}log4j.appender.file.fiLe=${log4j.log.dir}/runtime.loglog4j.appender.file.append=truelog4j.appender.file.maxfilesize= 2048kblog4j.appender.file.maxbackupindex=10log4j.appender.file.layout=${log4j.log.layout} log4j.appender.file.layout.conversionpattern=${log4j.log.layout.pattern}# +==================================== ==================================+## | [Target]-database# +----------------------------------------------------------------------+# Log4j.appender.database=org.apache.log4j.jdbc.jdbcappenderlog4j.appender.database.threshold= Errorlog4j.appender.database.url=jdbc:mysql://127.0.0.1:3306/springlog4j.appender.database.driver= com.mysql.jdbc.driverlog4j.appender.database.user=rootlog4j.appender.database.password= Liuriqilog4j.appender.database.layout=${log4j.log.layout}log4j.appender.database.sql=insert into tv_log4j ( Message) VALUES (' ${log4j.log.layout.pattern} ') # +=============================================================== =======+## | [Target]-email# +----------------------------------------------------------------------+ #log4j. Appender.email=org.apache.log4j.net.smtpappenderlog4j.appender.email.threshold =fatallog4j.appender.email.buffersize=10[email protected]log4j.appender.email.smtphost= Localhostlog4j.appender.email.subject=log4j message[email protected]log4j.appender.email.layout=${ log4j.log.layout}log4j.appender.email.layout.conversionpattern=${log4j.log.layout.pattern}# +================== ====================================================+ #上面配置相关的变量, I extracted it into the variable configuration file inside, as follows: # +======================= ===============================================+## | log4j config# +----------------------------------------------------------------------+ #log4j. log.dir=logs/# Log4j.log.level=all,trace,debug,info,warn,error,fatal,offlog4j.log.level=debug#log4j.log.target=console,file, database,email,socketlog4j.log.target=console,filelog4j.log.encoding=utf-8log4j.log.layout= org.apache.log4j.patternlayoutlog4j.log.layout.pattern=[%d%r] [%-5p] [%t] [%l] [%m]%n# +======================================================================+#
With the above configuration, the debug log information is sent to the console and the file log when the project starts.
Finally, you can use Log4j's own debug (), info (), warn (), error () method in your application to record the operations you want to record, as for how to store the logs and logs sent to the destination log4j.
Last point: Configuring a lower-level error logging policy logs advanced error messages, and configuring advanced error logging policies ignores low-level error messages. If the level of the error log is configured as debug, the log4j component records all levels of logs that contain debug, such as Debug, info, warn, error, fatal, and so on. When you configure the error level, log information such as debug, info, warn, and so on, is not logged at the following level of error.
Spring Consolidates log4j log components