log4j配置

日志路径:

web方式:
web.xml

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>webapp.root</param-value> </context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>

log4j.properites

log4j.rootCategory=INFO,stdout,R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %t %c - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${webapp.root}/logs/log.log log4j.appender.R.MaxFileSize=10000KB
log4j.appender.R.MaxBackupIndex=100
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %t %c - %m%n #log4j.logger.org.hibernate=INFO
log4j.logger.com.sohu.fortune.talk=DEBUG
log4j.logger.org.apache.commons.httpclient=ERRORf

Log4j支持两种配置文件格式,一种是XML格式的文件,一种是properties格式的文件。

获取路径:

String rootPath=this.getClass().getClassLoader().getResource("").getPath();

1.log4j.properties配置方法:

步骤:

0.下载log4j的jar包;

1.新建java项目;

2.新建包:

3.新建类:

4.在src下新建log4J.properties文本文件【日志配置文件】

5.新建lib文件夹:【存放第三方jar包】

6.新建bin文件夹:【存放生成的.class文件】

如下图:

package com.loveryw;

import org.apache.log4j.Logger; 

import org.apache.log4j.PropertyConfigurator;

public class log4Test {
private static Logger loger; //测试函数
public static void main(String args[])
{
String filePath="C:\\workspace\\Log4jTest\\src\\";
PropertyConfigurator.configure(filePath+"log4j.properties"); //获取路径,并加载log4j的配置文件
/*
roperties properties=new Properties();
InputStream inStream =inStream = Launcher.class.getClassLoader().getResourceAsStream("conf/log4j.properties");
  try {
  properties.load(inStream);
  }catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  PropertyConfigurator.configure(properties);
  log.info("start...");
*/
loger=Logger.getLogger("log4Test");
loger.debug("我是个bug");
loger.info("我是个info");
loger.error("我是个erro");
}
}

配置文件内容:log4J.properites

log4j.rootLogger=info,stdout,info,debug,error
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = INFO
log4j.appender.info.append=true
log4j.appender.info.File=C:\\workspace\\Log4jTest\\logs\\info.log log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.debug.datePattern='.'yyyy-MM-dd
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.append=true
log4j.appender.debug.File=C:\\workspace\\Log4jTest\\logs\\debug.log log4j.logger.error=error
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = ERROR
log4j.appender.error.append=true
log4j.appender.error.File=C:\\workspace\\Log4jTest\\logs\\error.log

2.xml配置方法:【主要用于web的配置】

常用参数说明:

1.log4j有三种主要组件:logger、appender and layout
  2.Log4j提供的appender有以下几种:
  org.apache.log4j.ConsoleAppender(控制台)
  org.apache.log4j.FileAppender(文件)
  org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
  org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件)
  org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
  3.Log4j提供的layout有以下几种:
  org.apache.log4j.HTMLLayout(以HTML表格形式布局)
  org.apache.log4j.PatternLayout(可以灵活地指定布局模式)
  org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串)
  org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
  4.Log4j提供的几种输出格式:
  %M:Used to output the method name where the logging request was issued.
  %m:Used to output the application supplied message associated with the logging event.
  %l:Used to output location information of the caller which generated the logging event
  %L:Used to output the line number from where the logging request was issued.
  %p:Used to output the priority of the logging event.
  %n:Outputs the platform dependent line separator character or characters.
  %r:Used to output the number of milliseconds elapsed since the start of the application until the creation of the logging event.
  %F:Used to output the file name where the logging request was issued.
  %d:Used to output the date of the logging event.
  %c:Used to output the category of the logging event
  %C:Used to output the fully qualified class name of the caller issuing the logging request
  5.如果是对于效率要求比较高的话,要在log.debug()之前加上log.isDebugEnabled()进行判断,这样能够大大减少执行时间
  6.对于各个appenders,共有的属性是layout(一般设置为org.apache.log4j.PatternLayout),Threshold(Log的级别)
  (1)ConsoleAppender:Target(System.out和System.err)
  (2)FileAppender:File(定义输出的文件名),Append(定义是否为追加)
  (3)DailyRollingFileAppender(除FileAppender属性外):MaxFileSize(最大文件大小),MaxBackupIndex()

最新文章

  1. HDU1003 简单DP
  2. ES6详解
  3. 3098: Hash Killer II
  4. c# 多线程 --Mutex(互斥锁)
  5. FS_11C14温湿度传感器(二)
  6. mysql主从复制配置(精简版)
  7. CreateWaitableTimer和SetWaitableTimer函数(定时器)
  8. linux入门教程(六) Linux文件与目录管理
  9. Java实现SSO
  10. Android开发系列之学习路线图
  11. (转载)javascript转自世纪流年blog
  12. 也谈android开发图像压缩
  13. optimizer hints
  14. PCIE_DMA实例三:Xilinx 7系列(KC705/VC709)FPGA的EDK仿真
  15. 还在为CSS布局发愁?你该看看这7条原则
  16. JMeter插件之 BlazeMeter&#39;s XMPP----测试Openfire等
  17. pip命令安装 pyinstaller失败解决办法
  18. emitted value instead of an instance of error the scope attribute for scoped slots webpack babel polyfill
  19. Spring基础(8) : 延迟加载,Bean的作用域,Bean生命周期
  20. 3.操作jQuery集合《jquery实战》

热门文章

  1. Excel分类汇总
  2. PHP实现图片压缩的两则实例(转)
  3. 【HTML代码】访问页面时,拨打页面中的电话号码
  4. PostgreSQL服务器参数配置
  5. angular使用代理解决跨域
  6. 阿里云服务器ubuntu安装redis2.8.13
  7. MySQL学习笔记之一---字符编码和字符集
  8. asp.net的验证码插件及方法、ashx验证码一般处理程序
  9. 向linux内核增加一个系统调用-1
  10. springmvc 初始化参数绑定(使用属性编辑器) 来处理类型转换问题