commons-logging组件与log4j组件的使用,已成为当今非常流行的日志记录方法,在许多
开源软件设计中,都会看到它们的身影;它们的组合使用,不仅简化了的log4j使用,而且还实
现了日志工具的灵活配置,以下介绍其使用方法:
一.commons-logging.properties的配置
#LogFactory implement
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
# SimpleLog
#org.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog
# JDK 1.4 logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# Avalon Toolkit
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.LogKitLogger
#Log4j
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
二.log4j.properties的配置
#rootLogger
log4j.rootLogger = DEBUG,stdout, R
#自定义输出目的地stdout
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}-[TEST] %p [%t] %C.%M(%L)-%m%n
#自定义输出目的地R
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.file=D:\\app\\personalProject\\classes\\hailin.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}-[TEST] %p [%t] %C.%M(%L)-%m%n
#定义具体的类日志属性
log4j.logger.com.tongda.whl.log=INFO
三.把commons-logging.properties,log4j.properties放在classpath下
commons-logging.properties可以不需要(参照commons-logging组件的使用说明),系统会有一个
自动的寻找过程,log4j.properties必须作配置(参照log4j组件的使用说明),至少配置一个根日志
(rootLogger)的级别及其输出地,如果不做配置会报错。如果具体的类不作配置日志属性,会默认继承根日志的
属性;如果需具体配置,则如下所示:
log4j.logger.com.tongda.whl.log=INFO,定义了com.tongda.whl.log包的日志属性
四.程序使用
package com.tongda.whl.log;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class LogTest {
private static Log log=LogFactory.getLog(LogTest.class);
public static void main(String [] args){
// System.out.println(log.getClass());//测试调用的是哪个日志工具
// System.out.println(LogTest.class);//测试类的路径
log.info("ddddddddd");
log.debug("xxxxxxxxx");
}
}
输出如下:2009-01-14 10:25:30-[TEST] INFO [main] com.tongda.whl.log.LogTest.main(9)-ddddddddd
[TEST]一般是项目名称
输出级别是:INFO
分享到:
相关推荐
为什么同时使用commons-logging和Log4j?commons-logging为我们提供了统一的日志接口,可以用各种日志工具类。log4j是目前输出日志的首选。
这是一个讲解commons-logging 和log4j的使用方法,里面有一些例子供大家参考。
官网下载,未作任何改动
用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, common-logging会通过动态查找的机制,在程序运行时自动找出真正使用的日志库。当然,common-logging内部有一个Simple logger的...
NULL 博文链接:https://tianjun309.iteye.com/blog/776137
Commons-loggin的目的是为“所有的Java日志实现”提供一个统一的接口,它自身的日志功能平常弱(只有一个简单的SimpleLog?...另外一个我能想到的“同时使用commons-logging和Log4j”的原因是,简化使用和配置
NULL 博文链接:https://icourses.iteye.com/blog/1839984
log4j日志包 log4j.jar\commons-logging-1.1.1.jar private static final Log log = LogFactory.getLog(xxxxxx.class); 下载后解压就能得到这两个包
开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发...
配置日志文件时需要的jar包,配置日志文件时需要的jar包,配置日志文件时需要的jar包
log4j 用的两个jar包,log4j-1.2.14.jar 和 commons-logging-1.1.jar
When writing a library it is very useful to log information. However there are many logging implementations out there, and a library cannot impose the use of a particular one on the overall ...
poi-3.6-20091214.jar commons-logging-1.1
日志框架需要的Jar包 commons-logging.jar log4j.jar
jar包大小:342KB log4j-1.2.6.jar jar包大小:135KB commons-validator-1.3.1.jar jar包大小:93KB commons-transaction-1.2.jar jar包大小:141KB commons-scxml-0.6.jar jar包大小:254KB commons-primitives-1.0.jar ...
Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的... Because we use commons-logging you are free to choose your logging implementation (Log4j is a common choice).
commons-logging-1.0.4.jar、hessian-3.0.1.jar、log4j-1.2.12.jar
commons-logging配合log4j,java log ,打印输出日志必备
log4j jdklog commons-logging 关系及简单应用 详情看blog http://blog.csdn.net/harrison2010/archive/2011/02/15/6185819.aspx
log4j 与commonslogging,搭配记录日志是现在最流行的记录日志的方法,两者搭配不仅没有增加日志记录的难度,反而大大简化了记录日志的操作。