Score:0

how to configure a different catalina.out log for each deployed WAR built with Grails on tomcat

br flag

Environment:

  • OS: Ubuntu 20.04 LTS
  • JDK: OpenJDK 11.0.12
  • Tomcat: 9.0.68
  • test war: gtest.war, built with Grails 5.2.4

There are two WAR files built with Grails in my tomcat9.

I want to configure different catalina.out log files for each deployed WAR on tomcat.

I know there is another similar topic how to configure a different catalina.out log for each deployed WAR on tomcat.

The approach in above topic is the same as the official page in Logging in Tomcat.

I followed the approach describing in above pages but all don't work.

BootStrap.groovy is:

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 = {
    }
}

The logback.xml in gtest.war is:

<?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 is:

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

The content of /opt/apache-tomcat-9.0.68/conf/logging.oproperties is below:

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

In /opt/apache-tomcat-9.0.68/logs, the log files are:

-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

And all the output of BootStrap.groovy is still in catalina.out, not in gtest_console.2022-10-11.log, nor in gtest_war.2022-10-11.log.

The content of gtest_war.2022-10-11.log is below:

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'

So, anyone could give the correct instruction? Thanks.

Romeo Ninov avatar
in flag
Check this answer: https://stackoverflow.com/a/42586742/2908599
wureka avatar
br flag
@RomeoNinov I tried the approach you gave. The result is worse because the log file `my_application.log` doesn't appear in the tomcat's log folder. Grails use logback as its logging aproach. Does this matter ?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.