How to configure the jdk14 logging's pattern
Asked Answered
H

2

10

I guess I can chnage pattern by adding the line java.util.logging.ConsoleHandler.pattern, however where to check the pattern information like %u %h etc?

Hand answered 24/3, 2011 at 8:54 Comment(0)
S
8

Edit: The below was written at the time for Java 6. For 7 and later, refer to David's answer below.

AFAIK there is no such property. There is a java.util.logging.FileHandler.pattern but this is to set the pattern of the output filename, not of the logging format.

The way you configure the output format in the util logging API is by setting the Formatter. By default, a SimpleFormatter is attached to your ConsoleHandler. This formatter simply hardcodes the pattern and doesn't allow you to set it.

If you need a different output format, you'll have to either implement your own Formatter, or use a different logging framework, such as logback.

Saccule answered 24/3, 2011 at 9:16 Comment(0)
A
30

This question has already been answered by somebody, but i want to provide some new information:

Since Java 7 it is possible to configure the output pattern for log messages with the SimpleFormatter.

You can use this property in your logging properties file:

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

If you need more information on the pattern syntax have a look here: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

The digits in the property value above refer to parameters provided to the formatter. Please refer to the official Java docs for more information: http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html

Example configuration file logging.properties:

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Pattern works since Java 7
java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

# Configure logging levels
# Available log levels are:
# OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL

# root logger
.level = WARNING

# child logger
org.example.level = ALL

When you call your java program you can specify your configuration file as parameter:

java -Djava.util.logging.config.file=logging.properties -jar myProgram.jar
Ardolino answered 22/2, 2013 at 12:12 Comment(4)
I also had to set root level for this to work .level = ALL.Familiarize
I have tried to go with those directions but cannot get commons-logging.properties to work in my case. Any Clue?Concise
Sorry, Commons Logging is a different framework. This answer is specific to java.util.logging.Ardolino
Set system property (-D...) before -jar statement. java -Djava.util.logging.config.file=logging.properties -jar myProgram.jar docs.oracle.com/javase/8/docs/technotes/tools/windows/java.htmlColobus
S
8

Edit: The below was written at the time for Java 6. For 7 and later, refer to David's answer below.

AFAIK there is no such property. There is a java.util.logging.FileHandler.pattern but this is to set the pattern of the output filename, not of the logging format.

The way you configure the output format in the util logging API is by setting the Formatter. By default, a SimpleFormatter is attached to your ConsoleHandler. This formatter simply hardcodes the pattern and doesn't allow you to set it.

If you need a different output format, you'll have to either implement your own Formatter, or use a different logging framework, such as logback.

Saccule answered 24/3, 2011 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.