📅  最后修改于: 2021-01-06 09:07:53             🧑  作者: Mango
日志记录级别用于对日志文件中的条目进行分类。但是它们以非常特定的方式进行分类,即按紧急程度进行分类。该级别使您可以分离以下类型的信息:
系统中提供的信息的数量和类型以及事件日志由配置文件中的log4j级别设置控制。每条日志消息均以消息级别为前缀。
日志记录级别是org.apache.log4j.Level类的实例。
Log4j具有以下级别的日志记录:
Log Level | Description |
---|---|
ALL | This level turns on all levels of logging. It includes the custom logging levels that you have defined. Once this one is configured and the levels are not considered at all, then all the appenders will start pouring the log events in log files. |
DEBUG | Debug is used a lot for debugging the application at development time. Every log message will appear to log files once this level is set. It basically belongs to developers. |
INFO | The INFO logging level is used to record messages about routine application operation. In real-time, system administrators watch the info logs to ensure what’s happening on the system right now, and if there is any problem in normal flow. |
WARN | WARN log level is used to indicate that you might have a problem and that you’ve detected an unusual situation. Maybe you were demanding to invoke a service, and it failed a couple of times before connecting on an automatic retry. It is unexpected and unusual, but no real harm was done, and it’s not known whether the issue will persist or recur. Someone should investigate warnings. |
ERROR | The ERROR log level is used to denote a serious problem that you must have to investigate immediately. Not as serious as FATAL, but still a problem. It simply means that your application has met really undesired state. For example, unexpected formatted input, database unavailability. |
FATAL | The FATAL log level, like ERROR, designates a problem. But unlike ERROR, it designates a very serious error event. You will not consider their presence very much on a normal day, but once they appear, it signals very bad news, even the application of death. |
OFF | This is the highest possible rank and is intended to turn off logging. |
TRACE | This has been recently introduced in version 1.2 and includes more information to debug level logs. |
在log4j.properties中设置日志级别
log4j.rootLogger=DEBUG, consoleAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
#Log info messages for package 'com.javatpoint.web.controller'
log4j.logger.com.javatpoint.web.controller=INFO, consoleAppender
在log4j.xml中设置日志级别
日志记录级别的工作实际上非常简单。在运行时,应用程序代码将创建具有级别的日志记录请求。同时,日志记录框架具有配置的日志级别,该级别用作阈值。如果请求级别在配置级别或更高级别,则它将记录到配置的目标。如果没有,则拒绝。就这么简单。
让我们将其视为以下级别的等级顺序:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
因此,例如,如果将日志记录框架级别设置为WARN,则将接受具有WARN,FATAL和ERROR级别中的任何级别的请求,而其余请求将被拒绝。
在上图中,垂直标题显示LogEvent的级别,而水平标题显示与适当的日志记录配置关联的Level。
对于第一列,您将看到日志在每个级别的工作方式。例如,对于WARN,(FATAL,ERROR和WARN)将可见。如果设置为OFF,则看不到任何内容。
配置文件:
# 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
Java程序:
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!");
}
}
现在编译并运行上面的程序,我们将在c:/usr/home/log4j/log.out文件中获得以下输出:
Warn Message!
Error Message!
Fatal Message!