logging

Set logger log level

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: May 16th, 2013
0 289 1 minute read

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

  • Create a newLogger instance.
  • Set the log level toLevel.INFO, with thesetLevel(Level newLevel) API method.
  • Log aSEVERE message, with thesevere(String msg) API method. The message will be logged, since the level is set toINFO.
  • Set the log level toLevel.SEVERE, with thesetLevel(Level newLevel) API method.
  • Log aWARNING message, with thewarning(String msg) API method. The message will not be logged, since the level is set toSEVERE.
  • Set the log level toLevel.OFF, with thesetLevel(Level newLevel) API method, to turn the logger off. Now no messages will be logged.
  • Set the log level toLevel.ALL, with thesetLevel(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 mainSEVERE: This message will be logged.Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample mainINFO: Information message.Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample mainWARNING: Warning message.Αυγ 12, 2012 1:03:32 ΜΜ com.javacodegeeks.snippets.core.LogLevelExample mainSEVERE: Severe message

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

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: May 16th, 2013
0 289 1 minute read
Photo of 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 atJava Code Geeks.

Related Articles

Bipartite Graph

Conditional logging

November 11th, 2012
Bipartite Graph

Log method call

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.