Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting logging level through CLI argument? Java

Quick question,

Looking through these examples which statically assign the logging level of a program

http://www.onjava.com/pub/a/onjava/2002/06/19/log.html?page=2

http://www.vogella.com/articles/Logging/article.html

How can I dynamically set the logging level through an argument when I call my Java application

e.g.

java -jar myprogram.jar FINE

Meaning that instead of this code

 logger.setLevel(Level.INFO);

I could have something like

logger.setLevel(args[0]);

When researching the documentation there doesnt appear to be away to set the log level with a string.

http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html

like image 463
tomaytotomato Avatar asked Mar 03 '26 11:03

tomaytotomato


2 Answers

You can use builtin method Level.parse(String), which parses a level name string into a Level.

The argument string may consist of either a level name or an integer value.

For example:

  • "SEVERE"
  • "INFO"
  • "1000"

You could create a Map of String and Level and refer to that:

logger.setLevel(logmap.get(args[0]));
like image 36
TeTeT Avatar answered Mar 06 '26 01:03

TeTeT