Converting log4j.properties file for Tomcat to work with Log4j2

后端 未结 3 1139
庸人自扰
庸人自扰 2021-02-03 13:18

I\'m trying to configure Tomcat 8 to use Log4j2 for logging.

I\'ve found this reference for Logging in Tomcat using Log4j. It provides a sample log4j.properties file th

3条回答
  •  你的背包
    2021-02-03 13:49

    After asking this question, I spent some more time with setting up log4j2, and this is the log4j2.xml file I came up with. It mimics the configuration described in Logging in Tomcat using Log4j. It uses multiple loggers to route messages to separate log files.

    
    
      
        ${sys:catalina.base}/logs
        %d [%t] %-5p %c- %m%n
      
      
        
          
        
        
          
          
            
            
          
          
        
        
          
          
            
            
          
          
        
        
          
          
            
            
          
          
        
        
          
          
            
            
          
          
        
      
      
        
          
        
        
          
        
        
          
        
        
          
        
      
    
    

    However, getting this to work properly required getting a bunch of other files configured correctly. The tomcat 7 internal logging with log4j2.xml posting from Paramita Banerjee was helpful with this.

    This file goes in CATALINA_HOME/bin/:

    • tomcat-juli.jar

    If you're pulling this from the Maven repository, you'll get a file named something like tomcat-extras-juli-8.0.15.jar (the current version when I wrote this). However, it needs to be renamed to tomcat-juli.jar – the Tomcat setup scripts use that name in setting up the CLASSPATH.

    These files go in CATALINA_HOME/lib/:

    • commons-logging-1.2.jar
    • log4j-1.2-api-2.1.jar
    • log4j-api-2.1.jar
    • log4j-core-2.1.jar
    • log4j-jcl-2.1.jar
    • log4j-jul-2.1.jar
    • log4j-web-2.1.jar
    • tomcat-juli-adapters-8.0.15.jar

    log4j-web-2.1.jar may not be needed here (it may just need to be deployed with your web application) – its use is described in Using Log4j 2 in Web Applications. log4j-1.2-api-2.1.jar is needed only if you have applications that use the older log4j 1.2 interface.

    In CATALINA_BASE/conf, I disabled logging.properties.

    I used the following setenv.sh script to define the CLASSPATH and LOGGING_MANAGER environment variables correctly for Tomcat. It goes in either CATALINA_BASE/bin or into CATALINA_HOME/bin. (I put it in CATALINA_HOME/bin.) It is executed by Tomcat's startup scripts if it's present. You might prefer something simpler, but this is in keeping with the style of the startup scripts.

    LOG4J_JARS="log4j-core-2.1.jar log4j-api-2.1.jar log4j-jul-2.1.jar"
    
    # make log4j2.xml available
    if [ ! -z "$CLASSPATH" ] ; then CLASSPATH="$CLASSPATH": ; fi
    CLASSPATH="$CLASSPATH""$CATALINA_BASE"/lib
    
    # Add log4j2 jar files to CLASSPATH
    for jar in $LOG4J_JARS ; do
      if [ -r "$CATALINA_HOME"/lib/"$jar" ] ; then
        CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/lib/"$jar"
      else
        echo "Cannot find $CATALINA_HOME/lib/$jar"
        echo "This file is needed to properly configure log4j2 for this program"
        exit 1
      fi
    done
    
    # use the logging manager from log4j-jul
    LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
    

    As Nick mentioned in his response, there's also the output to the access log. I haven't tried to do anything with that, either.

    I hope others find this useful. In retrospect, it seems pretty straightforward. However, there are a lot of parts that have to be “just right” for it to work, and that was a challenge (at least for a newbie).

提交回复
热议问题