关于log4j和commons-logging的一些疑惑?求指点

飞火流云 2016-11-01 08:39:11
1. 请问log4j和commons-logging的区别在哪里?
2. 希望将将同一个包内的不同的类的日志输出到同一个指定文件中。
为实现该目的,代码实现如下:

package logTest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogTest {

public static final Log LOG =
LogFactory.getLog(LogTest.class);

public void test() {
LOG.info("test1") ;
LOG.debug("test1") ;
}
}


package logTest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogTest2 {
public static final Log LOG =
LogFactory.getLog(LogTest2.class);

public static void main(String[] args) {
LOG.info("test2") ;
LOG.debug("test2") ;
new LogTest().test() ;
}

}


log4j.properties配置如下
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

LOG_DIR=F://LogTest/
log4j.rootLogger=INFO,console

#Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c-%L]-[%p] %m%n

#test package print out
log4j.logger.logTest=INFO,logT
log4j.appender.logT=org.apache.log4j.RollingFileAppender
log4j.appender.logT.MaxFileSize=10240KB
log4j.appender.logT.MaxBackupIndex=2
log4j.appender.logT.File=${LOG_DIR}/test.log
log4j.appender.logT.layout=org.apache.log4j.PatternLayout
log4j.appender.logT.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c-%L]-[%p] %m%n



结果输出:
log4j:WARN No appenders could be found for logger (logTest.LogTest2).
log4j:WARN Please initialize the log4j system properly.

不知道哪里错了?
...全文
270 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞火流云 2016-11-02
  • 打赏
  • 举报
回复
不知道问题在哪里? 配置似乎没有问题,我的目的是配置整个包的日志输出而非单个类,为什么会有: log4j:WARN No appenders could be found for logger (logTest.LogTest2).?
飞火流云 2016-11-01
  • 打赏
  • 举报
回复
明明是输出同一个包内的文件,为啥总提示我单个类的文件配置不对呢? 奇怪了 log4j:WARN No appenders could be found for logger (logTest.LogTest2).
Spring3: org.springframework.aop-3.1.0.RELEASE.jar org.springframework.asm-3.1.0.RELEASE.jar org.springframework.aspects-3.1.0.RELEASE.jar org.springframework.beans-3.1.0.RELEASE.jar org.springframework.context-3.1.0.RELEASE.jar org.springframework.context.support-3.1.0.RELEASE.jar org.springframework.core-3.1.0.RELEASE.jar org.springframework.expression-3.1.0.RELEASE.jar org.springframework.instrument-3.1.0.RELEASE.jar org.springframework.instrument.tomcat-3.1.0.RELEASE.jar org.springframework.jdbc-3.1.0.RELEASE.jar org.springframework.jms-3.1.0.RELEASE.jar org.springframework.orm-3.1.0.RELEASE.jar org.springframework.oxm-3.1.0.RELEASE.jar org.springframework.spring-library-3.1.0.RELEASE.libd org.springframework.test-3.1.0.RELEASE.jar org.springframework.transaction-3.1.0.RELEASE.jar org.springframework.web-3.1.0.RELEASE.jar org.springframework.web.portlet-3.1.0.RELEASE.jar org.springframework.web.servlet-3.1.0.RELEASE.jar org.springframework.web.struts-3.1.0.RELEASE.jar 由于我也不搞不清楚,Spring这么多jar包每个的作用,有些有相互依赖,没办法,只能全部一起导进来。如果有高人指点一下他们的作用,不胜感激! Spring3.0.1 jar下载地址:http://www.springsource.org/download Mybatis: mybatis-3.0.6.jar--mybatis核心包(http://code.google.com/p/mybatis/downloads/list) mybatis-spring-1.0.2.jar--同Spring整合所需jar(http://code.google.com/p/mybatis/downloads/list) Spring AOP 依赖jar: aopalliance-1.0.jar asm-3.3.1.jar aspectj-1.6.10.jar aspectjweaver-1.6.9.jar cglib-2.2.2.jar Jstl标签依赖jar: jstl-1.2.jar MySql数据库驱动jar: mysql-connector-java-5.0.4-bin.jar 其他jar: commons-logging-1.0.4.jar log4j-1.2.9.jar portlet-api-2.0.jar

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧