Java console logging level

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 .

Читайте также:  Include method in html

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 .

Источник

Logger in Java — Java Logging Example

Logger in Java - Java Logging Example

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Logger in Java

logger in java, java logging example Java Logging API was introduced in 1.4 and you can use java logging API to log application messages. In this java logging tutorial, we will learn basic features of Java Logger. We will also look into Java Logger example of different logging levels, Logging Handlers, Formatters, Filters, Log Manager and logging configurations. Java logging, logger in java, java logger example

Java Logger

java.util.logging.Logger is the class used to log application messages in java logging API. We can create java Logger with very simple one line code as;

Logger logger = Logger.getLogger(MyClass.class.getName()); 

Java Logging Levels

  1. SEVERE (highest)
  2. WARNING
  3. INFO
  4. CONFIG
  5. FINE
  6. FINER
  7. FINEST

There are two other logging levels, OFF that will turn off all logging and ALL that will log all the messages. We can set the logger level using following code:

Читайте также:  Html определить мобильное устройство

The logs will be generated for all the levels equal to or greater than the logger level. For example if logger level is set to INFO, logs will be generated for INFO, WARNING and SEVERE logging messages.

Java Logging Handlers

We can add multiple handlers to a java logger and whenever we log any message, every handler will process it accordingly. There are two default handlers provided by Java Logging API.

  1. ConsoleHandler: This handler writes all the logging messages to console
  2. FileHandler: This handler writes all the logging messages to file in the XML format.

We can create our own custom handlers also to perform specific tasks. To create our own Handler class, we need to extend java.util.logging.Handler class or any of it’s subclasses like StreamHandler, SocketHandler etc. Here is an example of a custom java logging handler:

package com.journaldev.log; import java.util.logging.LogRecord; import java.util.logging.StreamHandler; public class MyHandler extends StreamHandler < @Override public void publish(LogRecord record) < //add own logic to publish super.publish(record); >@Override public void flush() < super.flush(); >@Override public void close() throws SecurityException < super.close(); >> 

Java Logging Formatters

Formatters are used to format the log messages. There are two available formatters in java logging API.

  1. SimpleFormatter: This formatter generates text messages with basic information. ConsoleHandler uses this formatter class to print log messages to console.
  2. XMLFormatter: This formatter generates XML message for the log, FileHandler uses XMLFormatter as a default formatter.

We can create our own custom Formatter class by extending java.util.logging.Formatter class and attach it to any of the handlers. Here is an example of a simple custom formatter class.

package com.journaldev.log; import java.util.Date; import java.util.logging.Formatter; import java.util.logging.LogRecord; public class MyFormatter extends Formatter < @Override public String format(LogRecord record) < return record.getThreadID()+"::"+record.getSourceClassName()+"::" +record.getSourceMethodName()+"::" +new Date(record.getMillis())+"::" +record.getMessage()+"\n"; >> 

Logger in Java — Java Log Manager

java.util.logging.LogManager is the class that reads the logging configuration, create and maintains the logger instances. We can use this class to set our own application specific configuration.

LogManager.getLogManager().readConfiguration(new FileInputStream("mylogging.properties")); 

Here is an example of Java Logging API Configuration file. If we don’t specify any configuration, it’s read from JRE Home lib/logging.properties file. mylogging.properties

handlers= java.util.logging.ConsoleHandler .level= FINE # default file output is in user's home directory. java.util.logging.FileHandler.pattern = %h/java%u.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter # Limit the message that are printed on the console to INFO and above. java.util.logging.ConsoleHandler.level = INFO java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter com.journaldev.files = SEVERE 

Here is a simple java program showing usage of Logger in Java.

package com.journaldev.log; import java.io.FileInputStream; import java.io.IOException; import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class LoggingExample < static Logger logger = Logger.getLogger(LoggingExample.class.getName()); public static void main(String[] args) < try < LogManager.getLogManager().readConfiguration(new FileInputStream("mylogging.properties")); >catch (SecurityException | IOException e1) < e1.printStackTrace(); >logger.setLevel(Level.FINE); logger.addHandler(new ConsoleHandler()); //adding custom handler logger.addHandler(new MyHandler()); try < //FileHandler file name with max size and number of log files limit Handler fileHandler = new FileHandler("/Users/pankaj/tmp/logger.log", 2000, 5); fileHandler.setFormatter(new MyFormatter()); //setting custom filter for FileHandler fileHandler.setFilter(new MyFilter()); logger.addHandler(fileHandler); for(int i=0; ilogger.log(Level.CONFIG, "Config data"); > catch (SecurityException | IOException e) < e.printStackTrace(); >> > 

When you will run above java logger example program, you will notice that CONFIG log is not getting printed in file, that is because of MyFilter class.

package com.journaldev.log; import java.util.logging.Filter; import java.util.logging.Level; import java.util.logging.LogRecord; public class MyFilter implements Filter < @Override public boolean isLoggable(LogRecord log) < //don't log CONFIG logs in file if(log.getLevel() == Level.CONFIG) return false; return true; >> 

Also the output format will be same as defined by MyFormatter class.

1::com.journaldev.log.LoggingExample::main::Sat Dec 15 01:42:43 PST 2012::Msg977 1::com.journaldev.log.LoggingExample::main::Sat Dec 15 01:42:43 PST 2012::Msg978 1::com.journaldev.log.LoggingExample::main::Sat Dec 15 01:42:43 PST 2012::Msg979 1::com.journaldev.log.LoggingExample::main::Sat Dec 15 01:42:43 PST 2012::Msg980 

If we don’t add our own Formatter class to FileHandler, the log message will be printed like this.

 2012-12-14T17:03:13 1355533393319 996 com.journaldev.log.LoggingExample INFO com.journaldev.log.LoggingExample main 1 Msg996  

Console log messages will be of following format:

Dec 15, 2012 1:42:43 AM com.journaldev.log.LoggingExample main INFO: Msg997 Dec 15, 2012 1:42:43 AM com.journaldev.log.LoggingExample main INFO: Msg998 Dec 15, 2012 1:42:43 AM com.journaldev.log.LoggingExample main INFO: Msg998 

logger in java, java logging example

Below image shows the final Java Logger example project. That’s all for Logger in Java and Java Logger Example. You can download the project from below link.

Читайте также:  Java command line classpath file

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

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