📅  最后修改于: 2020-11-12 05:51:17             🧑  作者: Mango
org.apache.log4j.Level级别。您还可以通过对Level类进行子类化来定义自定义级别。
Level | Description |
---|---|
ALL | All levels including custom levels. |
DEBUG | Designates fine-grained informational events that are most useful to debug an application. |
INFO | Designates informational messages that highlight the progress of the application at coarse-grained level. |
WARN | Designates potentially harmful situations. |
ERROR | Designates error events that might still allow the application to continue running. |
FATAL | Designates very severe error events that will presumably lead the application to abort. |
OFF | The highest possible rank and is intended to turn off logging. |
TRACE | Designates finer-grained informational events than the DEBUG. |
如果p> = q,则在级别为q的记录器中启用级别为p的日志请求。此规则是log4j的核心。它假定级别是有序的。对于标准级别,我们有ALL 以下示例说明了如何过滤所有DEBUG和INFO消息。该程序使用记录器方法setLevel(Level.X)设置所需的记录级别: 此示例将打印除调试和信息以外的所有消息: 编译并运行LogClass程序时,它将生成以下结果- log4j为您提供了基于配置文件的级别设置,该级别设置使您无需更改源代码即可更改调试级别。 以下是一个示例配置文件,该文件将执行与上述示例中使用log.setLevel(Level.WARN)方法执行的任务相同的任务。 现在让我们使用下面的程序- 现在编译并运行以上程序,您将在/usr/home/log4j/log.out文件中得到以下结果-import org.apache.log4j.*;
public class LogClass {
private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
public static void main(String[] args) {
log.setLevel(Level.WARN);
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
Warn Message!
Error Message!
Fatal Message!
使用配置文件设置级别
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = WARN, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
import org.apache.log4j.*;
public class LogClass {
private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
Warn Message!
Error Message!
Fatal Message!