log4j2 xml configuration - Log to file and console (with different levels)

前端 未结 5 1057
情深已故
情深已故 2020-12-24 10:58

I want to do two things:

  1. Log to console with a certain log-level
  2. Log to file with another log-level

Console logging seems to work just

5条回答
  •  没有蜡笔的小新
    2020-12-24 11:29

    Summary others (@basiljames, @daker, @Jay Taylor) answers here:

    my log4j2.xml Configuration

    my case:

    • log4j2 version: 2.13.0

    • log4j2.xml configuration:

    
    
      
        
          
            %d{yyyyMMdd HH:mm:ss} %-5p [%t] %C{2} %F%L - %m%n
          
        
    
        
          
        
      
    
      
        
          
          
        
      
    
    

    Q: how to implement File and Console with different level ?

    A: as official doc also mentioned, the core part:

        
          
          
        
    

    can achieve:

    based on:

    • root level is DEBUG

    set:

    • File level to DEBUG
    • Console level to INFO

    Q: @Stealth Rabbi: so what was the problem?

    A: original main problem is:

    the logger name syntax is wrong

    that is name="filelogger" in:

        
            
        
    

    for normally the logger name is your class name, like

        
            
        
    

    another possible minor problem is:

    log content level is below your file level error, so file created but empty

    when set file level to error, but your log code is low level, such as

    logger.debug("output something");
    

    that is:

    logger lever in code(debug) < file level(error)

    so debug content will NOT output to log file, log file keep empty.

    Q: @Bendemann is it possible to specify a path where I want to save the file if I am running a war file on a tomcat server?

    A: yes. Just set the relative or absolute log file path to File's fileName.

    just like:

      
        
    ...
    

    is ok.

提交回复
热议问题