18

I am using Tomcat 7.0.28, running under OpenJDK 1.7 on Ubuntu, and am trying to modify the formatting string used by java.util.logging.SimpleFormatter. According to the Javadocs for that class, I can specify the property java.util.logging.SimpleFormatter.format to change the format. And indeed, when I run my webapp in Eclipse and change this property in my logging.properties file, it works.

However, when I deploy the app to Tomcat, this property does not seem to have any effect. I am confident that my properties file is being read correctly, as other changes that I make to it do indeed take effect (I'm reading the properties in from a file using

LogManager.getLogManager().readConfiguration(new FileInputStream(file))

where file is configured via a parameter in my web.xml file. I've tried putting the file in WEB-INF/classes/logging.properties, with no change in behavior.

The Javadocs for SimpleFormatter specify that if both a properties file and a system property specify the formatting string, the system property takes precedence. I have verified that the system property is not set

context.log ("Formatting system property is " + System.getProperty("java.util.logging.SimpleFormatter.format"));

in a ServletContextListener.contextInitialized method.

Here's my logging properties file in full

handlers=java.util.logging.ConsoleHandler

#  Default logging level for root logger
.level=FINE

#  Set the level for the ConsoleHandler
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tr] %3$s %4$s:  %5$s %n

I've tried everything I can think of, including modifying logging.properties in both the TOMCAT/conf and JRE_HOME/lib directory. Nothing seems to make any difference.

ryanmcfall
  • 585
  • 1
  • 6
  • 20
  • 4
    Facing same problem. Here's a guess: The `java.util.logging.SimpleFormatter.format` property feature was introduced in Java 7. I know that Tomcat basically uses a slightly modified version of `java.util.logging`. I wonder if that is the reason. – peterh Sep 26 '12 at 11:02
  • Yes nolan6000, I had the same problem and it was related with the JDK version that I was using. Thanks. – Miguel Gamboa May 03 '13 at 20:07

5 Answers5

9

Thanks to info in the bug report pointed out by nolan6000 I finally got this working with tomcat-juli.

Instead of:

java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n

it has to be:

1catalina.java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
rhinmass
  • 174
  • 1
  • 7
  • I'm not able to get this to work on Tomcat 7.0.22. More here https://stackoverflow.com/questions/49322523/tomcat-logging-properties-formatter-not-taking-effect – JackTheKnife Mar 16 '18 at 15:27
8

java.util.logging.SimpleFormatter.format is indeed documented to be settable in either logging.properties -which does not work for me either- or as a command line argument (java option).

The command line argument appears to work for me. Because the $JAVA_OPTS variable is going through so much tampering and ends up in eval, this is how I solved it (on Debian, java 1.7.0_07, apache-tomcat-7.0.30)

$CATALINA_HOME/bin/catalina.sh (line 230):

JAVA_OPTS="$JAVA_OPTS \"-Djava.util.logging.SimpleFormatter.format=%1\\\$tY-%1\\\$tm-%1\\\$td %1\\\$tH:%1\\\$tM:%1\\\$tS.%1\\\$tL %4\\\$s %3\\\$s %5\\\$s%6\\\$s%n\""
i_g_r
  • 101
  • 1
  • 3
7

You may be witnessing this bug.

The bug is fixed from Tomcat version 7.0.41 onwards as well as 6.0.38 onwards.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
peterh
  • 18,404
  • 12
  • 87
  • 115
3

Don't know if this will solve your problem, but it may be worth a try. I was seeing the same behavior, although in my case I was doing the logging setup programmatically instead of using properties. Turns out that the property value for java.util.logging.SimpleFormatter.format needs to be set BEFORE constructing the (in my case) FileHandler. I was setting it after constructing the FileHandler, but before constructing SimpleFormatter. I wonder if, in your properties file, defining java.util.logging.SimpleFormatter.format BEFORE defining any of the Handler properties will solve the problem.

Steve
  • 39
  • 2
3

Using a formatter pattern in default Tomcat 8 JULI logger needs this, you may put arguments to a global $tomcat/conf/logging.properties or webapp specific $tomcat/webapps/myapp/WEB-INF/classes/logging.properties file.

This is my global file where have also disabled a manager-webapp logging files. Logging line is:
2015-09-23 17:32:11 INFO org.apache.catalina.startup.Catalina Server startup in 1028 ms

#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, 2localhost.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

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

# formatter attributes = date, source, logger, level, message, thrown
java.util.logging.SimpleFormatter.format = %1$tF %1$tT %4$s %3$s %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.encoding = UTF-8
1catalina.org.apache.juli.AsyncFileHandler.formatter = java.util.logging.SimpleFormatter
#1catalina.org.apache.juli.AsyncFileHandler.bufferSize = 2048

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.encoding = UTF-8
2localhost.org.apache.juli.AsyncFileHandler.formatter = java.util.logging.SimpleFormatter
#2localhost.org.apache.juli.AsyncFileHandler.bufferSize = 2048

#3manager.org.apache.juli.AsyncFileHandler.level = FINE
#3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
#3manager.org.apache.juli.AsyncFileHandler.prefix = manager.

#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.

java.util.logging.ConsoleHandler.level = FINE
#java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
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

# 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 lines
#org.apache.jasper.compiler.TldLocationsCache.level = FINE
#org.apache.jasper.servlet.TldScanner.level=FINE
Whome
  • 10,181
  • 6
  • 53
  • 65
  • it seems to work for me in the global file , but when i specify the format in web-app specific file, it does not seem to work – gaurav5430 Jul 16 '17 at 10:15
  • 1
    Thanks @Whome! It works for me, but only with the java.util.logging.SimpleFormatter. With the org.apache.juli.OneLineFormatter doesn't :( – Gaucho Feb 22 '18 at 17:14