首页 > 代码库 > log4j-初识

log4j-初识

1.配置文件介绍:

1.1. 控制台输出:log4j.rootLogger=DEBUG, Console ,File

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
1.2.文件输出:
#File
log4j.appender.File = org.apache.log4j.FileAppender
log4j.appender.File.File = C://log2.log
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n

2.程序中使用

package com.open1111;

import org.apache.log4j.Logger;

public class Test {

private static Logger logger=Logger.getLogger(Test.class); // 获取logger实例

public static void main(String[] args) {
logger.info("普通Info信息");
logger.debug("调试debug信息");
logger.error("报错error信息");
logger.warn("警告warn信息");
logger.fatal("严重错误fatal信息");

logger.error("报错信息", new IllegalArgumentException("非法参数"));
}
}

log4j-初识