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

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

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

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


    
        D:/logs
    
    
        
            
        

        
            
                
                
            
            
            
                
                
            
            
        
        
        
            
                
                
            
            
            
                
                
            
            
        
        
        
            
                
            
            
            
                
                
            
            
        
    

    
        
            
            
            
            
        
    

测试代码:

public static void main(String[] args) {  
    Logger logger = LogManager.getLogger(Client.class);  
    logger.trace("trace level");  
    logger.debug("debug level");  
    logger.info("info level");  
    logger.warn("warn level");  
    logger.error("error level");  
    logger.fatal("fatal level");  
} 

2 异步写日志

配置文件:

  
      
        D:/logs  
        mylog  
      
  
      
          
              
          
          
              
              
                  
                  
              
              
          
          
              
          
      
  
      
          
              
          
          
              
          
      
  

测试代码:

public static void main(String[] args) {  
    Logger logger = LogManager.getLogger("asynclog");  
    logger.trace("trace level");  
    logger.debug("debug level");  
    logger.info("info level");  
    logger.warn("warn level");  
    logger.error("error level");  
    logger.fatal("fatal level");  
}  

3 输出到MongoDB

添加依赖:

  
    org.apache.logging.log4j  
    log4j-nosql  
    2.5  
  
  
    org.mongodb  
    mongo-java-driver  
    3.2.2  
  

配置文件:

  
      
          
              
          
  
          
              
          
      
  
      
          
              
          
          
              
          
      
  

4 输出到Flume

Flume配置(flume-conf.properties)

agent1.sources=source1   
agent1.sinks=sink1   
agent1.channels=channel1   
  
agent1.sources.source1.type=avro  
agent1.sources.source1.channels=channel1  
agent1.sources.source1.bind=0.0.0.0  
agent1.sources.source1.port=41414  
  
agent1.sinks.sink1.type=file_roll   
agent1.sinks.sink1.sink.directory=D:/log  
agent1.sinks.sink1.channel=channel1  
agent1.sinks.sink1.sink.rollInterval=86400  
agent1.sinks.sink1.sink.batchSize=100  
agent1.sinks.sink1.sink.serializer=text  
agent1.sinks.sink1.sink.serializer.appendNewline = false  
  
agent1.channels.channel1.type=file   
agent1.channels.channel1.checkpointDir=D:/log/checkpoint   
agent1.channels.channel1.dataDirs=D:/log/data  

启动Flume(注:测试环境为windows)

flume-ng.cmd agent --conf ../conf/ --conf-file ../conf/flume-conf.properties -name agent1  

添加依赖:

  
    org.apache.logging.log4j  
    log4j-flume-ng  
    2.5  
 

配置文件:

  
      
          
              
              
          
      
      
          
              
          
      
  

转自:http://blog.csdn.net/autfish/article/details/51244787

你可能感兴趣的:(详解log4j2(下) - Async/MongoDB/Flume Appender 按日志级别区分文件输出)