java 我想把一个服务里面的所有异常信息全部记录在logger里怎么配置

java 我想把一个服务里面的所有异常信息全部记录在logger里怎么配置,第1张

用log4j

配置文件:

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

<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">

 

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

 

    <appender name="RollingAppender" class="org.apache.log4j.DailyRollingFileAppender">

       <param name="File" value="logs/app.log" />

       <param name="Encoding" value="UTF-8" />

       <param name="DatePattern" value="'.'yyyy-MM-dd" />

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

          <param name="ConversionPattern" value="[%p] %d %c %M - %m%n"/>          

       </layout>

    </appender>

 

    <root>

        <priority value="DEBUG"/>

        <appender-ref ref="RollingAppender" />

    </root>

     

</log4j:configuration>

运用

先实例化

运用

log4j.properties

----------------------------------------------------------------------------------------------

###set output encoding###

log4j.appender.logfile.encoding=UTF-8

### set log levels ###

log4j.rootLogger = debug,stdout,D,E

### out print at console ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n

### write in log ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

#//\u5FC5\u987B\u662F\u5177\u4F53\u7684\u6587\u4EF6\u800C\u4E0D\u662F\u6587\u4EF6\u5939

log4j.appender.D.File = D:\kingcs.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = DEBUG

log4j.appender.D.layout = org.apache.log4j.PatternLayout

log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### save error log ###

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender

# //\u5FC5\u987B\u662F\u5177\u4F53\u7684\u6587\u4EF6\u800C\u4E0D\u662F\u6587\u4EF6\u5939

log4j.appender.E.File = D:\kingcs_error.log

log4j.appender.E.Append = true

log4j.appender.E.Threshold = ERROR

log4j.appender.E.layout = org.apache.log4j.PatternLayout

log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n

### direct log messages to stdout ###

#log4j.appender.stdout=org.apache.log4j.ConsoleAppender

#log4j.appender.stdout.Target=System.out

#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###

#log4j.appender.file=org.apache.log4j.FileAppender

#log4j.appender.file.File=hibernate.log

#log4j.appender.file.layout=org.apache.log4j.PatternLayout

#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

#log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info

#log4j.logger.org.hibernate=debug

### log HQL query parser activity

#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL

#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###

#log4j.logger.org.hibernate.type=info

#log4j.logger.org.hibernate.type=debug

### log schema export/update ###

#log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees

#log4j.logger.org.hibernate.hql=debug

### log cache activity ###

#log4j.logger.org.hibernate.cache=debug

### log transaction activity

#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition

#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###

### leakages when using DriverManagerConnectionProvider ###

#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

要用的jar包

---------------------------------------------------------------------

commons-logging-1.1.1.jar

log4j-1.2.16.jar

测试

--------------------------------------------------------------------

public static void main(String[] args) {

Logger log = Logger.getLogger(Test.class)

log.debug("test!!!!!!!!!!!!!!!!")

log.error("err!!!!!!!!!!!!!!!!!!11")

}

以上测试代码运行下,这时打开 D:\kingcs.log 和D:\kingcs_error.log看看有没有打印。。。

希望能够帮助你。

同上,在你的项目中加上log4j,

log4j就是log for java,Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,具体就不啰嗦了,配置过程网上有一大堆

要学习可以看看这里的:http://blog.csdn.net/azheng270/article/details/2173430/

对着配一次就明白了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11460100.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存