详解log4j2(下) - Async/MongoDB/Flume Appender 按日志级别区分文件输出

1. 按日志级别区分文件输出

有些人习惯按日志信息级别输出到不同名称的文件中,如info.log,error.log,warn.log等,在log4j2中可通过配置Filters来实现。

假定需求是把INFO及以下级别的信息输出到info.log,WARN和ERROR级别的信息输出到error.log,FATAL级别输出到fatal.log,配置文件如下:

[java]  view plain  copy
 print ?
  1. "WARN" monitorInterval="300">  
  2.       
  3.         "LOG_HOME">D:/logs  
  4.       
  5.       
  6.         "Console" target="SYSTEM_OUT">  
  7.             "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />  
  8.           
  9.   
  10.         "InfoFile"  
  11.             fileName="${LOG_HOME}/info.log"  
  12.             filePattern="${LOG_HOME}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">  
  13.               
  14.                 "warn" onMatch="DENY" onMismatch="NEUTRAL" />  
  15.                 "trace" onMatch="ACCEPT" onMismatch="DENY" />  
  16.               
  17.             "%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />  
  18.               
  19.                   
  20.                 "10 MB" />  
  21.               
  22.             "20" />  
  23.           
  24.           
  25.         "ErrorFile"  
  26.             fileName="${LOG_HOME}/error.log"  
  27.             filePattern="${LOG_HOME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">  
  28.               
  29.                 "fatal" onMatch="DENY" onMismatch="NEUTRAL" />  
  30.                 "warn" onMatch="ACCEPT" onMismatch="DENY" />  
  31.               
  32.             "%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />  
  33.               
  34.                   
  35.                 "10 MB" />  
  36.               
  37.             "20" />  
  38.           
  39.           
  40.         "FatalFile"  
  41.             fileName="${LOG_HOME}/fatal.log"  
  42.             filePattern="${LOG_HOME}/$${date:yyyy-MM}/fatal-%d{yyyy-MM-dd}-%i.log">  
  43.               
  44.                 "fatal" onMatch="ACCEPT" onMismatch="DENY" />  
  45.               
  46.             "%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />  
  47.               
  48.                   
  49.                 "10 MB" />  
  50.               
  51.             "20" />  
  52.           
  53.       
  54.   
  55.       
  56.         "trace">  
  57.             "Console" />  
  58.             "InfoFile" />  
  59.             "ErrorFile" />  
  60.             "FatalFile" />  
  61.           
  62.       
  63.   
测试代码:

[java]  view plain  copy
 print ?
  1. public static void main(String[] args) {  
  2.     Logger logger = LogManager.getLogger(Client.class);  
  3.     logger.trace("trace level");  
  4.     logger.debug("debug level");  
  5.     logger.info("info level");  
  6.     logger.warn("warn level");  
  7.     logger.error("error level");  
  8.     logger.fatal("fatal level");  
  9. }  

2 异步写日志

配置文件:

[java]  view plain  copy
 print ?
  1. "WARN" monitorInterval="300">  
  2.       
  3.         "LOG_HOME">D:/logs  
  4.         "FILE_NAME">mylog  
  5.       
  6.   
  7.       
  8.         "Console" target="SYSTEM_OUT">  
  9.             "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />  
  10.           
  11.         "MyFile"  
  12.             fileName="${LOG_HOME}/${FILE_NAME}.log"  
  13.             filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd HH-mm}-%i.log">  
  14.             
  15.                 pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />  
  16.               
  17.                 "1" />  
  18.                 "10 MB" />  
  19.               
  20.             "20" />  
  21.           
  22.         "Async">  
  23.             "MyFile" />  
  24.           
  25.       
  26.   
  27.       
  28.         "asynclog" level="trace" additivity="false" >  
  29.             "Async" />  
  30.           
  31.         "error">  
  32.             "Console" />  
  33.           
  34.       
  35.   
测试代码:

[java]  view plain  copy
 print ?
  1. public static void main(String[] args) {  
  2.     Logger logger = LogManager.getLogger("asynclog");  
  3.     logger.trace("trace level");  
  4.     logger.debug("debug level");  
  5.     logger.info("info level");  
  6.     logger.warn("warn level");  
  7.     logger.error("error level");  
  8.     logger.fatal("fatal level");  
  9. }  

3 输出到MongoDB

添加依赖:

[java]  view plain  copy
 print ?
  1.   
  2.     org.apache.logging.log4j  
  3.     log4j-nosql  
  4.     2.5  
  5.   
  6.   
  7.     org.mongodb  
  8.     mongo-java-driver  
  9.     3.2.2  
  10.   
配置文件:

[java]  view plain  copy
 print ?
  1. "WARN" monitorInterval="300">  
  2.       
  3.         "Console" target="SYSTEM_OUT">  
  4.             "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />  
  5.           
  6.   
  7.         "databaseAppender">  
  8.             "test" collectionName="errorlog"  
  9.                 server="localhost" port="27017" />  
  10.           
  11.       
  12.   
  13.       
  14.         "mongolog" level="trace" additivity="false">  
  15.             "databaseAppender" />  
  16.           
  17.         "error">  
  18.             "Console" />  
  19.           
  20.       
  21.   

4 输出到Flume

Flume配置(flume-conf.properties)

[java]  view plain  copy
 print ?
  1. agent1.sources=source1   
  2. agent1.sinks=sink1   
  3. agent1.channels=channel1   
  4.   
  5. agent1.sources.source1.type=avro  
  6. agent1.sources.source1.channels=channel1  
  7. agent1.sources.source1.bind=0.0.0.0  
  8. agent1.sources.source1.port=41414  
  9.   
  10. agent1.sinks.sink1.type=file_roll   
  11. agent1.sinks.sink1.sink.directory=D:/log  
  12. agent1.sinks.sink1.channel=channel1  
  13. agent1.sinks.sink1.sink.rollInterval=86400  
  14. agent1.sinks.sink1.sink.batchSize=100  
  15. agent1.sinks.sink1.sink.serializer=text  
  16. agent1.sinks.sink1.sink.serializer.appendNewline = false  
  17.   
  18. agent1.channels.channel1.type=file   
  19. agent1.channels.channel1.checkpointDir=D:/log/checkpoint   
  20. agent1.channels.channel1.dataDirs=D:/log/data  
启动Flume(注:测试环境为windows)
[java]  view plain  copy
 print ?
  1. flume-ng.cmd agent --conf ../conf/ --conf-file ../conf/flume-conf.properties -name agent1  
添加依赖:

[java]  view plain  copy
 print ?
  1.   
  2.     org.apache.logging.log4j  
  3.     log4j-flume-ng  
  4.     2.5  
  5.   
配置文件:

[java]  view plain  copy
 print ?
  1. "WARN" monitorInterval="300">  
  2.       
  3.         "eventLogger" compress="false">  
  4.             "127.0.0.1" port="41414" />  
  5.             "18060" includeMDC="true" appName="MyApp" />  
  6.           
  7.       
  8.       
  9.         "trace">  
  10.             "eventLogger" />  
  11.           
  12.       
  13.  

你可能感兴趣的:(JAVA)