Java logging set default level

Java Logging: Configuration

The initialization of the configuration is taken care of by the java.util.logging.LogManager class.

Configuration Class

You can use a Java class to configure the Java Logging API. You do so by specifying the name of the class in the JVM parameter java.util.logging.config.class . It is the constructor of that class that should load the configuration and apply it to the Logger ‘s in the hierarchy.

Configuration File

If no configuration class is specified, you can instead specify a configuration file (but no configuration class can be specified then!).

The Java Logging API has a default logging configuration file located at «lib/logging.properties» , inside the JRE directory. If you edit this file, you edit the default logging settings for the entire JRE, for every program executed. This is most often not what you want to do, though.

Instead, you can set a separate configuration file for your application. You do so by setting the JVM property java.util.logging.config.file to point to this file.

The configuration file is a standard property file as you know them from Java. Inside this property file you can set properties that configure the various Logger ‘s and Handler ‘s used in your application.

Here is a list of properties you can set in the configuration file. You should double check the JavaDoc over time to see if any of this changes (e.g. in a later version of Java than Java 6).

Читайте также:  Head first java ozon
Property Description
handlers A white space or comma separated list of handler class names to be added to the root Logger
config A white space or comma separated list of class names which will be instantiated when the LogManager is initialized. The constructors of these classes can execute arbitrary configuration code.
«logger».handlers Sets the handler classes to use for a given Logger in the hierarchy. Replace the «logger» with a specific name of a Logger in your app (e.g. com.jenkov.web).
«logger».useParentHandlers Tells a given Logger whether it should log to its parents or not (true or false).
«logger».level Tells a given Logger what minimum log level it should log.
java.util.logging.FileHandler.level Sets the default log level for all FileHandler ‘s.
java.util.logging.FileHandler.filter A class name of the Filter to use on all FileHandler ‘s.
java.util.logging.FileHandler.formatter A class name of the Formatter to use on all FileHandler ‘s.
java.util.logging.FileHandler.encoding The encoding to use by all FileHandler ‘s (e.g. UTF-8, UTF-16 etc.).
java.util.logging.FileHandler.limit The approximate amount of bytes to write to a log file, before rotating to a new file.
java.util.logging.FileHandler.count The number of log files to use in the log file rotation.
java.util.logging.FileHandler.append Sets whether or not the FileHandler ‘s should append to an existing file or not (true or false), if an existing log file is found.
java.util.logging.FileHandler.pattern The log file name pattern.
java.util.logging.ConsoleHandler.level Sets the default log level of all ConsoleHandler ‘s.
java.util.logging.ConsoleHandler.filter Sets the Filter to use by all ConsoleHandler ‘s
java.util.logging.ConsoleHandler.formatter Sets the Formatter to use by all ConsoleHandler ‘s.
java.util.logging.ConsoleHandler.encoding Sets the encoding to use by all ConsoleHandler ‘s.
java.util.logging.StreamHandler.level Sets the default log level of all StreamHandler ‘s.
java.util.logging.StreamHandler.filter Sets the Filter to use by all StreamHandler ‘s
java.util.logging.StreamHandler.formatter Sets the Formatter to use by all StreamHandler ‘s.
java.util.logging.StreamHandler.encoding Sets the encoding to use by all StreamHandler ‘s.
java.util.logging.SocketHandler.level Sets the default log level of all SocketHandler ‘s.
java.util.logging.SocketHandler.filter Sets the Filter to use by all SocketHandler ‘s.
java.util.logging.SocketHandler.formatter Sets the Formatter to use by all SocketHandler ‘s.
java.util.logging.SocketHandler.encoding Sets the encoding to use by all SocketHandler ‘s.
java.util.logging.SocketHandler.host Sets the host name of the host to send the log messages to (e.g. jenkov.com).
java.util.logging.SocketHandler.port Sets the port number of of the host to send the log message to (e.g. 9999).
java.util.logging.MemoryHandler.level Sets the default log level of all MemoryHandler ‘s.
java.util.logging.MemoryHandler.filter Sets the Filter to use by all MemoryHandler ‘s.
java.util.logging.MemoryHandler.size The size of the internal LogRecord buffer.
java.util.logging.MemoryHandler.push The push level of messages causing the buffer to be pushed to the target Handler . Defaults to SEVERE.
java.util.logging.MemoryHandler.target The class name of the target Handler .
Читайте также:  Numpy python операции с матрицами

Here is an example configuration file. Not all properties are set to a value. You should do that in your own configuration file, or omit the configuration parameter if it has no value.

Note: Configuration properties are applied in the sequence they are listed in the config file. That means, that you should configure parent Logger ‘s before child Logger ‘s. Otherwise the configuration of the parent Logger will override that of the child Logger .

handlers = java.util.logging.FileHandler config = "logger".handlers = "logger".useParentHandlers = "logger".level = java.util.logging.FileHandler.level = WARNING java.util.logging.FileHandler.filter = java.util.logging.FileHandler.formatter = java.util.logging.FileHandler.encoding = java.util.logging.FileHandler.limit = java.util.logging.FileHandler.count = java.util.logging.FileHandler.append = false java.util.logging.FileHandler.pattern = log.%u.%g.txt java.util.logging.ConsoleHandler.level = WARNING java.util.logging.ConsoleHandler.filter = java.util.logging.ConsoleHandler.formatter = java.util.logging.ConsoleHandler.encoding = java.util.logging.StreamHandler.level = WARNING java.util.logging.StreamHandler.filter = java.util.logging.StreamHandler.formatter = java.util.logging.StreamHandler.encoding = java.util.logging.SocketHandler.level = WARNING java.util.logging.SocketHandler.filter = java.util.logging.SocketHandler.formatter = java.util.logging.SocketHandler.encoding = java.util.logging.SocketHandler.host = java.util.logging.SocketHandler.port = java.util.logging.MemoryHandler.level = WARNING java.util.logging.MemoryHandler.filter = java.util.logging.MemoryHandler.size = java.util.logging.MemoryHandler.push = java.util.logging.MemoryHandler.target =

Источник

Class Level

The Level class defines a set of standard logging levels that can be used to control logging output. The logging Level objects are ordered and are specified by ordered integers. Enabling logging at a given level also enables logging at all higher levels.

Clients should normally use the predefined Level constants such as Level.SEVERE.

  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)

It is possible for third parties to define additional logging levels by subclassing Level. In such cases subclasses should take care to chose unique integer level values and to ensure that they maintain the Object uniqueness property across serialization by defining a suitable readResolve method.

Field Summary

Constructor Summary

Method Summary

Methods declared in class java.lang.Object

Field Details

OFF

OFF is a special level that can be used to turn off logging. This level is initialized to Integer.MAX_VALUE .

SEVERE

SEVERE is a message level indicating a serious failure. In general SEVERE messages should describe events that are of considerable importance and which will prevent normal program execution. They should be reasonably intelligible to end users and to system administrators. This level is initialized to 1000 .

WARNING

WARNING is a message level indicating a potential problem. In general WARNING messages should describe events that will be of interest to end users or system managers, or which indicate potential problems. This level is initialized to 900 .

INFO

INFO is a message level for informational messages. Typically INFO messages will be written to the console or its equivalent. So the INFO level should only be used for reasonably significant messages that will make sense to end users and system administrators. This level is initialized to 800 .

CONFIG

CONFIG is a message level for static configuration messages. CONFIG messages are intended to provide a variety of static configuration information, to assist in debugging problems that may be associated with particular configurations. For example, CONFIG message might include the CPU type, the graphics depth, the GUI look-and-feel, etc. This level is initialized to 700 .

FINE

FINE is a message level providing tracing information. All of FINE, FINER, and FINEST are intended for relatively detailed tracing. The exact meaning of the three levels will vary between subsystems, but in general, FINEST should be used for the most voluminous detailed output, FINER for somewhat less detailed output, and FINE for the lowest volume (and most important) messages. In general the FINE level should be used for information that will be broadly interesting to developers who do not have a specialized interest in the specific subsystem. FINE messages might include things like minor (recoverable) failures. Issues indicating potential performance problems are also worth logging as FINE. This level is initialized to 500 .

FINER

FINER indicates a fairly detailed tracing message. By default logging calls for entering, returning, or throwing an exception are traced at this level. This level is initialized to 400 .

Источник

Java Util Logging — Log Levels

In this example, we will learn how to change Java Util Logging default level to a new value.

To change a log level we must use Logger#setLevel() and Handler#setLevel() .

Examples

Programmatically setting Log Level

We are going to set the log Level to ALL on the root logger/handlers, then we will find out all declared Levels in the Level class via reflection and then we will use the log() method on them. We will also set a custom easy to read log format.

public class LogLevelExample < private static Logger log = Logger.getLogger(LogLevelExample.class.getName()); static < System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT %1$tL] [%4$-7s] %5$s %n"); >public static void main(String[] args) throws Exception < setLevel(Level.ALL); Setlevels = getAllLevels(); int i = 1; for (Level level : levels) < log.log(level, level.getName() + " - " + (i++)); >> public static void setLevel(Level targetLevel) < Logger root = Logger.getLogger(""); root.setLevel(targetLevel); for (Handler handler : root.getHandlers()) < handler.setLevel(targetLevel); >System.out.println("level set: " + targetLevel.getName()); > public static Set getAllLevels() throws IllegalAccessException < ClasslevelClass = Level.class; Set allLevels = new TreeSet<>( Comparator.comparingInt(Level::intValue)); for (Field field : levelClass.getDeclaredFields()) < if (field.getType() == Level.class) < allLevels.add((Level) field.get(null)); >> return allLevels; > >

Output

level set: ALL
[2017-09-21 13:55:24 796] [ALL ] ALL - 1
[2017-09-21 13:55:24 798] [FINEST ] FINEST - 2
[2017-09-21 13:55:24 799] [FINER ] FINER - 3
[2017-09-21 13:55:24 799] [FINE ] FINE - 4
[2017-09-21 13:55:24 799] [CONFIG ] CONFIG - 5
[2017-09-21 13:55:24 799] [INFO ] INFO - 6
[2017-09-21 13:55:24 800] [SEVERE ] SEVERE - 8
[2017-09-21 13:55:24 800] [OFF ] OFF - 9

Setting Level in properties file

We are going to set FINEST level:

src/main/resources/logging.properties

handlers= java.util.logging.ConsoleHandler .level= FINEST java.util.logging.ConsoleHandler.level = FINEST java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s] %5$s %n 
public class LogLevelPropertiesExample < private static Logger log; static < String path = LogLevelPropertiesExample.class .getClassLoader() .getResource("logging.properties") .getFile(); System.setProperty("java.util.logging.config.file", path); log = Logger.getLogger(LogLevelPropertiesExample.class.getName()); >public static void main(String[] args) throws Exception < Setlevels = getAllLevels(); int i = 1; for (Level level : levels) < log.log(level, level.getName() + " - " + (i++)); >> . >

Output

[2017-09-20 23:49:20] [FINEST ] FINEST - 2 
[2017-09-20 23:49:20] [FINER ] FINER - 3
[2017-09-20 23:49:20] [FINE ] FINE - 4
[2017-09-20 23:49:20] [CONFIG ] CONFIG - 5
[2017-09-20 23:49:20] [INFO ] INFO - 6
[2017-09-20 23:49:20] [WARNING] WARNING - 7
[2017-09-20 23:49:20] [SEVERE ] SEVERE - 8
[2017-09-20 23:49:20] [OFF ] OFF - 9

The default log Level

The default level is INFO. If we don’t set the level in either of above examples then output would be:

[2017-09-20 23:51:45 801] [INFO ] INFO - 6 [2017-09-20 23:51:45 834] [WARNING] WARNING - 7 [2017-09-20 23:51:45 834] [SEVERE ] SEVERE - 8 [2017-09-20 23:51:45 834] [OFF ] OFF - 9 

Example Project

Dependencies and Technologies Used:

Источник

Оцените статью