Commons Logging was introduced earlier, which can be used as a "log interface". And the real "logging implementation" can use Log4j.

Log4j is a very popular logging framework, the latest version is 2.x.

Log4j is a component-based log system, and its architecture is roughly as follows:

When we use Log4j to output a log, Log4j automatically outputs the same log to different destinations through different Appenders. E.g:

  • console: output to the screen;
  • file: output to a file;
  • socket: output to a remote computer through the network;
  • jdbc: output to database

In the process of outputting logs, Filter is used to filter which logs need to be output and which logs do not need to be output. For example, only output ERRORlevel logs.

Finally, format the log information through Layout, for example, automatically add information such as date, time, method name, etc.

Although the above structure is complex, when we actually use it, we do not need to care about the API of Log4j, but configure it through the configuration file.

Taking XML configuration as an example, when using Log4j, we can put a log4j2.xmlfile classpathdown to let Log4j read the configuration file and output the log according to our configuration. The following is an example of a configuration file:

Although configuring Log4j is tedious, once configured, it is very convenient to use. For the above configuration file, all INFOlevel logs will be automatically output to the screen, and ERRORlevel logs will not only be output to the screen, but also output to the file at the same time. And, once the log file reaches the specified size (1MB), Log4j will automatically cut the new log file and keep up to 10 copies.

It is not enough to have a configuration file, because Log4j is also a third-party library, we need to download Log4j from here , after decompression, put the following 3 jar packages into classpathit:

  • log4j-api-2.x.jar
  • log4j-core-2.x.jar
  • log4j-jcl-2.x.jar

Because Commons Logging will automatically discover and use Log4j, put the downloaded one in the previous commons-logging-1.2.jarsection classpath.

To print the log, you only need to write it in the way of Commons Logging, without changing any code, you can get the log output of Log4j, similar to:

Best Practices

In the development phase, the Commons Logging interface is always used to write logs, and there is no need to introduce Log4j in the development phase. If you need to write the log to a file, you only need to put the correct configuration file and the jar package related to Log4j classpath, and you can automatically switch the log to use Log4j to write without modifying any code.

practise

According to the configuration file, observe the log file written by Log4j.

commons logging + log4j

summary

Logs are implemented through Commons Logging, and Log4j can be used without modifying the code;

To use Log4j, you only need to put log4j2.xml and related jars into the classpath;

If you want to replace Log4j, you only need to remove log4j2.xml and related jars;

Only when you extend Log4j, you need to refer to the interface of Log4j (for example, the function of encrypting logs and writing them to the database needs to be developed by yourself).

Categories: Java