使用Log4j進行日誌操作

jianghe_03發表於2008-04-11
在應用程式中新增日誌記錄總的來說基於三個目的:監視程式碼中變數的變化情況,週期性的記錄到檔案中供其他應用進行統計分析工作;跟蹤程式碼執行時軌跡,作為日後審計的依據;擔當整合開發環境中的偵錯程式的作用,向檔案或控制檯列印程式碼的除錯資訊。
概述


1.1. 背景


在應用程式中新增日誌記錄總的來說基於三個目的:監視程式碼中變數的變化情況,週期性的記錄到檔案中供其他應用進行統計分析工作;跟蹤程式碼執行時軌跡,作為日後審計的依據;擔當整合開發環境中的偵錯程式的作用,向檔案或控制檯列印程式碼的除錯資訊。

最普通的做法就是在程式碼中嵌入許多的列印語句,這些列印語句可以輸出到控制檯或檔案中,比較好的做法就是構造一個日誌操作類來封裝此類操作,而不是讓一系列的列印語句充斥了程式碼的主體。

1.2. Log4j簡介


在強調可重用元件開發的今天,除了自己從頭到尾開發一個可重用的日誌操作類外,Apache為我們提供了一個強有力的日誌操作包-Log4j。

Log4j是Apache的一個開放原始碼專案,通過使用Log4j,我們可以控制日誌資訊輸送的目的地是控制檯、檔案、GUI元件、甚至是套介面伺服器、NT的事件記錄器、UNIX Syslog守護程式等;我們也可以控制每一條日誌的輸出格式;通過定義每一條日誌資訊的級別,我們能夠更加細緻地控制日誌的生成過程。最令人感興趣的就是,這些可以通過一個配置檔案來靈活地進行配置,而不需要修改應用的程式碼。

此外,通過Log4j其他語言介面,您可以在C、C++、.Net、PL/SQL程式中使用Log4j,其語法和用法與在Java程式中一樣,使得多語言分散式系統得到一個統一一致的日誌元件模組。而且,通過使用各種第三方擴充套件,您可以很方便地將Log4j整合到J2EE、JINI甚至是SNMP應用中。

本文介紹的Log4j版本是1.2.3。作者試圖通過一個簡單的客戶/伺服器Java程式例子對比使用與不使用Log4j 1.2.3的差別,並詳細講解了在實踐中最常使用Log4j的方法和步驟。在強調可重用元件開發的今天,相信Log4j將會給廣大的設計開發人員帶來方便。加入到Log4j的隊伍來吧!


回頁首


一個簡單的例子


我們先來看一個簡單的例子,它是一個用Java實現的客戶/伺服器網路程式。剛開始我們不使用Log4j,而是使用了一系列的列印語句,然後我們將使用Log4j來實現它的日誌功能。這樣,大家就可以清楚地比較出前後兩個程式碼的差別。

2.1. 不使用Log4j


2.1.1. 客戶程式

package log4j ;
import java.io.* ;
import java.net.* ;
/**
*
* <p> Client Without Log4j </p>
* <p> Description: a sample with log4j</p>
* @version 1.0
*/
public class ClientWithoutLog4j {
/**
*
* @param args
*/
public static void main ( String args [] ) {
String welcome = null;
String response = null;
BufferedReader reader = null;
PrintWriter writer = null;
InputStream in = null;
OutputStream out = null;
Socket client = null;
try {
client = new Socket ( "localhost", 8001 ) ;
System.out.println ( "info: Client socket: " + client ) ;
in = client.getInputStream () ;
out = client.getOutputStream () ;
} catch ( IOException e ) {
System.out.println ( "error: IOException : " + e ) ;
System.exit ( 0 ) ;
}
try{
reader = new BufferedReader( new InputStreamReader ( in ) ) ;
writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ;
welcome = reader.readLine () ;
System.out.println ( "debug: Server says: '" + welcome + "'" ) ;
System.out.println ( "debug: HELLO" ) ;
writer.println ( "HELLO" ) ;
response = reader.readLine () ;
System.out.println ( "debug: Server responds: '" + response + "'") ;
System.out.println ( "debug: HELP" ) ;
writer.println ( "HELP" ) ;
response = reader.readLine () ;
System.out.println ( "debug: Server responds: '" + response + "'" ) ;
System.out.println ( "debug: QUIT" ) ;
writer.println ( "QUIT" ) ;
} catch ( IOException e ) {
System.out.println ( "warn: IOException in client.in.readln()" ) ;
System.out.println ( e ) ;
}
try{
Thread.sleep ( 2000 ) ;
} catch ( Exception ignored ) {}
}
}


2.1.2. 伺服器程式

package log4j ;
import java.util.* ;
import java.io.* ;
import java.net.* ;
/**
*
* <p> Server Without Log4j </p>
* <p> Description: a sample with log4j</p>
* @version 1.0
*/
public class ServerWithoutLog4j {
final static int SERVER_PORT = 8001 ; // this server's port
/**
*
* @param args
*/
public static void main ( String args [] ) {
String clientRequest = null;
BufferedReader reader = null;
PrintWriter writer = null;
ServerSocket server = null;
Socket socket = null;
InputStream in = null;
OutputStream out = null;
try {
server = new ServerSocket ( SERVER_PORT ) ;
System.out.println ( "info: ServerSocket before accept: " + server ) ;
System.out.println ( "info: Java server without log4j, on-line!" ) ;
// wait for client's connection
socket = server.accept () ;
System.out.println ( "info: ServerSocket after accept: " + server ) ;
in = socket.getInputStream () ;
out = socket.getOutputStream () ;
} catch ( IOException e ) {
System.out.println( "error: Server constructor IOException: " + e ) ;
System.exit ( 0 ) ;
}
reader = new BufferedReader ( new InputStreamReader ( in ) ) ;
writer = new PrintWriter ( new OutputStreamWriter ( out ) , true ) ;
// send welcome string to client
writer.println ( "Java server without log4j, " + new Date () ) ;
while ( true ) {
try {
// read from client
clientRequest = reader.readLine () ;
System.out.println ( "debug: Client says: " + clientRequest ) ;
if ( clientRequest.startsWith ( "HELP" ) ) {
System.out.println ( "debug: OK!" ) ;
writer.println ( "Vocabulary: HELP QUIT" ) ;
}
else {
if ( clientRequest.startsWith ( "QUIT" ) ) {
System.out.println ( "debug: OK!" ) ;
System.exit ( 0 ) ;
}
else{
System.out.println ( "warn: Command '" +
clientRequest + "' not understood." ) ;
writer.println ( "Command '" + clientRequest
+ "' not understood." ) ;
}
}
} catch ( IOException e ) {
System.out.println ( "error: IOException in Server " + e ) ;
System.exit ( 0 ) ;
}
}
}
}


2.2. 遷移到Log4j


2.2.1. 客戶程式

package log4j ;
import java.io.* ;
import java.net.* ;
// add for log4j: import some package
import org.apache.log4j.PropertyConfigurator ;
import org.apache.log4j.Logger ;
import org.apache.log4j.Level ;
/**
*
* <p> Client With Log4j </p>
* <p> Description: a sample with log4j</p>
* @version 1.0
*/
public class ClientWithLog4j {
/*
add for log4j: class Logger is the central class in the log4j package.
we can do most logging operations by Logger except configuration.
getLogger(...): retrieve a logger by name, if not then create for it.
*/
static Logger logger = Logger.getLogger
( ClientWithLog4j.class.getName () ) ;
/**
*
* @param args : configuration file name
*/
public static void main ( String args [] ) {
String welcome = null ;
String response = null ;
BufferedReader reader = null ;
PrintWriter writer = null ;
InputStream in = null ;
OutputStream out = null ;
Socket client = null ;
/*
add for log4j: class BasicConfigurator can quickly configure the package.
print the information to console.
*/
PropertyConfigurator.configure ( "ClientWithLog4j.properties" ) ;
// add for log4j: set the level
// logger.setLevel ( ( Level ) Level.DEBUG ) ;
try{
client = new Socket( "localhost" , 8001 ) ;
// add for log4j: log a message with the info level
logger.info ( "Client socket: " + client ) ;
in = client.getInputStream () ;
out = client.getOutputStream () ;
} catch ( IOException e ) {
// add for log4j: log a message with the error level
logger.error ( "IOException : " + e ) ;
System.exit ( 0 ) ;
}
try{
reader = new BufferedReader ( new InputStreamReader ( in ) ) ;
writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ;
welcome = reader.readLine () ;
// add for log4j: log a message with the debug level
logger.debug ( "Server says: '" + welcome + "'" ) ;
// add for log4j: log a message with the debug level
logger.debug ( "HELLO" ) ;
writer.println ( "HELLO" ) ;
response = reader.readLine () ;
// add for log4j: log a message with the debug level
logger.debug ( "Server responds: '" + response + "'" ) ;
// add for log4j: log a message with the debug level
logger.debug ( "HELP" ) ;
writer.println ( "HELP" ) ;
response = reader.readLine () ;
// add for log4j: log a message with the debug level
logger.debug ( "Server responds: '" + response + "'") ;
// add for log4j: log a message with the debug level
logger.debug ( "QUIT" ) ;
writer.println ( "QUIT" ) ;
} catch ( IOException e ) {
// add for log4j: log a message with the warn level
logger.warn ( "IOException in client.in.readln()" ) ;
System.out.println ( e ) ;
}
try {
Thread.sleep ( 2000 ) ;
} catch ( Exception ignored ) {}
}
}


2.2.2. 伺服器程式

package log4j;
import java.util.* ;
import java.io.* ;
import java.net.* ;
// add for log4j: import some package
import org.apache.log4j.PropertyConfigurator ;
import org.apache.log4j.Logger ;
import org.apache.log4j.Level ;
/**
*
* <p> Server With Log4j </p>
* <p> Description: a sample with log4j</p>
* @version 1.0
*/
public class ServerWithLog4j {
final static int SERVER_PORT = 8001 ; // this server's port
/*
add for log4j: class Logger is the central class in the log4j package.
we can do most logging operations by Logger except configuration.
getLogger(...): retrieve a logger by name, if not then create for it.
*/
static Logger logger = Logger.getLogger
( ServerWithLog4j.class.getName () ) ;
/**
*
* @param args
*/
public static void main ( String args[]) {
String clientRequest = null ;
BufferedReader reader = null ;
PrintWriter writer = null ;
ServerSocket server = null ;
Socket socket = null ;
InputStream in = null ;
OutputStream out = null ;
/*
add for log4j: class BasicConfigurator can quickly configure the package.
print the information to console.
*/
PropertyConfigurator.configure ( "ServerWithLog4j.properties" ) ;
// add for log4j: set the level
// logger.setLevel ( ( Level ) Level.DEBUG ) ;
try{
server = new ServerSocket ( SERVER_PORT ) ;
// add for log4j: log a message with the info level
logger.info ( "ServerSocket before accept: " + server ) ;
// add for log4j: log a message with the info level
logger.info ( "Java server with log4j, on-line!" ) ;
// wait for client's connection
socket = server.accept() ;
// add for log4j: log a message with the info level
logger.info ( "ServerSocket after accept: " + server ) ;
in = socket.getInputStream() ;
out = socket.getOutputStream() ;
} catch ( IOException e ) {
// add for log4j: log a message with the error level
logger.error ( "Server constructor IOException: " + e ) ;
System.exit ( 0 ) ;
}
reader = new BufferedReader ( new InputStreamReader ( in ) ) ;
writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ;
// send welcome string to client
writer.println ( "Java server with log4j, " + new Date () ) ;
while ( true ) {
try {
// read from client
clientRequest = reader.readLine () ;
// add for log4j: log a message with the debug level
logger.debug ( "Client says: " + clientRequest ) ;
if ( clientRequest.startsWith ( "HELP" ) ) {
// add for log4j: log a message with the debug level
logger.debug ( "OK!" ) ;
writer.println ( "Vocabulary: HELP QUIT" ) ;
}
else {
if ( clientRequest.startsWith ( "QUIT" ) ) {
// add for log4j: log a message with the debug level
logger.debug ( "OK!" ) ;
System.exit ( 0 ) ;
}
else {
// add for log4j: log a message with the warn level
logger.warn ( "Command '"
+ clientRequest + "' not understood." ) ;
writer.println ( "Command '"
+ clientRequest + "' not understood." ) ;
}
}
} catch ( IOException e ) {
// add for log4j: log a message with the error level
logger.error( "IOException in Server " + e ) ;
System.exit ( 0 ) ;
}
}
}
}


2.2.3. 配置檔案

2.2.3.1. 客戶程式配置檔案

log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n


2.2.3.2. 伺服器程式配置檔案

log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n


2.3. 比較


比較這兩個應用可以看出,採用Log4j進行日誌操作的整個過程相當簡單明瞭,與直接使用System.out.println語句進行日誌資訊輸出的方式相比,基本上沒有增加程式碼量,同時能夠清楚地理解每一條日誌資訊的重要程度。通過控制配置檔案,我們還可以靈活地修改日誌資訊的格式,輸出目的地等等方面,而單純依靠System.out.println語句,顯然需要做更多的工作。

下面我們將以前面使用Log4j的應用作為例子,詳細講解使用Log4j的主要步驟。


回頁首


Log4j基本使用方法


Log4j由三個重要的元件構成:日誌資訊的優先順序,日誌資訊的輸出目的地,日誌資訊的輸出格式。日誌資訊的優先順序從高到低有ERROR、WARN、INFO、DEBUG,分別用來指定這條日誌資訊的重要程度;日誌資訊的輸出目的地指定了日誌將列印到控制檯還是檔案中;而輸出格式則控制了日誌資訊的顯示內容。

3.1.定義配置檔案


其實您也可以完全不使用配置檔案,而是在程式碼中配置Log4j環境。但是,使用配置檔案將使您的應用程式更加靈活。

Log4j支援兩種配置檔案格式,一種是XML格式的檔案,一種是Java特性檔案(鍵=值)。下面我們介紹使用Java特性檔案做為配置檔案的方法:

配置根Logger,其語法為:
log4j.rootLogger = [ level ] , appenderName, appenderName, …

其中,level 是日誌記錄的優先順序,分為OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者您定義的級別。Log4j建議只使用四個級別,優先順序從高到低分別是ERROR、WARN、INFO、DEBUG。通過在這裡定義的級別,您可以控制到應用程式中相應級別的日誌資訊的開關。比如在這裡定義了INFO級別,則應用程式中所有DEBUG級別的日誌資訊將不被列印出來。
appenderName就是指定日誌資訊輸出到哪個地方。您可以同時指定多個輸出目的地。
配置日誌資訊輸出目的地Appender,其語法為
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1

log4j.appender.appenderName.option = valueN其中,Log4j提供的appender有以下幾種:
org.apache.log4j.ConsoleAppender(控制檯),
org.apache.log4j.FileAppender(檔案),
org.apache.log4j.DailyRollingFileAppender(每天產生一個日誌檔案),org.apache.log4j.RollingFileAppender(檔案大小到達指定尺寸的時候產生一個新的檔案),
org.apache.log4j.WriterAppender(將日誌資訊以流格式傳送到任意指定的地方)
配置日誌資訊的格式(佈局),其語法為:
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 = value1

log4j.appender.appenderName.layout.option = valueN其中,Log4j提供的layout有以下幾種:
org.apache.log4j.HTMLLayout(以HTML表格形式佈局),
org.apache.log4j.PatternLayout(可以靈活地指定佈局模式),
org.apache.log4j.SimpleLayout(包含日誌資訊的級別和資訊字串),
org.apache.log4j.TTCCLayout(包含日誌產生的時間、執行緒、類別等等資訊)
3.2.在程式碼中使用Log4j


下面將講述在程式程式碼中怎樣使用Log4j。

3.2.1.得到記錄器

使用Log4j,第一步就是獲取日誌記錄器,這個記錄器將負責控制日誌資訊。其語法為:

public static Logger getLogger( String name),

通過指定的名字獲得記錄器,如果必要的話,則為這個名字建立一個新的記錄器。Name一般取本類的名字,比如:

static Logger logger = Logger.getLogger ( ServerWithLog4j.class.getName () ) ;

3.2.2.讀取配置檔案

當獲得了日誌記錄器之後,第二步將配置Log4j環境,其語法為:
BasicConfigurator.configure (): 自動快速地使用預設Log4j環境。
PropertyConfigurator.configure ( String configFilename) :讀取使用Java的特性檔案編寫的配置檔案。
DOMConfigurator.configure ( String filename ) :讀取XML形式的配置檔案。

3.2.3.插入記錄資訊(格式化日誌資訊)

當上兩個必要步驟執行完畢,您就可以輕鬆地使用不同優先順序別的日誌記錄語句插入到您想記錄日誌的任何地方,其語法如下:

Logger.debug ( Object message ) ;
Logger.info ( Object message ) ;
Logger.warn ( Object message ) ;
Logger.error ( Object message ) ;

相關文章