Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Tomcat Catalina logs in json using default jar(tomact-juli.jar)

I have updated the logging.properties file of tomcat to print the logs in json format. But, the issue is value of "message " has some escape characters, which makes my log invalid json. Please let me know how to escape these characters(:,[,],/) in json using default tomcat-juli.jar and used as a string.

Below is my updated logging.properties file:

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

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

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

java.util.logging.SimpleFormatter.format = {"date" :"%1$tF", "timestamp": "%1$tT" , "loggerlevel": "%4$s", "loggersource": "%3$s" , "message": "%5$s%6$s"}%n

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.formatter = java.util.logging.SimpleFormatter

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.formatter = java.util.logging.SimpleFormatter

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.formatter = java.util.logging.SimpleFormatter

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.formatter = java.util.logging.SimpleFormatter

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# 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
like image 626
Sneha Avatar asked Mar 04 '23 10:03

Sneha


1 Answers

I would suggest you official way (without using middleware):

  1. Download Elastic JUL formatter and logging core libraries: https://mvnrepository.com/artifact/co.elastic.logging/jul-ecs-formatter https://mvnrepository.com/artifact/co.elastic.logging/ecs-logging-core
  2. Put them into tomcat/bin folder
  3. List them in CLASSPATH system property in tomcat/bin/setenv.sh(bat). If no such file - create it with the next content: on Unix - export CLASSPATH="$CLASSPATH:ecs-logging-core.jar:jul-ecs-formatter.jar" on Windows - set CLASSPATH=%CLASSPATH%;ecs-logging-core.jar;jul-ecs-formatter.jar
  4. Edit tomcat/conf/logging.properties in such way:
handlers = java.util.logging.ConsoleHandler
.handlers = java.util.logging.ConsoleHandler
...
java.util.logging.ConsoleHandler.formatter = co.elastic.logging.jul.EcsFormatter

Such configuration will make tomcat to write all events in appropriate Elastic (Logstash) JSON format into console (stdout) which is very suitable when you running in docker or other container.

Documentation https://www.elastic.co/guide/en/ecs-logging/java/1.x/setup.html

like image 179
Yan Pak Avatar answered May 08 '23 06:05

Yan Pak