### Logger hierarchy
- Named Hierarchy
A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.
- root logger
- it always exists,
- it cannot be retrieved by name.
- Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
- Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
DEBUG < INFO < WARN < ERROR < FATAL.
// get a logger instance named "com.foo" |
### Appenders
More than one appender can be attached to a logger.
Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.
- Appender Additivity
The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".
However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.
Loggers have their additivity flag set to true by default.
log4j.additivity.logName = false
or
log.setAdditivity(false); |
### PatternLayout
Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character.
- The conversion character specifies the type of data, e.g. category, priority, date, thread name.
- The format modifiers control such things as field width, padding, left and right justification.
%-5p means the priority of the logging event should be left justified to a width of five characters
- Conversion Character
Conversion Character
### Configuration
- BasicConfigurator.configure();
- creates a rather simple log4j setup
- add to the root logger a ConsoleAppender
- The output will be formatted using a PatternLayout set to the pattern "%-4r [%t] %-5p %c %x - %m%n"
- by default, the root logger is assigned to Level.DEBUG
### Properties
${} from System.properties or Log4j.props
### threshold
修改某个logger其中一个appender的level
log4j.appender.R2.threshold=WARN
### example
log4j.rootLogger=INFO,R,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${etl.log}/root.log
log4j.appender.R.MaxFileSize=25MB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.logger.xdata.etl.kafka.consumer.Consumer=INFO,C1,C2
log4j.appender.C1=org.apache.log4j.RollingFileAppender
log4j.appender.C1.File=${etl.log}/consumer/info.log
log4j.appender.C1.MaxFileSize=25MB
log4j.appender.C1.MaxBackupIndex=1
log4j.appender.C1.layout=org.apache.log4j.PatternLayout
log4j.appender.C1.layout.ConversionPattern=[%-5p]\t[%d{yyyy-MM-dd HH:mm:ss,SSS}]\t[%r]\t%F\t%m%n
log4j.appender.C2.threshold=WARN
log4j.appender.C2=org.apache.log4j.RollingFileAppender
log4j.appender.C2.File=${etl.log}/consumer/error.log
log4j.appender.C2.MaxFileSize=25MB
log4j.appender.C2.MaxBackupIndex=1
log4j.appender.C2.layout=org.apache.log4j.PatternLayout
log4j.appender.C2.layout.ConversionPattern=[%-5p]\t[%d{yyyy-MM-dd HH:mm:ss,SSS}]\t[%r]\t%F\t%m%n