如何为在 tomcat 上使用 Grails 构建的每个部署的 WAR 配置不同的 catalina.out 日志

如何为在 tomcat 上使用 Grails 构建的每个部署的 WAR 配置不同的 catalina.out 日志

环境:

  • 操作系统:Ubuntu 20.04 LTS
  • JDK:OpenJDK 11.0.12
  • Tomcat:9.0.68
  • 测试战:gtest.war,使用 Grails 5.2.4 构建

我的tomcat9中有两个使用Grails构建的WAR文件。

我想为 tomcat 上部署的每个 WAR 配置不同的 catalina.out 日志文件。

我知道还有另一个类似的话题如何为 tomcat 上部署的每个 WAR 配置不同的 catalina.out 日志

上述主题中的方法与官方页面中的方法相同Tomcat 中的日志

我按照以上页面中描述的方法进行操作,但都不起作用。

BootStrap.groovy是:

package gtest

import grails.core.GrailsApplication
import grails.util.Environment

class BootStrap {
    GrailsApplication grailsApplication
    String appVersion
    String grailsVersion
    def init = { servletContext ->
        appVersion = grailsApplication.metadata.getApplicationVersion()
        grailsVersion = grailsApplication.metadata.getGrailsVersion()
        log.info("init(): ************************************************************")
        log.info("init(): ******     Grails ver ${grailsVersion}                          *******")
        log.info("init(): ******     WAR Build environment:${Environment.current.toString().padRight(12, ' ')}        *******")
        log.info("init(): ******     GTest ver ${appVersion} begins to init     ******")
        log.info("init(): ************************************************************")
        log.info("init(): Locale:" + Locale.default)
        log.info("")
        log.info("")
        log.info("")
        println("println(): ************************************************************")
        println("println(): ******     Grails ver ${grailsVersion}                          *******")
        println("println(): ******     WAR Build environment:${Environment.current.toString().padRight(12, ' ')}        *******")
        println("println(): ******     Zebulun Backoffice ver ${appVersion} begins to init     ******")
        println("println(): ************************************************************")
        println("println(): Locale:" + Locale.default)

    }
    def destroy = {
    }
}

gtest.war 中的内容logback.xml为:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex</pattern>
        </encoder>
    </appender>

    <root level="error">
        <appender-ref ref="STDOUT" />
    </root>
    <logger name="gtest" level="TRACE" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
</configuration>

gtest.war/WEB-INF/classes/logging.properties是:

handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

org.apache.juli.FileHandler.level = FINE
org.apache.juli.FileHandler.directory = ${catalina.base}/logs
org.apache.juli.FileHandler.prefix = gtest_war.

java.util.logging.ConsoleHandler.level = FINE

java.util.logging.ConsoleHandler.formatter = java.util.logging.OneLineFormatter

内容/opt/apache-tomcat-9.0.68/conf/logging.oproperties如下:

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler, 5gtest.org.apache.juli.AsyncFileHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90
1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.maxDays = 90
2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8

3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.maxDays = 90
3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8

4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
4host-manager.org.apache.juli.AsyncFileHandler.maxDays = 90
4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8

5gtest.org.apache.juli.AsyncFileHandler.level = FINE
5gtest.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
5gtest.org.apache.juli.AsyncFileHandler.prefix = gtest_console.
5gtest.org.apache.juli.AsyncFileHandler.maxDays = 2
5gtest.org.apache.juli.AsyncFileHandler.encoding = UTF-8

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/gtest].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/gtest].handlers = 5gtest.org.apache.juli.AsyncFileHandler



# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

# To see debug messages in TldLocationsCache, uncomment the following line:
#org.apache.jasper.compiler.TldLocationsCache.level = FINE

# To see debug messages for HTTP/2 handling, uncomment the following line:
#org.apache.coyote.http2.level = FINE

# To see debug messages for WebSocket handling, uncomment the following line:
#org.apache.tomcat.websocket.level = FINE

在 中/opt/apache-tomcat-9.0.68/logs,日志文件包括:

-rw-r-----  1 tomcat tomcat 202187 Oct 11 08:12 catalina.2022-10-11.log
-rw-r-----  1 tomcat tomcat 909787 Oct 11 08:12 catalina.out
-rw-r-----  1 tomcat tomcat      0 Oct 11 08:02 gtest_console.2022-10-11.log
-rw-r-----  1 tomcat tomcat   6817 Oct 11 08:12 gtest_war.2022-10-11.log
-rw-r-----  1 tomcat tomcat      0 Oct 11 05:25 host-manager.2022-10-11.log
-rw-r-----  1 tomcat tomcat  14954 Oct 11 07:46 localhost.2022-10-11.log
-rw-r-----  1 tomcat tomcat  99075 Oct 11 08:50 localhost_access_log.2022-10-11.txt
-rw-r-----  1 tomcat tomcat  97841 Oct 11 08:12 manager.2022-10-11.log

并且的所有输出BootStrap.groovy仍然在 中catalina.out,不在 中gtest_console.2022-10-11.log,也不在 中gtest_war.2022-10-11.log

内容gtest_war.2022-10-11.log如下:

11-Oct-2022 08:02:40.270 INFO [main] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext
11-Oct-2022 08:02:45.735 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Oct-2022 08:02:46.181 INFO [main] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath
11-Oct-2022 08:02:51.254 INFO [main] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
11-Oct-2022 08:03:05.770 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [2,372] milliseconds.
11-Oct-2022 08:03:05.779 INFO [main] org.apache.catalina.core.ApplicationContext.log Initializing Spring GrailsDispatcherServlet 'grailsDispatcherServlet'
11-Oct-2022 08:11:46.635 INFO [main] org.apache.catalina.core.ApplicationContext.log Destroying Spring FrameworkServlet 'grailsDispatcherServlet'
11-Oct-2022 08:11:46.635 INFO [main] org.apache.catalina.core.ApplicationContext.log Oct 11, 2022 8:11:46 AM org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'grailsDispatcherServlet'

11-Oct-2022 08:11:46.636 INFO [main] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext
11-Oct-2022 08:11:51.915 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Oct-2022 08:11:52.321 INFO [main] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath
11-Oct-2022 08:11:57.473 INFO [main] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
11-Oct-2022 08:12:12.029 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [2,441] milliseconds.
11-Oct-2022 08:12:12.038 INFO [main] org.apache.catalina.core.ApplicationContext.log Initializing Spring GrailsDispatcherServlet 'grailsDispatcherServlet'

那么,有人能给出正确的指示吗?谢谢。

相关内容