第一部分,快速入门rTJLinux联盟 rTJLinux联盟 首先,需要去下载LOG4J这个软件并解压缩出其中的log4j.jar.在你的应用程序的classpath中包含该JAR文件,你也可以简单地将这个文件拷贝到JDK的%java_home%\lib\ext目录下。rTJLinux联盟 在作完以上工作后,你可以将下面的代码保存到名为TestLogging.java中:rTJLinux联盟 ##############################rTJLinux联盟 import org.apache.log4j.*;rTJLinux联盟 rTJLinux联盟 // How to use log4jrTJLinux联盟 public class TestLogging {rTJLinux联盟 rTJLinux联盟 // Initialize a logging category. Here, we get THE ROOT CATEGORYrTJLinux联盟 //static Category cat = Category.getRoot();rTJLinux联盟 // Or, get a custom categoryrTJLinux联盟 static Category cat = Category.getInstance(TestLogging.class.getName());rTJLinux联盟 rTJLinux联盟 // From here on, log away! Methods are: cat.debug(your_message_string),rTJLinux联盟 // cat.info(...), cat.warn(...), cat.error(...), cat.fatal(...)rTJLinux联盟 rTJLinux联盟 public static void main(String args[]) {rTJLinux联盟 // Try a few logging methodsrTJLinux联盟 cat.debug("Start of main()");rTJLinux联盟 cat.info("Just testing a log message with priority set to INFO");rTJLinux联盟 cat.warn("Just testing a log message with priority set to WARN");rTJLinux联盟 cat.error("Just testing a log message with priority set to ERROR");rTJLinux联盟 cat.fatal("Just testing a log message with priority set to FATAL");rTJLinux联盟 rTJLinux联盟 // Alternate but INCONVENIENT formrTJLinux联盟 cat.log(Priority.DEBUG, "Calling init()");rTJLinux联盟 rTJLinux联盟 new TestLogging().init();rTJLinux联盟 }rTJLinux联盟 rTJLinux联盟 public void init() {rTJLinux联盟 java.util.Properties prop = System.getProperties();rTJLinux联盟 java.util.Enumeration enum = prop.propertyNames();rTJLinux联盟 rTJLinux联盟 cat.info("***System Environment As Seen By Java***");rTJLinux联盟 cat.debug("***Format: PROPERTY = VALUE***");rTJLinux联盟 rTJLinux联盟 while (enum.hasMoreElements()) {rTJLinux联盟 String key = (String) enum.nextElement();rTJLinux联盟 cat.info(key + " = " + System.getProperty(key));rTJLinux联盟 }rTJLinux联盟 }rTJLinux联盟 rTJLinux联盟 }rTJLinux联盟 rTJLinux联盟 ############################################################rTJLinux联盟 Log4J 默认情况下可以记录五个层次(由低到高)的日志消息。rTJLinux联盟 1) debugrTJLinux联盟 2)inforTJLinux联盟 3)warnrTJLinux联盟 4)errorrTJLinux联盟 5)fatalrTJLinux联盟 rTJLinux联盟 在TestLoggin.class的目录中保存下列行在一个名字为log4j.properties 文件中.默认情况下,当你在代码中使用getRoot()或getInstance("category_name")时,Log4j会在应用程序的 classpath中查找该文件:rTJLinux联盟 ############################################rTJLinux联盟 log4j.rootCategory=DEBUG, dest1rTJLinux联盟 log4j.appender.dest1=org.apache.log4j.ConsoleAppenderrTJLinux联盟 log4j.appender.dest1.layout=org.apache.log4j.PatternLayoutrTJLinux联盟 ############################################rTJLinux联盟 ConsoleAppender指定的是控制台附加器,即日志消息会输出到控制台上,而PatternLayout则指定了消息输出的格式,默认情况下格式为%m%n,%m指定的是消息内容,%n指定的是操作系统平台上的换行符,这里更类似于C语言中的输出控制语句。rTJLinux联盟 现在,你可以编译并且运行TestLogging.java了,你可以获得以下输出结果:rTJLinux联盟 rTJLinux联盟 Start of main()rTJLinux联盟 Just testing a log message with priority set to INFOrTJLinux联盟 Just testing a log message with priority set to WARNrTJLinux联盟 Just testing a log message with priority set to ERRORrTJLinux联盟 Just testing a log message with priority set to FATALrTJLinux联盟 Calling init()rTJLinux联盟 ***System Environment As Seen By Java***rTJLinux联盟 ***Format: PROPERTY = VALUE***rTJLinux联盟 java.runtime.name = Java(TM) 2 Runtime Environment, Standard EditionrTJLinux联盟 sun.boot.library.path = c:\jdk1.3\jre\binrTJLinux联盟 java.vm.version = 1.3.0_02rTJLinux联盟 java.vm.vendor = Sun Microsystems Inc.rTJLinux联盟 ... and so onrTJLinux联盟 rTJLinux联盟 如果想打印消息的层次如debug,info,error等,那可以在log4j.properties 文件的最后一行上增加如下一行:rTJLinux联盟 log4j.appender.dest1.layout.ConversionPattern=%-5p: %m%nrTJLinux联盟 这一行覆盖了默认的消息输出格式%m%n,%p指定的是打印消息的层次(info,debug...,其中-5指定的是五个字符的宽度,-指定的是左对齐),%m指定的是消息的内容,%n指定的则是操作系统平台上的换行符.rTJLinux联盟 当作完这些工作后,无须重新编译TestLogging.java,再次运用TestLogg,会得到以下不出的输出结果:rTJLinux联盟 DEBUG: Start of main()rTJLinux联盟 INFO : Just testing a log message with priority set to INFOrTJLinux联盟 WARN : Just testing a log message with priority set to WARNrTJLinux联盟 ERROR: Just testing a log message with priority set to ERRORrTJLinux联盟 FATAL: Just testing a log message with priority set to FATALrTJLinux联盟 DEBUG: Calling init()rTJLinux联盟 INFO : ***System Environment As Seen By Java***rTJLinux联盟 DEBUG: ***Format: PROPERTY = VALUE***rTJLinux联盟 INFO : java.runtime.name = Java(TM) 2 Runtime Environment, Standard EditionrTJLinux联盟 INFO : sun.boot.library.path = c:\jdk1.3\jre\binrTJLinux联盟 INFO : java.vm.version = 1.3.0_02rTJLinux联盟 INFO : java.vm.vendor = Sun Microsystems Inc.rTJLinux联盟 ... and so onrTJLinux联盟 rTJLinux联盟 如果不想输出日志的DEBUG与INFO消息,那么可以修改"log4j.rotCategory=DEBUG,dest1"为:rTJLinux联盟 log4j.rootCategory=WARN,dest1rTJLinux联盟 该行文件告诉Log4j跳过层次低于WARN的消息输出,也就是说如DEBUG,INFO层次的消息将不会产生输出,再次运行TestLogging.class,得到以下结果:rTJLinux联盟 ####################rTJLinux联盟 WARN : Just testing a log message with priority set to WARNrTJLinux联盟 ERROR: Just testing a log message with priority set to ERRORrTJLinux联盟 FATAL: Just testing a log message with priority set to FATALrTJLinux联盟 ####################rTJLinux联盟 rTJLinux联盟 第二部分 Log4j 详解rTJLinux联盟 Log4j有三个主要的组件:category ,附件器和布局。rTJLinux联盟 在程序中,你可以初始化一个category 并且调用它的各种日志方法来将消息字符串记录到日志中。rTJLinux联盟 一个category可以被配置用来输出到多个目标,这些日志目标在Log4j框架中被称为附件器,这些附件器可以包括控制台、文本文件、HTML文件、 XML文件甚至是Windows平的事件日志系统,甚至可以被作为邮件被发送。而这些所有的目标都是通过log4j.properties文件来进行配置,对于使用Log4j框架的程序来讲只是简单地调用类似于info()、debug()等的方法。rTJLinux联盟 附件器类可以是ConsoleAppender, FileAppender, SMTPAppender, SocketAppender, NTEventLogAppender, SyslogAppender, JMSAppender, AsyncAppender 和 NullAppender等。 附件器类可以使用布局(layout)来在发送消息到目标之前进行格式化。例如HTMLLayout将会把消息格式化为HTML 格式。rTJLinux联盟 除了可以记录消息字符串到日志文件之外,同时还可以记录日期、时间、消息层次、类名、源代码的行数、方法名称、线程名称以及其它信息,而具体的输出需要由附件器的布局管理器来配置。rTJLinux联盟 category的名字是大小写区分以"."分隔的一个字符串。一般情况下我们通常使用your_class_name.class.getName()来获得一个JAVA类名来作为category的名字,例如testproj.util.test。rTJLinux联盟 Each word in the category name is said to be an ancestor of the subsequent words and a parent of the immediately following word. This is important because Log4j has this concept of inheriting priorities and appenders from ancestors until overridden in a particular category.rTJLinux联盟 有一个没有名称的category叫root,它就像xml的document元素,是所有category的祖先。rTJLinux联盟 可以使用以下代码来初始一个根category或指定的category。rTJLinux联盟 ################rTJLinux联盟 Category cat = Category.getRoot();rTJLinux联盟 Category cat2 = Category.getInstance("your.category.name");rTJLinux联盟 ###################rTJLinux联盟 代表层次的常量由高到次是FATAL、ERROR、WARN、INFO和DEBUG,可以在log4j.properties中指定category所属的层次,例如指定log4j.rootCategory=WARN,simple则意味调用root这个category的程序只会记录WARN及 WARN以上的消息。如果没有为一个category指定默认的category,那么category将会从其父category来继承。rTJLinux联盟 常见的Category类的日志方法有:rTJLinux联盟 public void log(Priority p, Object message);rTJLinux联盟 rTJLinux联盟 // Convenient shortcuts to the generic logging methodrTJLinux联盟 public void debug(Object message);rTJLinux联盟 public void info(Object message);rTJLinux联盟 public void warn(Object message);rTJLinux联盟 public void error(Object message);rTJLinux联盟 public void fatal(Object message);rTJLinux联盟 rTJLinux联盟 log4j 只记录层次与预设层次相等或更高级别的消息,如以下代码:rTJLinux联盟 Category cat = Category.getRoot();rTJLinux联盟 cat.setPriority(Priority.ERROR);//设置预设层次为ERROR级rTJLinux联盟 // Later...rTJLinux联盟 //cat.info("Started processing..."); //这条消息将不会输出,ERRORrTJLinux联盟 cat.error("User input is erroneous!"); //消息输出,层次相等rTJLinux联盟 cat.fatal("Cannot process user input. Program terminated!"); //消息输出,层次高于预设层次rTJLinux联盟 rTJLinux联盟 第三部分 Log4j 配置rTJLinux联盟 所有的配置工作应该在log4j.properties文件中完成,而该文件一般须放在应用程序的相同的目录中。rTJLinux联盟 在日志系统使用之前,我们必须首先配置log4j.配置log4j意味着增加附件器到Category并且为每一个Category设置一个Layout。rTJLinux联盟 category之间是有继承关系,但他们增加到log4j.properties文件中的顺序是不固定的。rTJLinux联盟 示例一:rTJLinux联盟 #############################################################rTJLinux联盟 # 设置log4j的根category所使用的预设层次是DEBUG,而只使用A1这个附件器.rTJLinux联盟 log4j.rootCategory=DEBUG, A1rTJLinux联盟 #附件器A1被设置为控制台附件器。rTJLinux联盟 log4j.appender.A1=org.apache.log4j.ConsoleAppenderrTJLinux联盟 #附件器使用的布局是PatternLayout,即模式布局rTJLinux联盟 log4j.appender.A1.layout=org.apache.log4j.PatternLayoutrTJLinux联盟 #附件器A1的模式是%-4r [%t] %-5p %c %x - %m%n,其中%m代表消息字符串,%n代表换行符,其它以%开头的字符代表的含义如下文。rTJLinux联盟 log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%nrTJLinux联盟 ################################################################# rTJLinux联盟 示例二:rTJLinux联盟 #########################################################rTJLinux联盟 #### Use two appenders, one to log to console, another to log to a filerTJLinux联盟 log4j.rootCategory=debug, stdout, RrTJLinux联盟 # Print only messages of priority WARN or higher for your categoryrTJLinux联盟 log4j.category.your.category.name=WARNrTJLinux联盟 # Specifically inherit the priority levelrTJLinux联盟 #log4j.category.your.category.name=INHERITEDrTJLinux联盟 #### First appender writes to consolerTJLinux联盟 log4j.appender.stdout=org.apache.log4j.ConsoleAppenderrTJLinux联盟 rTJLinux联盟 log4j.appender.stdout.layout=org.apache.log4j.PatternLayoutrTJLinux联盟 # Pattern to output the caller's file name and line number.rTJLinux联盟 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%nrTJLinux联盟 #### Second appender writes to a filerTJLinux联盟 log4j.appender.R=org.apache.log4j.RollingFileAppenderrTJLinux联盟 log4j.appender.R.File=example.logrTJLinux联盟 # Control the maximum log file sizerTJLinux联盟 log4j.appender.R.MaxFileSize=100KBrTJLinux联盟 # Archive log files (one backup file here)rTJLinux联盟 log4j.appender.R.MaxBackupIndex=1rTJLinux联盟 log4j.appender.R.layout=org.apache.log4j.PatternLayoutrTJLinux联盟 log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nrTJLinux联盟 ########################################################rTJLinux联盟
|