Maleen Abewardana
Maleen Abewardana

Reputation: 14572

Change log4j log level dynamically without configureAndWatch

I know that, configureAndWatch is a handy way of changing log levels dynamically in a tomcat application. But instead of doing that, is there another way of doing it by configuring tomcat to log using log4j?

In apache docs, it says

The steps described in this section are needed when you want to reconfigure Tomcat to use Apache log4j for its own logging. These steps are not needed if you just want to use log4j in your own web application.

But can't we use this same configuration to log our application logs? (I think if I put relevant jars and property file into lib folder will work. But i was unable get it right yet.)

Upvotes: 0

Views: 649

Answers (1)

Paul Vargas
Paul Vargas

Reputation: 42060

You can use a servlet for change programmatically the level. e.g.:

String logLevel = request.getParameter("level");
Logger root = Logger.getRootLogger();

if ("DEBUG".equalsIgnoreCase(level)) {
    root.setLevel(Level.DEBUG);
} else if ("INFO".equalsIgnoreCase(level)) {
    root.setLevel(Level.INFO);
} else if ("WARN".equalsIgnoreCase(level)) {
    root.setLevel(Level.WARN);
} else if ("ERROR".equalsIgnoreCase(level)) {
    root.setLevel(Level.ERROR);
} else if ("FATAL".equalsIgnoreCase(level)) {
    root.setLevel(Level.FATAL);
}

Upvotes: 1

Related Questions