Log4J and JBuilder, EclPse

xiaoxiao2021-03-06  84

Although this article is short but the general developer is already enough, the application process of Log4j in JBuilder and Eclipse is described in the text, and it is a very good start. I believe.

1 Overview

Log4j is a log4 family member, a log4 family package: Look at 4 The letter knows which environment and the API developed. The log4j we want to introduce is of course an API developed for Java.

Log4 family

Log4c

Suitable for c

LOG4CPLUS / LOG4CPP

Suitable for C

Log4j

Suitable for Java

Log4net

Suitable for DOTNET

Log4perl

Suitable for Perl

Log4PHP

Suitable for PHP

Log4plsql

Suitable for PL / SQL

Log4ls

Suitable for LotusScript log4py / log4p

Suitable for python qmmslog

Suitable for QT / C platforms

Log4R

Suitable for Ruby JDK1.4's Util.logging Framework

Suitable for JDK1.4UTIL

We omitted the long-term big interlass for log4j, briefly summarizes its role: it is the output interface of the application log, the output format, and output location can be used during development, and can be used during operation. The log category mainly:

Log class

Debug

Debugging information

Info

General information

Warn

Warning information

Error

Error message

Fatal

Severe error message

That is, you can define the type of output log based on these levels. We still go directly to how to use, don't talk too much theory.

2, download log4j

The download address is: http://logging.apache.org/site/binindex.cgi, the highest version available is:

JAKARTA-LOG4J-1.2.8, there are different packages, such as the Windows platform: jakarta-log4j-1.2.8.zip, find: jakarta-log4j-1.2.8 / dist / lib directory log4j- 1.2.8.jar file, this is the JAR file we have to use, and other documents are temporarily restless. Copy this file to the specified location, such as D: / lib directory. (We will add this file in the application engineering).

3, create an app in JBuilder

Procedure can refer to other information

4, add log4j-1.2.8.jar to JBuilder project

4.1 Establish "User Home" library (as an example of JBuilder9 trial version)

Click on the menu [Tools] - [Configure Libraries] to open the Library File Design dialog box, select "User Home" in the tree directory, click the [New ....] Button, pop up the Configuration dialog box, enter log4jlib in the "Name" position, then Click the [Add ....] Button, navigate through the directory tree to the directory where the log4j-1.2.8.jar is located, such as a D: / lib directory, select Log4j-1.2.8.jar, click 3 times [OK] back to jBuilder Main interface. This way you have established a library file. Let JBuilder can use the latest version of Log4j. In fact, in the advanced JBUILDER version already contains the library file of log4j, that is, this step can be omitted.

4.2, add library file reference to the project

Right-click the project name in JBuilder's "Project" dialog box, select "Properties" in the pop-up POP menu, open the Engineering Properties dialog, select the [Paths] tag - [Required Libraries] tab - [Add ....] Button, Select "log4jlib" and click 2 [ok] to return to the main interface of JBuilder, so that the project adds library file reference. 5, log4j output format definition

Log4j's format definition is defined by a ".properties" file, which defines a lot of elements regarding the digital formats and output locations, see the case where the following example is (the example in log4j.properties). Switch log4j.rootlogger = debug, b1 of B1 or A1, and comment different locations, implement log output to file or console. Place this log4j.properties file is placed in the topic of the project (with the .jpx file in the same directory).

Log4j.properties content:

Log4j.rootlogger = debug, b1 # Print to file # log4j.Appender.a1 = org.apache.log4j.rollingfileAppender # log4j.Appender.a1.file = example.log # log4j.Appender.a1.maxfilesize = 100KB # log4j. appender.a1.maxbackupindex = 1 # log4j.Appender.a1.Layout = org.apache.log4j.patternLayout # log4j.Appender.a1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M % N

# Print to the console log4j.Appender.b1 = org.apache.log4j.consoleappender log4j.Appender.b1.Layout = Org.apache.log4j.patternLayout log4j.Appender.b1.Layout.conversionPattern =% - 4R% -5p [ % T]% 37C% 3X -% m% n log4j.Appender.b1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

6, use log4j in the Java class file

6.1, IMPORT needs package file

Import org.apache.log4j. *; Place this statement on the IMPORT position of the file.

6.2, define the logger object

Static logger logger = logger.getlogger (frame1.class.getname ()); defines this object where the program declared variables and is a static object. Where Frame1 is the name of your current class.

6.3, load attribute file

PropertyConfigurator.configure ("log4j.properties"); fill in this statement at the beginning of the Jbinit () method, load the properties file.

6.4, you can use anywhere

Input similar to where logging is required

Logger.info ("Log Information");

Logger.warn ("Log Information");

Logger.debug ("Log Information");

Logger.fatal ("Log Information");

Logger.Error ("Log Information");

such as:

/ ** * Main Factory component initialization * @throws java.lang.exception * / private void jbinit () throws exception {propertyConfigurator.configure ("log4j.properties"); logger.info (Logonuser "initialization start"); // logonuser is the user name of the landing system ................ ................... ................ logger.info (Logonuser "initialization end");} The output result is:

INFO [AWT-EventQueue-0] - Admin Initialization Start INFO [AWT-EventQueue-0] (MainFrame.java: 1013) - ADMIN initialization end

Here, we have completed the log4j application process in JBuilder, and the format of the output log is not very neat, the process of outputting the test of the file, just changed in log4j.properties:

Log4j.properties content:

log4j.rootLogger = debug, A1 # Print to File log4j.appender.A1 = org.apache.log4j.RollingFileAppender log4j.appender.A1.File = example.log log4j.appender.A1.MaxFileSize = 100KB log4j.appender.A1. MaxBackupindex = 1 log4j.appender.a1.Layout = org.apache.log4j.patternLayout log4j.Appender.a1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

# Print to the console # log4j.Appender.b1 = org.apache.log4j.consolerappender # log4j.appender.b1.Layout = org.apache.log4j.patternlayout # log4j.Appender.b1.Layout.conversionPattern =% - 4R% -5P [% T]% 37C% 3X -% M% N # log4j.Appender.b1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

Log4j will create an Example.log file in places with log4j.properties, and log information. When the size of the file is greater than 100KB, log4j creates a new EXAMPLE.LOG file, back up the Example.log file. The number of backup files depends on the MaxBackupIndex property.

Next section We introduce the application process of log4j on Eclipse

7, add log4j-1.2.8.jar to Eclipse

7.1, introduced when new Java projects

The first case is the second step in the second step in the new Java project (add external jar (x)] - select log4j-1.2.8.jar, click [Open] Back to New Engineering Second Step , Click to complete. This introduces the required library file.

7.2, to the existing project, add log4j-1.2.8.jar

If the project already exists, by right-click Project Name - [Properties] - [Java Build Path] - [Add External JAR (X)] - Select Log4j-1.2.8.jar, click [Open] Back to New Engineering Properties - [OK] will go to the interface of the project. 7.3, import log4j.properties file

Through the import function of the project, import the log4j.properties file directly to the most high-level directory of the project. Other steps are 6.1-6.4

7.4, an ordinary application structure is as follows:

/ *

* CREATED ON 2004-10-10

* /

Package com.log4j;

Import org.apache.log4j. *;

/ **

* @Author jem.lee

* /

Public class mylogtest {

Static logger logger = logger.getlogger (MyLogtest.class.getname ());

Public static void main (String [] args) {

PropertyConfigurator.configure ("log4j.properties");

MyLogtest.logger.info ("Test Information Start");

MyLogtest.logger.info ("Test Information End");

}

}

Output results:

INFO [main] (myLogtest.java: 16) - Test information start

INFO [main] (myLogtest.java: 17) - the end of the test information

8, summary

Using the log4j can realize the output of the log record, in fact, many applications will bring a log management module, how to fully utilize the log is also a systematic characteristics, the way you develop log class features and operations may be better than log4j, but I believe you Most of it is impossible, it is better to use the ready-made API, the efficiency is improved, the standard is neat, everything is fine.

转载请注明原文地址:https://www.9cbs.com/read-108496.html

New Post(0)