记录到能用的程度
首先官网下载
http://logging.apache.org/log4j/2.x/download.html
下载bin的那个
从中取出log4j-api-XX.jar和log4j-core-XX.jar导入进eclipse中(从properties--build path里面添加)
或者这些dependence加到pom.xml里面
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency>
classpath(src根目录或是src/main/resources,具体未解)下面建立log4j.properties
内容如下
#config root logger log4j.rootLogger = INFO,system.out log4j.appender.system.out=org.apache.log4j.ConsoleAppender log4j.appender.system.out.layout=org.apache.log4j.PatternLayout log4j.appender.system.out.layout.ConversionPattern=%5p{%F:%L} - %m%n #log级别{文件名:行数} - 信息 换行 #config this Project.file logger log4j.logger.thisProject.file=INFO,thisProject.file.out log4j.appender.thisProject.file.out=org.apache.log4j.DailyRollingFileAppender log4j.appender.thisProject.file.out.File=logContentFile.log log4j.appender.thisProject.file.out.layout=org.apache.log4j.PatternLayout配置就算好了
在想要输出log的类里创建logger对象并使用
首先import这些
import org.apache.log4j.LogManager; import org.apache.log4j.Logger;
private static final Logger logger = LoggerFactory.getLogger(类名.class.getName());
然后用
logger.info("info");
logger.warn("warn");
logger.error("error");
等语句输出,输出格式在properties里面设置,具体不知
参考:
http://blog.csdn.net/lu8000/article/details/25754415
http://blog.sina.com.cn/s/blog_5f7d1a610100ebph.html
http://www.cnblogs.com/suman/archive/2010/10/23/1858864.html