Home » Java » Enterprise Java » How to change logging level in runtime

About Gal Levinsky

Graduated B.s.c Information System Engineering on BGU University (2004). Been working on large cloud based ERP application in SAP for 7 years and as a development group manager in affiliation company. Co-founded few startups in the domain of social web.

How to change logging level in runtime

Changing the log logging level in runtime is important mainly in production environment where you might want to have debug logging for limited amount of time.

Well, changing the root logger is very simple – assuming you have an input parameter with the wanted logging level simply get the root logger and set by the input logging level, such as:

 
 
 
 

Logger root = Logger.getRootLogger();

//setting the logging level according to input
if ('FATAL'.equalsIgnoreCase(logLevel)) {
    root.setLevel(Level.FATAL);
}else if ('ERROR'.equalsIgnoreCase(logLevel)) {
    root.setLevel(Level.ERROR);
}

However – the common case is that we maintain log instance per class, for example:

class SomeClass{

//class level logger
static Logger logger - Logger.getLogger(SomeClass.class);
}

and setting the root logger is not enough, since the class level logger will not be affected.

The trick is to remember get all the loggers in the system and change their logging level too.
For example:

Logger root = Logger.getRootLogger();
Enumeration allLoggers = root.getLoggerRepository().getCurrentCategories();

//set logging level of root and all logging instances in the system
if ('FATAL'.equalsIgnoreCase(logLevel)) {
    root.setLevel(Level.FATAL);
    while (allLoggers.hasMoreElements()){
        Category tmpLogger = (Category) allLoggers.nextElement();
        tmpLogger .setLevel(Level.FATALLogging, Enterprise Java, Log4j);
    }
}else if ('ERROR'.equalsIgnoreCase(logLevel)) {
    root.setLevel(Level.ERROR);
    while (allLoggers.hasMoreElements()){
        Category tmpLogger = (Category) allLoggers.nextElement();
        tmpLogger .setLevel(Level.ERROR);
    }
}

So just wrap it up in a service class and call it from your controller, with a dynamic logLevel String parameter which represent the logging level you wish to set your system to.

If any of you need the complete solution, please let me know.

The basic approach is in this link.

Reference: How to change logging level in runtime from our JCG partner Gal Levinsky at the Gal Levinsky’s blog blog.

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 ....

 

2 comments

  1. Hi,

    I am in need of a solution where i need to change my java logging(java.util.logging) levels at run time.
    I am having an application which is being deployed on tomcat.
    In my application’s classes, each class has its own Logger instance.

    I want to write a method through which i can change the logger level from Warning to ALL and from ALL to warning at runtime without being changing the logging.properties file and restarting tomcat.

    Please help me with this solution of yours.
    I am waiting.
    If possible can you mail me the solution on 2408.santosh@gmail.com

  2. Hi mate,
    There is an open source pluggable Java web library for runtime logging level configuration (http://la-team.github.io/light-logging-configurer/).
    Check it out!

Leave a Reply

Your email address will not be published. Required fields are marked *

*


× four = 24

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close