log4j的一个简单例子
来源:互联网 发布:专业开淘宝店 编辑:程序博客网 时间:2024/05/01 16:54
一个简单的例子
我们先来看一个简单的例子,它是一个用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的主要步骤。
3. 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 ) ;
4. 参考资料
如果您想更深入地了解Log4j,请经常访问下面提及的相关链接。
Log4j项目主页------------------------------------------------------www.log4j.org
Log4j FAQ -------------------------------------------------------www.log4j.org/log4j/faq.html
我们先来看一个简单的例子,它是一个用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的主要步骤。
3. 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 ) ;
4. 参考资料
如果您想更深入地了解Log4j,请经常访问下面提及的相关链接。
Log4j项目主页------------------------------------------------------www.log4j.org
Log4j FAQ -------------------------------------------------------www.log4j.org/log4j/faq.html
- log4j的一个简单例子
- 一个简单的log4j例子
- log4j简单的例子
- log4j :一个完整的例子
- 一个简单的+Log4j+配置
- 一个简单的例子
- 一个关于spring,log4j的例子
- 一个不错的log4j.properties例子
- 一个不错的log4j.properties例子
- log4j简单例子
- log4j 简单例子
- log4j的简单的例子~以及使用log4j和不使用log4j的对比
- openmp 例子:一个简单的OpenMP例子
- 一个简单的Struts例子
- 一个简单的Hibernate例子!
- auto_ptr的一个简单例子
- 一个简单的Struts例子
- 一个简单的JSF例子
- pragma指令简介
- #pragma 预处理指令详解
- 枚举法(Enumeration)
- Spring 的MVC I18N-国际化相关配置
- asp.net几种安全验证方式
- log4j的一个简单例子
- 关于Oracle sequence的cache的一点内容
- 高级语言,汇编语言,机器语言
- InputBox,“OK,Cancel”两个按钮,无论点哪个都一样,怎么办?
- 人一生要去中国的55个地方
- seecord
- 开源WinMD5中的MTQueue类
- 判断字符为数字
- C#操作xml文件入门