logging

Conditional logging

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: May 16th, 2013
0 584 1 minute read

This is an example of how to use Conditional logging. Using conditional logging in a Class means that we check theLevel for which theLogger is enabled before we log a message to that level. We have implemented a Class that uses a logger. The Class consists of a simple method that uses the logger to log messages after checking the logging level. The basic steps of the example are:

  • Create a Class and create a new logger instance for the Class.
  • Create a new instance of the class and invoke its method. The method has a simple functionality.
  • In the beggining and at the end of the method check if the logger level is set to INFO. If so, use theinfo(String msg) API method to log a message.

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;import java.util.Date; public class ConditionalLoggingExample {    private Logger logger = Logger.getLogger(ConditionalLoggingExample.class.getName());     public static void main(String[] args) {      ConditionalLoggingExample example = new ConditionalLoggingExample();  example.Method();    }      public void Method() {  // Check if the logging level before enter into the log  if (logger.isLoggable(Level.INFO)) {logger.info("Entering executeMethod() at : " + new Date());  }   // Method functionality  for (int i = 0; i< 5; i++) {for (int j = 0; j< 5; j++) {    System.out.print(i + j + " ");}System.out.println("");  }   if (logger.isLoggable(Level.INFO)) {logger.info("Exiting executeMethod() at  : " + new Date());  }    }}

Output:

Αυγ 12, 2012 1:45:55 ΜΜ com.javacodegeeks.snippets.core.ConditionalLoggingExample MethodINFO: Entering executeMethod() at : Sun Aug 12 13:45:55 EEST 20120 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 Αυγ 12, 2012 1:45:55 ΜΜ com.javacodegeeks.snippets.core.ConditionalLoggingExample MethodINFO: Exiting executeMethod() at  : Sun Aug 12 13:45:55 EEST 2012

 
This was an example of how to use Conditional logging 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 Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: May 16th, 2013
0 584 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Log method call

November 11th, 2012
Bipartite Graph

Set logger log level

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.