Preface
As the project size becomes larger and larger, new modules will be introduced continuously. Different modules will print their own logs, which will eventually lead to the log being unable to be viewed. For example, in my own project, the following logs exist:
Among them, the amount of log data of the message log and background thread is very large. If all logs are printed in one file and using tail -f log.log file, you will find that the logs are scrolling quickly, and you cannot view or even locate a specific SQL or Service access log.
The solution is to classify and output different logs so that the mutual logs do not affect each other. The access to logs by particularly important interfaces can easily locate and troubleshoot problems.
Step 1: Configure in log4j.properties
First post all my own log4j.properties configurations:
log4j.rootLogger=INFO, console, file log4j.appender.console=net.czt.log.AsyncConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d [%t] %-5p crazyant-web %-17c{2} (%13F:%L) %X{USER_ID}|%X{USER_IP}|%X{SERVER_ADDRESS}|%X{SERVER_NAME}|%X{REQUEST_URI}|%X{SESSION_ID} - %m%nlog4j.appender.console.bufferSize=10000log4j.appender.console.encoding=UTF-8 log4j.appender.file=org.apache.log4j.RollingFileAppenderlog4j.appender.file.file=/home/work/apache-tomcat-6.0.39/logs/crazyant.loglog4j.appender.file.MaxBackupIndex=5log4j.appender.file.MaxFileSize=1GBlog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=[%-5p] crazyant-web %d{yyyy-MM-dd HH:mm:ss,SSS} %X{USER_ID}|%X{USER_IP}|%X{SERVER_ADDRESS}|%X{SERVER_NAME}|%X{REQUEST_URI}|%X{SESSION_ID} method:%l%n%m%nlog4j.appender.file.bufferSize=10000log4j.appender.file.encoding=UTF-8 log4j.logger.net.czt.crazyant.msg=DEBUG, messagelog4j.additivity.net.czt.crazyant.msg=falselog4j.appender.message=org.apache.log4j.RollingFileAppenderlog4j.appender.message.File=/home/work/apache-tomcat-6.0.39/logs/crazyant_message.loglog4j.appender.mess age.Append=truelog4j.appender.message.MaxFileSize=1GBlog4j.appender.message.MaxBackupIndex=5log4j.appender.message.layout=org.apache.log4j.PatternLayoutlog4j.appender.message.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p][%c{1}] [%t] - %m%nlog4j.appender.message.encoding=UTF-8 log4j.logger.net.czt.crazyant.async.service=DEBUG, asynclog4j.additivity.net.czt.crazyant.async.service=falselog4j.appender.async=org.apache.log4j.RollingFileAppenderlog4j.appender.async.File=/home/work/apache-tomcat-6.0.39/logs/crazyant_async.loglog4j.appende r.async.Append=truelog4j.appender.async.MaxFileSize=1GBlog4j.appender.async.MaxBackupIndex=5log4j.appender.async.layout=org.apache.log4j.PatternLayoutlog4j.appender.async.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p][%c{1}] [%t] - %m%nlog4j.appender.async.encoding=UTF-8 log4j.logger.net.czt.orm.mybatis.SqlMonitorManager=DEBUG, showsqllog4j.additivity.net.czt.orm.mybatis.SqlMonitorManager=falselog4j.logger.net.czt.transaction.interceptor.SmartTransactionInterceptor=DEBUG, showsqllog4j.additivity.net.czt.transaction.interceptor.SmartTransactionInterceptor=falselog4j.appender.showsql=org.apache.log4j.RollingFileAppenderlog4j.appender.showsql.File=/home/work/apache-tomcat-6.0.39/logs/crazyant_sql.logl og4j.appender.showsql.Append=truelog4j.appender.showsql.MaxFileSize=1GBlog4j.appender.showsql.MaxBackupIndex=5log4j.appender.showsql.layout=org.apache.log4j.PatternLayoutlog4j.appender.showsql.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p][%c{1}] [%t] - %m%nlog4j.appender.showsql.encoding=UTF-8 log4j.logger.net.czt.crazyant.service=DEBUG, servicelog4j.additivity.net.czt.crazyant.service=falselog4j.appender.service=org.apache.log4j.RollingFileAppenderlog4j.appender.service.File=/home/work/apache-tomcat-6.0.39/logs/crazyant_service.loglog4j.appender.se rvice.Append=truelog4j.appender.service.MaxFileSize=1GBlog4j.appender.service.MaxBackupIndex=5log4j.appender.service.layout=org.apache.log4j.PatternLayoutlog4j.appender.service.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p][%c{1}] [%t] - %m%nlog4j.appender.service.encoding=UTF-8Below the configuration file, you can easily see that I output message (message), async (backend thread), showsql (database log), and service (interface call) to different log files respectively.
Some of these explanations:
log4j.rootLogger=INFO, console, file
log4j has the concept of rootLogger and ordinary Logger. By default, we only need a rootLogger, that is, all logs will only be output to this log file.
Take a look at the configuration of a normal Logger (taking the interface log service as an example):
1. log4j.logger.net.czt.crazyant.service=DEBUG, service
The " net.czt.crazyant.service " in this sentence indicates the full path of the package that takes effect in the normal logger log configuration
The color service indicates the name of the ordinary logger
2. log4j.additivity.net.czt.crazyant.service=false
The " net.czt.crazyant.service " in it is the same as the above, indicating the package targeted by the configuration item.
The meaning of configuration in this sentence is that do not output the log of the package to the rootLogger log, but only output it to the log you configured;
3. log4j.appender.service=org.apache.log4j.RollingFileAppender , and the configuration items below this configuration section
The "service" string here is the same as the "service" of the first configuration item above, indicating the configuration of the ordinary Logger;
The configuration items below are the same as rootLogger, indicating the output file every day, encoding UTF8, sharding rules, output mode for each line, etc.
The problem I encountered was that after log4j.properties above was configured, I found that each log file was created, but there was no content in it. Why is this? Let’s take a look at the second thing you pay attention to below;
Step 2: When outputting the log, you need to set the specific class corresponding to the log object.
What does it mean? In the above configuration item, there is a package string " net.czt.crazyant.service ". So let's think about it ourselves, how does log4j output logger logs in different packages to different files? Think about it, there are two methods:
1. Use intercepter or aop method, log4j detects the log output by itself. When it detects which package the log is generated, it will be output to the corresponding file;
2. The user passes a Class parameter, log4j obtains the corresponding Package of the Class, and use this as the basis to locate different log files;
Looking at the code, it is obvious that log4j uses the latter simple and direct method:
import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory; public class MyClassImpl implements MyClass { /** * loger */ private static final Log logger = LogFactory.getLog(MyClassImpl.class); /** * my func */ public void myfunc() { logger.info("call method myfunc."); }} In logger = LogFactory.getLog(MyClassImpl.class) , the Class parameter using the logger is passed in, and the package address that the Class is reflected is the package address used by log4j to output the log.
This approach also has powerful features, which facilitates logical log classification. For example, many codes do not belong to a package, but they belong to logically together. For example, the processing of messages is not just the interface calling the Service package, but may also call the operation of sending msg. If you want to output some logs in the msg package to the Service, then when the msg logger is initialized, a Service Class is just passed.
Or for all logs of a certain class, all their logger objects can be from a single encapsulated object instance, and there is only one parameter passed in this single object instance, which can be used to identify this logical classification.
Summarize
In Log4j.properties , separate output of the logs of package or specific class is supported, but it also requires that the logger initialization in the code can correspond to the package in the log configuration.
Okay, the above is the entire content of this article. I hope the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.