log4j 用法
来源:互联网 发布:睡眠面膜推荐 知乎 编辑:程序博客网 时间:2024/06/06 22:45
关于getLogger:
public static Logger getLogger(Class clazz)
- Shorthand for
getLogger(clazz.getName())
. - Parameters:
clazz
- The name ofclazz
will be used as the name of the logger to retrieve. SeegetLogger(String)
for more detailed information.
public static Logger getLogger(String name)
- Retrieve a logger named according to the value of the
name
parameter. If the named logger already exists, then the existing instance will be returned. Otherwise, a new instance is created.By default, loggers do not have a set level but inherit it from their neareast ancestor with a set level. This is one of the central features of log4j.
- Parameters:
name
- The name of the logger to retrieve.
即getLogger如果没有这个name对应的logger,那么将产生一个新的logger
关于log4j.properties:
除了rootLogger以外,还可以设置更多的logger,只要设置好log级别,appender,以及输出样式就可以了,例如下面的例子,rootLogger是DONG, INFO级别,打到文件dong.log, 另外定义了一个logger SUN, INFO级别,打到文件sun.log, 值得注意的是
log4j.additivity.xxx的设置,默认为true,即其他logger的也会输出到rootLogger,置为false可以只打到子logger本身,
除非通过Logger.getlogger指定配置文件中定义好的logger,否则将采用rootLogger的配置,
通过
setLevel(Level.toLevel("DEBUG"))可以在程序中设置logLevel为DEBUG
log4j.properties:
log4j.rootLogger=INFO, DONGlog4j.logger.sun=INFO, SUNlog4j.additivity.sun=falselog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%nlog4j.appender.CONSOLE.Threshold=INFOlog4j.appender.CONSOLE.Target=System.outlog4j.appender.CONSOLE.Encoding=GBKlog4j.appender.DONG = org.apache.log4j.RollingFileAppenderlog4j.appender.DONG.File = dong.loglog4j.appender.DONG.Encoding=GBKlog4j.appender.DONG.MaxFileSize = 10MBlog4j.appender.DONG.MaxBackupIndex = 20log4j.appender.DONG.layout = org.apache.log4j.PatternLayoutlog4j.appender.DONG.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%nlog4j.appender.SUN = org.apache.log4j.RollingFileAppenderlog4j.appender.SUN.File = sun.loglog4j.appender.SUN.Encoding=GBKlog4j.appender.SUN.MaxFileSize = 10MBlog4j.appender.SUN.MaxBackupIndex = 20log4j.appender.SUN.layout = org.apache.log4j.PatternLayoutlog4j.appender.SUN.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
以下是一个程序例子:
java文件:
package dong.sun;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Properties;import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;public class logTest {public static void main(String[] args) {System.out.println("This is a test for log4j");try { String confFileName = "./log4j.properties"; File confFile = new File(confFileName); FileInputStream is; is = new FileInputStream(confFile); Properties properties = new Properties(); properties.load(is); PropertyConfigurator.configure(properties); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }Logger log1 = Logger.getLogger("dong.sun.logTest1");if (log1.isDebugEnabled()) {log1.debug("debug message for dong.sun.logTest1 after setting log4j.properties");}if (log1.isInfoEnabled()) {log1.info("info message for dong.sun.logTest1 after setting log4j.properties");}Logger log2 = Logger.getLogger("sun");if (log2.isDebugEnabled()) {log2.debug("debug message for dong.sun before setting log level");}if (log2.isInfoEnabled()) {log2.info("info message for dong.sun before setting log level");}log2.setLevel(Level.toLevel("DEBUG"));if (log2.isDebugEnabled()) {log2.debug("debug message for dong.sun after setting log level");}if (log2.isInfoEnabled()) {log2.info("info message for dong.sun after setting log level");}}}
pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sun</groupId> <artifactId>logTest</artifactId> <version>1.0</version> <name>logTest</name> <description>test for logger4j</description> <dependencies><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.9</version></dependency> </dependencies> <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.6</source><target>1.6</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>dong.sun.logTest</mainClass></manifest></archive></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy</id><phase>install</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/lib</outputDirectory></configuration></execution></executions></plugin></plugins></build></project>
build以后的target为:
target
|__logTest-1.0.jar
|__ lib
|__log4j-1.2.9.jar
最后将logTest-1.0.jar,lib以及log4j.properties置于同一目录下运行java -jar logTest-1.0.jar,结果如下:
dong.log:
2013-01-05 00:56:56,668 [main] [dong.sun.logTest1] [INFO] - info message for dong.sun.logTest1 after setting log4j.properties
sun.log:
2013-01-05 00:56:56,668 [main] [sun] [INFO] - info message for dong.sun before setting log level
2013-01-05 00:56:56,668 [main] [sun] [DEBUG] - debug message for dong.sun after setting log level
2013-01-05 00:56:56,668 [main] [sun] [INFO] - info message for dong.sun after setting log level
- 【log4j】log4j的用法
- Log4j用法
- log4j用法
- Log4J用法
- log4j用法
- Log4j用法
- Log4j用法
- log4j用法
- log4j用法
- log4j用法
- log4j 用法
- log4j用法
- Log4j用法
- Log4J用法
- log4j用法。
- log4j的用法
- Log4j 用法及配置
- log4j 的用法
- php学习笔记(三十二)ajax结合pageView类实现页面无刷新请求
- CMD打开IIS,重启iis等
- Tornado 安装
- windows下opengl开发
- ADO.NET对象模型
- log4j 用法
- 程序员的职业方向: 技术?管理?
- object pascal跟c的一些小区别
- 字段初始值无法引用非静态字段、方法
- JTAG接口无法连接的问题
- 第六章 初始化与清除
- 静态链表
- 可更新订阅的事务复制错误:列名 'msrepl_tran_version' 无效
- 跟开涛学SpringMVC 5章-6章