Home » Core Java » util » logging » Set logger log level

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Set logger log level

With this example we are going to demonstrate how to set a Logger‘s log level. The Level defines a set of standard logging levels that can be used to control logging output. The standard levels are provided at the Level API. In short, to set logger log level you should:

  • Create a new Logger instance.
  • Set the log level to Level.INFO, with the setLevel(Level newLevel) API method.
  • Log a SEVERE message, with the severe(String msg) API method. The message will be logged, since the level is set to INFO.
  • Set the log level to Level.SEVERE, with the setLevel(Level newLevel) API method.
  • Log a WARNING message, with the warning(String msg) API method. The message will not be logged, since the level is set to SEVERE.
  • Set the log level to Level.OFF, with the setLevel(Level newLevel) API method, to turn the logger off. Now no messages will be logged.
  • Set the log level to Level.ALL, with the setLevel(Level newLevel) API method, to turn the logger on. Now all messages will be logged.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;


import java.util.logging.Logger;
import java.util.logging.Level;
 
public class LogLevelExample {
    
    // Create logger instance
    private static Logger logger = Logger.getLogger(LogLevelExample.class.getName());
 
    public static void main(String[] args) {

  

  // Set the log level to Level.INFO

  logger.setLevel(Level.INFO);

  logger.severe("This message will be logged.");
 

  // Set the log level to Level.SEVERE

  logger.setLevel(Level.SEVERE);

  logger.warning("This message won't be logged.");
 

  // Turn of the log

  logger.setLevel(Level.OFF);

  logger.info("All log is turned off.");
 

  // Turn the logger on

  logger.setLevel(Level.ALL);

  logger.info("Information message.");

  logger.warning("Warning message.");

  logger.severe("Severe message.");
    }
}

Output:

Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample main
SEVERE: This message will be logged.
Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample main
INFO: Information message.
Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample main
WARNING: Warning message.
Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample main
SEVERE: Severe message

 
This was an example of how to set a logger’s log Level in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of