Using log4j to output logs of different packages to different files

preface

With the increasing scale of the project, new modules will be introduced continuously. Different modules will print their own logs. Finally, the logs cannot be viewed at all. For example, in my own project, there are the following logs:

Among them, the amount of message log and background thread log data is very large. If all logs are printed in one file, use tail - f log Log file, you will find that the log is scrolling rapidly, and you can't view or even locate a specific SQL or service access log at all.

The solution is to classify and output different logs, so that the logs do not affect each other. In particular, the important interface access log can easily locate and troubleshoot problems.

Step 1: in log4j Configuration in properties

First post all my own log4j Properties configuration:

At the bottom of the configuration file, you can easily see that I output message, async, showsql and service to different log files.

Some of these explanations:

log4j. rootLogger=INFO,file

Log4j has the concepts of a rootlogger and an 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 common logger (taking the interface log service as an example):

1、log4j. logger. net. czt. crazyant. service=DEBUG,service

Net czt. crazyant. "Service" indicates the full path of the package in which the normal logger log configuration takes effect

The color service indicates the name of the common logger

2、log4j. additivity. net. czt. crazyant. service=false

Net czt. crazyant. "Service", the same as above, indicates the package for which this configuration item is targeted

This sentence means not to output the log of the package to the rootlogger log, but only to the log configured by yourself;

3、log4j. appender. service=org. apache. log4j. Rollingfileappender and the configuration items under the 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 daily output file, encoding utf8, fragmentation rules, output mode of each line, etc

My own problem is log4j After configuring the properties, I found that each log file was created, but there was no content in it. Why? Let's look at the second point of attention below;

Step 2. When outputting the log, you need to set the specific class corresponding to the log object

What do you mean? In the above configuration item, there is a "net czt. crazyant. The package string of "service". Let's think about how log4j outputs the logger logs in different packages to different files. Think about two methods:

1. In the form of interceptor or AOP, log4j detects the log output by itself, and outputs the log to the corresponding file when it detects the package in which the log is generated;

2. The user passes a class parameter and log4j obtains the package corresponding to the class, which is used to locate different log files;

Looking at the code, it is obvious that log4j uses the latter simple and direct method:

At logger = LogFactory In getlog (myclassimpl. Class), the class parameter using the logger is passed in, and the package address reflected by the class is the package address log4j uses to output the log.

This method is also powerful to facilitate logical log classification. For example, many codes do not belong to a package, but they belong to the same package. For example, the message processing is not only the interface calling the service package, but also the operation of sending MSG. If you want to output some logs in the MSG package to the service, When the logger of this msg is initialized, just pass in a service class.

Or for all logs of a class, all their logger objects can come from an encapsulated single object instance, and the single object instance can only pass in one parameter to identify the logical classification.

summary

In log4j In properties, the log of package or specific class can be output separately, but it also needs to correspond to the package in the log configuration when the logger in the code is initialized.

Well, the above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>