在struts2的网站上看到一个不错的网站:http://www.vaannila.com 看到里面的一些文章很不错。从今天开始陆续翻译一些文章。今天开始翻译Log4j。
Log4j is a simple and flexible logging framework. In this tutorial you will learn how to configure log4j for your applications. Let’s get started first download the latest version of log4j ( Download ). I am using log4j version 1.2.15. Add the log4j-1.2.15.jar to the classpath.
Log4j是一个简单而且灵活的日志记录框架,在这教程中,我们将学习到在你的应用中如何对log4j进行配置。首先下载最新的版本log4j(下载)。我使用的是1.2.15版本。将Jar包log4j-1.2.15.jar 加入类路径。
Next you need to create an instance of Logger class. You can create one using the Logger.getLogger(HelloWorld.class) method. It takes one argument the fully qualified class name.
接下来,我们需要创建一个Logger类实例。我们可以创建一个使用Logger.getLogger(Helloworld.class)的方法。该方法需要传递一个类的完整名字。
Now we need to configure log4j. The simple way to do that is using BasicConfigurator. configure() method. This will log all the messages on the console.
现在,我们需要配置下log4j。简单的方法是使用BasicConfigurator. configure() 方法。这将会在控制台输出记录的所有信息。
Now everything is ready you can log messages using any of the print statements of the Logger class. In the following code I use the debug() method to display the “HelloWorld!” message.
现在,所有的都准备好了。我们可以使用Logger类中任何的输出方式来记录这些消息。在接下来的代码中,我使用debug的方法来显示“Helloworld!”的信息。
package comvaannilahelloworld; import orgapachelog4jBasicConfigurator; import orgapachelog4jLogger; public class HelloWorld { static final Logger logger = LoggergetLogger(HelloWorld.class); public static void main(String[] args) { BasicConfiguratorconfigure(); loggerdebug("Hello World!"); } }
The other methods available are info(), warn(), error() and fatal(). Each method represents a logger level namely DEBUG, INFO, WARN, ERROR and FATAL.
The following example shows how to use these methods.
此外,还可以使用 info(), warn(), error() and fatal()等方法。每个方法代表了一个日志记录级别分别为: DEBUG, INFO, WARN, ERROR and FATAL.
接下里的一个例子将演示如何使用这些方法
package comvaannilahelloworld; import orgapachelog4jBasicConfigurator; import orgapachelog4jLogger; public class HelloWorld { static final Logger logger = LoggergetLogger(HelloWorldclass); public static void main(String[] args) { BasicConfiguratorconfigure(); loggerdebug("Sample debug message"); loggerinfo("Sample info message"); loggerwarn("Sample warn message"); loggererror("Sample error message"); loggerfatal("Sample fatal message"); } }
Here is the output of the above code.
以上代码输出的结果如下:
0 [main] DEBUG comvaannilahelloworldHelloWorld - Sample debug message 0 [main] INFO comvaannilahelloworldHelloWorld - Sample info message 0 [main] WARN comvaannilahelloworldHelloWorld - Sample warn message 0 [main] ERROR comvaannilahelloworldHelloWorld - Sample error message 0 [main] FATAL comvaannilahelloworldHelloWorld - Sample fatal message
The output contains the time elapsed from the start of the program in milliseconds, the thread name, the logger level, the class name and the log message.
输出的信息包含程序执行所使用的毫秒,线程名,日志级别,类名和日志信息。
You can download the log4j configuration file here.
你可以下载这个例子
Source :Download
Source + Lib :Download
该文章翻译自:http://www.vaannila.com/log4j/log4j-configuration.html