slf4j and log4j2 use

1. slf4j and log4j2

  slf4j is a log interface can not directly use, requires a combination of a specific frame used to achieve logging (log4j, logback, log4j2 etc.).

1.1 Why not just use a specific implementation, and the need to use the log interfaces

  Interface specification defines a log, you can have different logging implementation, use only the face of the interface (slf4j packages are also used, not directly use the log to achieve a specific package), so the applications are free implementation framework log of changes, without the need to change the relevant code in the log code. In short: just change jar package is implemented to log no direct impact on users, if the service code directly using specific logging implementation, when the change log to achieve, you need to log in to change the business-related calls .

 

2 log4j2 profile

  It consists mainly Configuration, Appenders, Loggers, properties several properties

2.1 Configuration Properties

  There are multiple properties monitorInterval status and so on.

  status: for controlling the frame itself log4j2 log level, for example, can be configured as: "trace", "debug", "info", "warn".

  monitorInterval: every how many seconds to re-read the configuration file, you can not restart the case of application to modify the configuration. Similar to the hot deployment

2.2 Appenders property

  Output source, where the definition of the log output.

  Console: the log output to the console

  File: The log output to a specified file, you need to configure the file path

  RollingRandomAccessFile : also output to a file, but when support when the file reaches a certain size, the other from a new file is written to the log .   

    fileName specify the current log file location and file name
    filePattern Rolling occurs when specified, and rename the file transfer rules
    when SizeBasedTriggeringPolicy specified size when the file volume is greater than the specified value, the trigger Rolling
    number specified file saved DefaultRolloverStrategy most
    TimeBasedTriggeringPolicy this configuration requires and filePattern combination, attention filePattern configuration file rename rule is $ {FILE_NAME} -% d { yyyy-mM-dd HH-mm} -% i, the minimum time of particle size mm, i.e. minute
    TimeBasedTriggeringPolicy specified size is 1, is combined to generate a new file every 1 minute. If the change% d {yyyy-MM-dd HH}, a minimum particle size of hours, each hour generates a file

  Async: asynchronous, the need to specify which output of the source asynchronous (typically used to configure RollingRandomAccessFile) by AppenderRef

2.3 Loggers property

  name can enter the package name, it may be output to different paths in different files in the package

  level: What level of output logs

  additivity: whether inheritance, if it is true, the parent class logger output corresponding to the output file will again log, is usually set to false

<Logger name="rollingRandomAccessFileLogger" level="trace" additivity="true">  
    <AppenderRef ref="RollingRandomAccessFile" />  
</Logger>

2.4 properties property

  Used to define the constant, at other times to refer to the configuration, the configuration is optional

 

3 log4j2 Case

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<-! root path -> <Configuration Status = "WARN" Packages = "org.apache.logging.log4j.core.pattern"> < Properties> ! <- output format definition file, can be referenced directly back to avoid duplication of writing format -> <Property name = "the PATTERN">% {D the mM-dd-YYYY HH: mm: ss.SSS} | -% - 5level [ % Thread] C% [% L] - | n-% MSG% </ Property> </ Properties>   <- - define the path output!> <the appenders in> <Console name = "the CONSOLE" target = " system_out "> <= the PatternLayout pattern" $ {} the pATTERN "/> </ Console> <RollingFile name="TraceLog" fileName="logs/TraceLog.log" filePattern
="logs/TraceLog-$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout pattern="%msg%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="250 MB" /> </Policies> </RollingFile> <Async name="AsyncTraceLog"> <AppenderRef ref="TraceLog" /> </Async> <Async name="AsyncCONSOLE"> <AppenderRef ref="CONSOLE" /> </Async> </appenders>   
  
  <!-- 按照不同的包路径和日志文件路径关联在一起 --> <loggers> <AsyncLogger name="com.log.broker" level="info" additivity="false"> <AppenderRef ref="AsyncCONSOLE" /> </AsyncLogger> <Logger name="com.log.trace" level="info" additivity="false"> <AppenderRef ref="AsyncTraceLog" /> </Logger> <root level="info"> <AppenderRef ref="AsyncCONSOLE" /> </root> </loggers> </configuration>

  Some commonly used file path placeholders:

  % D {HH: mm: ss.SSS} represents the output time to the millisecond
   % T output current thread name
   % -5level output log level - 5 represents a left-aligned and fixed output five characters, if the right if less than 0 % MSG log text
   % n-wrap 

  other commonly placeholders are:
   % class F output where filename, such as Log4j2Test.java
   % L outputs row number
   % M where the output method name
   % l statement where the number of output lines, including the class name, method name, file name, line number

 

This article comes from: https://blog.csdn.net/vbirdbest/article/details/71751835

Guess you like

Origin www.cnblogs.com/lichangfeng/p/12593680.html