欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈在JAVA项目中LOG4J的使用

程序员文章站 2024-03-11 17:23:37
一、直接使用: //输出到项目文件夹下output1.txt文件中 ////////////////////////////// // debug -...

一、直接使用:

//输出到项目文件夹下output1.txt文件中

//////////////////////////////

// debug - here is some debug

// info - here is some info

// warn - here is some warn

// error - here is some error

// fatal - here is some fatal

//////////////////////////////

package hunnu.sanha.test;

import org.apache.log4j.level;

import org.apache.log4j.logger;

import org.apache.log4j.simplelayout;

import org.apache.log4j.fileappender;

public class simpandfile {

 static logger logger = logger.getlogger(simpandfile.class);

 public static void main(string args[]) {

  simplelayout layout = new simplelayout();

  fileappender appender = null;

  try {

   appender = new fileappender(layout,"output1.txt",false);

  } catch(exception e) {}

  logger.addappender(appender);

  logger.setlevel((level) level.);

  logger.debug("here is some debug");

  logger.info("here is some info");

  logger.warn("here is some warn");

  logger.error("here is some error");

  logger.fatal("here is some fatal");

 }

}

 

//按格式输出到项目文件夹下output2.html

package hunnu.sanha.test;

import java.io.*;

import org.apache.log4j.level;

import org.apache.log4j.logger;

import org.apache.log4j.htmllayout;

import org.apache.log4j.writerappender;

public class htmlandwrite {

 static logger logger = logger.getlogger(htmlandwrite.class);

 public static void main(string args[]) {

  htmllayout layout = new htmllayout();

  writerappender appender = null;

  try {

   fileoutputstream output = new fileoutputstream("output2.html");

   appender = new writerappender(layout,output);

  } catch(exception e) {}

  logger.addappender(appender);

  logger.setlevel((level) level.);

  logger.debug("here is some debug");

  logger.info("here is some info");

  logger.warn("here is some warn");

  logger.error("here is some error");

  logger.fatal("here is some fatal");

 }

}

 

 

//输出到控制台

////////////////////////////////////////////////////////////////////////////////////////////////////////////

milliseconds since program start: 0

classname of caller: hunnu.sanha.test.consandpatt

date in iso8601 format: 2008-07-29 11:02:30,578

location of log event: hunnu.sanha.test.consandpatt.main(consandpatt.java:20)

message: here is some debug

 

milliseconds since program start: 15

classname of caller: hunnu.sanha.test.consandpatt

date in iso8601 format: 2008-07-29 11:02:30,593

location of log event: hunnu.sanha.test.consandpatt.main(consandpatt.java:21)

message: here is some info

 

milliseconds since program start: 15

classname of caller: hunnu.sanha.test.consandpatt

date in iso8601 format: 2008-07-29 11:02:30,593

location of log event: hunnu.sanha.test.consandpatt.main(consandpatt.java:22)

message: here is some warn

 

milliseconds since program start: 15

classname of caller: hunnu.sanha.test.consandpatt

date in iso8601 format: 2008-07-29 11:02:30,593

location of log event: hunnu.sanha.test.consandpatt.main(consandpatt.java:23)

message: here is some error

 

milliseconds since program start: 15

classname of caller: hunnu.sanha.test.consandpatt

date in iso8601 format: 2008-07-29 11:02:30,593

location of log event: hunnu.sanha.test.consandpatt.main(consandpatt.java:24)

message: here is some fatal

///////////////////////////////////////////////////////

package hunnu.sanha.test;

import org.apache.log4j.level;

import org.apache.log4j.logger;

import org.apache.log4j.patternlayout;

import org.apache.log4j.consoleappender;

public class consandpatt {

 static logger logger = logger.getlogger(consandpatt.class);

 public static void main(string args[]) {

  // note, %n is newline

  string pattern = "milliseconds since program start: %r %n";

    pattern += "classname of caller: %c %n";

    pattern += "date in iso8601 format: %d{iso8601} %n";

    pattern += "location of log event: %l %n";

    pattern += "message: %m %n %n";

  

  patternlayout layout = new patternlayout(pattern);

  consoleappender appender = new consoleappender(layout);

  logger.addappender(appender);

  logger.setlevel((level) level.);

  logger.debug("here is some debug");

  logger.info("here is some info");

  logger.warn("here is some warn");

  logger.error("here is some error");

  logger.fatal("here is some fatal");

 }

}

二、使用配置文件(都直接放到项目文件夹下)

//xmllog4jconfig.xml

<?xml version="1.0" encoding="utf-8" ?>

<!doctype log4j:configuration system "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="appender" class="org.apache.log4j.fileappender">

  <param name="file" value="indentify-log.txt"/>

  <param name="append" value="false"/>

  <layout class="org.apache.log4j.patternlayout">

  <param name="conversionpattern" value="%d [%t] %p - %m%n"/>

  </layout>

 </appender>

 <root>

  <priority value ="debug"/>

  <appender-ref ref="appender"/>

 </root>

</log4j:configuration>

 

//externalxmltest.java

 package hunnu.sanha.external;

import org.apache.log4j.logger;

import org.apache.log4j.xml.domconfigurator;

public class externalxmltest {

 static logger logger = logger.getlogger(externalxmltest.class);

 public static void main(string args[]) {

  domconfigurator.configure("xmllog4jconfig.xml");

  logger.debug("here is some debug");

  logger.info("here is some info");

  logger.warn("here is some warn");

  logger.error("here is some error");

  logger.fatal("here is some fatal");

 }

}

//结果输出到indentify-log.txt文件中

2008-07-29 10:48:11,375 [main] debug - here is some debug

2008-07-29 10:48:11,375 [main] info - here is some info

2008-07-29 10:48:11,375 [main] warn - here is some warn

2008-07-29 10:48:11,375 [main] error - here is some error

2008-07-29 10:48:11,375 [main] fatal - here is some fatal

 

 

// plainlog4jconfig.txt

# initialise root logger with level debug and call it blah

log4j.rootlogger=debug, blah

# add a consoleappender to the logger blah

log4j.appender.blah=org.apache.log4j.consoleappender

# set set that layout to be simplelayout

log4j.appender.blah.layout=org.apache.log4j.simplelayout

 

//externalplaintest.java

package hunnu.sanha.external;

import org.apache.log4j.logger;

import org.apache.log4j.propertyconfigurator;

public class externalplaintest {

 static logger logger = logger.getlogger(externalplaintest.class);

 public static void main(string args[]) {

  propertyconfigurator.configure("plainlog4jconfig.txt");

  logger.debug("here is some debug");

  logger.info("here is some info");

  logger.warn("here is some warn");

  logger.error("here is some error");

  logger.fatal("here is some fatal");

 }

}

//结果输出到控制台

debug - here is some debug

info - here is some info

warn - here is some warn

error - here is some error

fatal - here is some fatal

附加:

2.1.   日志信息的优先级

分为off、fatal、error、warn、info、debug、all或者您定义的级别。
log4j建议只使用四个级别,优先级从高到低分别是error、warn、info、debug。通过在这里定义的级别,您可以控制到应用程序中相应级别的日志信息的开关。
假如在一个级别为q的logger中发生一个级别为p的日志请求,如果p>=q,那么请求将被启用。这是log4j的核心原则。
比如在这里定义了info级别,则应用程序中所有debug级别的日志信息将不被打印出来;

2.2.   输出源的使用

有选择的能用或者禁用日志请求仅仅是log4j的一部分功能。log4j允许日志请求被输出到多个输出源。用log4j的话说,一个输出源被称做一个appender。
appender包括console(控制台), files(文件), gui components(图形的组件), remote socket servers(socket 服务), jms(java信息服务), nt event loggers(nt的事件日志), and remote unix syslog daemons(远程unix的后台日志服务)。它也可以做到异步记录。
一个logger可以设置超过一个的appender。
用addappender 方法添加一个appender到一个给定的logger。对于一个给定的logger它每个生效的日志请求都被转发到该logger所有的appender上和该logger的父辈logger的appender上。

2.2.1.  consoleappender


如果使用consoleappender,那么log信息将写到console。效果等同于直接把信息打印到system.out上了。

2.2.2.  fileappender

使用fileappender,那么log信息将写到指定的文件中。这应该是比较经常使用到的情况。
相应地,在配置文件中应该指定log输出的文件名。如下配置指定了log文件名为dglog.txt
log4j.appender.a2.file=dglog.txt
注意将a2替换为具体配置中appender的别名。

2.2.3.  dailyrollingappender

使用fileappender可以将log信息输出到文件中,但是如果文件太大了读起来就不方便了。这时就可以使用dailyrollingappender。dailyrollingappender可以把log信息输出到按照日期来区分的文件中。配置文件就会每天产生一个log文件,每个log文件只记录当天的log信息:
log4j.appender.a2=org.apache.log4j.dailyrollingfileappender
log4j.appender.a2.file=dglog
log4j.appender.a2.datepattern='.'yyyy-mm-dd
log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern= %5r %-5p %c{2} - %m%n

2.2.4.  org.apache.log4j.rollingfileappender


文件大小到达指定尺寸的时候产生一个新的文件。
log4j.appender.r=org.apache.log4j.rollingfileappender
log4j.appender.r.file= ../logs/dglog.log
# control the maximum log file size
log4j.appender.r.maxfilesize=100kb
# archive log files (one backup file here)
log4j.appender.r.maxbackupindex=1
log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%p %t %c - %m%n
这个配置文件指定了输出源r,是一个轮转日志文件。最大的文件是100kb,当一个日志文件达到最大尺寸时,log4j会自动把example.log重命名为dglog.log.1,然后重建一个新的dglog.log文件,依次轮转。

2.2.5.  org.apache.log4j.writerappender

将日志信息以流格式发送到任意指定的地方。

2.3.  layout的配置

layout指定了log信息输出的样式。

2.3.1.  布局样式

org.apache.log4j.htmllayout(以html表格形式布局),
org.apache.log4j.patternlayout(可以灵活地指定布局模式),
org.apache.log4j.simplelayout(包含日志信息的级别和信息字符串),
org.apache.log4j.ttcclayout(包含日志产生的时间、线程、类别等等信息)

2.3.2.  格式


%m 输出代码中指定的消息
%p 输出优先级,即debug,info,warn,error,fatal
%r 输出自应用启动到输出该log信息耗费的毫秒数
%c 输出所属的类目,通常就是所在类的全名
%t 输出产生该日志事件的线程名
%n 输出一个回车换行符,windows平台为"rn",unix平台为"n"
%d 输出日志时间点的日期或时间,默认格式为iso8601,也可以在其后指定格式,比如:%d{yyy mmm dd hh:mm:ss,sss},输出类似:2002年10月18日 22:10:28,921
%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:testlog4.main(test log4.java:10)

2.3.3.  例子

例子1:显示日期和log信息
log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss} %m%n
打印的信息是:
2002-11-12 11:49:42,866 select * from role where 1=1 order by createdate desc

例子2:显示日期,log发生地方和log信息
log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss} %l "#" %m%n
2002-11-12 11:51:46,313 cn.net.unet.weboa.system.dao.roledao.select(roledao.java:409) "#"
select * from role where 1=1 order by createdate desc 
 
例子3:显示log级别,时间,调用方法,log信息
log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh:mm:ss,sss}
method:%l%n%m%n
log信息:
[debug] 2002-11-12 12:00:57,376
method:cn.net.unet.weboa.system.dao.roledao.select(roledao.java:409)
select * from role where 1=1 order by createdate desc

2.4.   配置文件的例子:

log4j.rootlogger=debug
#将dao层log记录到daolog,alllog中
log4j.logger.dao=debug,a2,a4
#将逻辑层log记录到businesslog,alllog中
log4j.logger.businesslog=debug,a3,a4

#a1--打印到屏幕上
log4j.appender.a1=org.apache.log4j.consoleappender
log4j.appender.a1.layout=org.apache.log4j.patternlayout
log4j.appender.a1.layout.conversionpattern=%-5p [%t] %37c %3x - %m%n

#a2--打印到文件daolog中--专门为dao层服务
log4j.appender.a2=org.apache.log4j.dailyrollingfileappender
log4j.appender.a2.file=daolog
log4j.appender.a2.datepattern='.'yyyy-mm-dd
log4j.appender.a2.layout=org.apache.log4j.patternlayout
log4j.appender.a2.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh:mm:ss,sss}
method:%l%n%m%n

#a3--打印到文件businesslog中--专门记录逻辑处理层服务log信息
log4j.appender.a3=org.apache.log4j.dailyrollingfileappender
log4j.appender.a3.file=businesslog
log4j.appender.a3.datepattern='.'yyyy-mm-dd
log4j.appender.a3.layout=org.apache.log4j.patternlayout
log4j.appender.a3.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh:mm:ss,sss}
method:%l%n%m%n

#a4--打印到文件alllog中--记录所有log信息
log4j.appender.a4=org.apache.log4j.dailyrollingfileappender
log4j.appender.a4.file=alllog
log4j.appender.a4.datepattern='.'yyyy-mm-dd
log4j.appender.a4.layout=org.apache.log4j.patternlayout
log4j.appender.a4.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh:mm:ss,sss}
method:%l%n%m%n

以上这篇浅谈在java项目中log4j的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。