Writing a program on the Java platform also takes some time. From the previous J2EE to the current background program, log4j is more or less used to output logs. However, every time you use the program, someone else configures it, then, let me know how to use it and call it directly. It is always in the semi-solution state for log4j. Today, I have studied log4j in detail by knowing its attitude.
1. Use log4j in eclipse
For programmers, the fastest way is to get started first and see the intuitive effect, so the first step is how to make your programs available in eclipse.
Step 1: first, create a project minatest, add a package named testlog4j, and add the following code:
Package testlog4j;
Import org. Apache. log4j. Logger;
Import org. Apache. log4j. basicconfigurator;
Import org. Apache. log4j. level;
Public class testlog4j {
Public static void main (string argv []) {
// Create a logger by the name of class log4jtest.
Logger logger = logger. getlogger (testlog4j. Class );
// Use the default configuration.
// Basicconfigurator. Configure ();
// Set the logger level to level. Info
Logger. setlevel (level. info );
// This request will be disabled since level. debug <level. info.
Logger. debug ("this is Debug .");
// These requests will be enabled.
Logger.info ("this is an info .");
Logger. Warn ("this is a warning .");
Logger. Error ("this is an error .");
Logger. Fatal ("this is a fatal error .");
Return;
}
}
Step 2: add the log4j. jar package to the Project
After the above Code is written, a syntax error will be prompted in eclipse, because eclipse does not know where to import the corresponding class. In package explorer, right-click the current project and choose property> JAVA build path. On the tab page, choose libraries> Add jars to add the downloaded jar package of log4j. "OK", you will find that in the current project of package explorer, in addition to the previous JRE system library, there is also a referenced libraries, which is the added jar package. After "clean", no syntax error occurs.
At this time, if we run as application, we will get the following prompt:
Log4j: warn no appenders cocould be found for logger (COM. Foo ).
Log4j: Warn please initialize the log4j system properly.
Log4j: Warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Because the program cannot find the log4j. properties configuration file. Therefore, the configuration file is added to the current project.
Step 3: Add log4j. properties to the current project
1) Right-click the current project. New-> folder, enter the name of a folder in the pop-up dialog box "folder name". log4j is used here. Click "advanced" and select "link to folder in the file system" and "Browse" as a directory. Generally, select the directory of the current project and enter the name of the associated folder, for example, D:/workspace/minatest/log4j. In this way, an associated directory is created. Next, find the created log4j folder in package explorer, and import the log4j. properties file you defined (the import process is saved). If there is no ready-made file, you can refer to the following:
Log4j. rootlogger = debug, R, O
# Stdout
Log4j. appender. O = org. Apache. log4j. leleappender
# File
Log4j. appender. r = org. Apache. log4j. rollingfileappender
Log4j. appender. R. File = log4j. Log
# Control the maximum log file size
Log4j. appender. R. maxfilesize = 100kb
# Archive log files (one backup file here)
Log4j. appender. R. maxbackupindex = 1
Log4j. appender. R. layout = org. Apache. log4j. patternlayout
Log4j. appender. O. layout = org. Apache. log4j. patternlayout
Log4j. appender. r. layout. conversionpattern = [% d {iso8601}] % 5 P % 6.6r [% T] % x-% C. % m (% F: % L)-% m % N
Log4j. appender. o. layout. conversionpattern = [% d {iso8601}] % 5 P % 6.6r [% T] % x-% C. % m (% F: % L)-% m % N
Use it first, meaning it will be said later
Step 4 compile
Right-click the package (testlog4j), run as-> RUN configuration, select classpath on the tab page displayed, and select the current project "minatest ", the "advanced" on the right is displayed in the optional status. After you click it, a dialog box is displayed. Select "add Folders", find the current project, select the log4j folder, and click OK. In this way, we will add log4j. properties to the class path of the current project. Select Run. View the eclipse Console
[19:44:42, 523] info 0 [main]-testlog4j. testlog4j. Main (testlog4j. Java: 24)-This is an info.
[19:44:42, 533] warn 10 [main]-testlog4j. testlog4j. Main (testlog4j. Java: 25)-this is a warning.
[19:44:42, 533] Error 10 [main]-testlog4j. testlog4j. Main (testlog4j. Java: 26)-this is an error.
[19:44:42, 533] fatal 10 [main]-testlog4j. testlog4j. Main (testlog4j. Java: 27)-this is a fatal error.
At this point, you can write code and debug it in eclipse.
2 log4j. Properties
For more information, see the relevant documentation. Here we only use the format to help you get started.
The basic format of the log4j configuration file is as follows:
# Configure the root Logger
Log4j. rootlogger = [level], appendername1, appendername2 ,...
Let's look back at the above
Log4j. rootlogger = debug, R, O
Here, R and O are two appdernames. Of course, they can also be other names.
The next step defines different attributes for the two appender.
First look at appender o
Log4j. appender. O = org. Apache. log4j. leleappender defines the output to the console.
Log4j. appender. O. layout = org. Apache. log4j. patternlayout defines the output layout.
Log4j. appender. o. layout. conversionpattern = [% d {iso8601}] % 5 P % 6.6r [% T] % x-% C. % m (% F: % L)-% m % N output format
When you look at appender R, the difference is that the output is different.
Log4j. appender. r = org. Apache. log4j. rollingfileappender
Log4j. appender. R. File = log4j. Log
This means output to the file
Therefore, this program still has a log4j. log file in the current project.
For more information, see the official documentation!