Selective logging

T

tnorgd

Hi,

I am using java.util.logging. I would like to be able to selectively
change a log-level in a desired branch of code, e.g.: in a class
my.package.util.* or in my.package.gui.*. I would like to do this as a
command line switch specified when a program is started. I would
prefer to avoid editing a logger config file. I expect it is quite
easy, but no good solution comes to my mind after reading API for
java.util.logging.LogManager. Could you give me some clues?

Best,
Dominik
 
A

Arne Vajhøj

tnorgd said:
I am using java.util.logging. I would like to be able to selectively
change a log-level in a desired branch of code, e.g.: in a class
my.package.util.* or in my.package.gui.*. I would like to do this as a
command line switch specified when a program is started. I would
prefer to avoid editing a logger config file. I expect it is quite
easy, but no good solution comes to my mind after reading API for
java.util.logging.LogManager. Could you give me some clues?

Logger.getLogger(loggername).setLevel(loglevel);

Arne
 
D

Donkey Hottie

tnorgd said:
Hi,

I am using java.util.logging. I would like to be able to
selectively change a log-level in a desired branch of
code, e.g.: in a class my.package.util.* or in
my.package.gui.*. I would like to do this as a command
line switch specified when a program is started. I would
prefer to avoid editing a logger config file. I expect it
is quite easy, but no good solution comes to my mind
after reading API for java.util.logging.LogManager. Could
you give me some clues?

Best,
Dominik

No. I think you need to create/provide an own Handler instance for each
package in question.

That is up to you, but the default package does not handle it. Each package
inherits the Handler from its parent, if it does not have a specific handler
defined for it.

Needs your own custom code.
 
M

markspace

Donkey said:
No. I think you need to create/provide an own Handler instance for each
package in question.

That is up to you, but the default package does not handle it. Each
package inherits the Handler from its parent, if it does not have a
specific handler defined for it.


I think Arne's solution was more correct. At least if you follow the
naming conventions, you can manipulate the loggers by name. I would
have said something similar to you, Donkey, but Arne's solution was
pretty elegant and straight-forward.

Needs your own custom code.


Should probably experiment with this a bit before rolling your own.
 
T

tnorgd

I see, if I name my loggers by a class name, e.g.
Logger l = Logger.getLogger("my.package.SomeClass");

then it is easy to implement a command-line switch for:
java MyProg -suppress my.package.SomeClass

But how to implement this:
java MyProg -suppress my.package

One possibility is to ask a LogManager for all loggers and then to
test which logger fits to a given string.

This looks interesting, e.g. I can use regular expression to control
the loggers. But isn't there any simpler solution (i.e. somethin
already existing in java.util.logging)?

Thanks a lot for help!

Best,
Dominik
 
A

Arne Vajhøj

tnorgd said:
I see, if I name my loggers by a class name, e.g.
Logger l = Logger.getLogger("my.package.SomeClass");

then it is easy to implement a command-line switch for:
java MyProg -suppress my.package.SomeClass

But how to implement this:
java MyProg -suppress my.package

One possibility is to ask a LogManager for all loggers and then to
test which logger fits to a given string.

This looks interesting, e.g. I can use regular expression to control
the loggers. But isn't there any simpler solution (i.e. somethin
already existing in java.util.logging)?

You do not need to do anything.

When you set the log level of my.package it is default
applied to my.package.SomeClass and my.package.AnotherClass
as well.

Arne
 
A

Arne Vajhøj

Arne said:
You do not need to do anything.

When you set the log level of my.package it is default
applied to my.package.SomeClass and my.package.AnotherClass
as well.

Demo:

import java.util.logging.Level;
import java.util.logging.Logger;

public class LogLevel {
public static void main(String[] args) {
Logger one = Logger.getLogger("my.package.SomeClass");
Logger two = Logger.getLogger("my.package.AnotherClass");
one.info("bla bla 1A");
two.info("bla bla 2A");
Logger.getLogger("my.package").setLevel(Level.WARNING);
one.info("bla bla 1B");
two.info("bla bla 2B");
Logger.getLogger("my.package").setLevel(Level.INFO);
one.info("bla bla 1C");
two.info("bla bla 2C");
}
}

Arne
 
M

markspace

tnorgd said:
But how to implement this:
java MyProg -suppress my.package


You can use a system property for this:

java -Djava.util.logging.config.class=mylogging.ConfigClass MyProg

This will by-pass the properties file.

One possibility is to ask a LogManager for all loggers and then to
test which logger fits to a given string.


As mentioned, loggers are hierarchical. If "my.package" is set to a
given level, then all loggers in that package space will initially use
the same level.
 
D

Donkey Hottie

markspace said:
I think Arne's solution was more correct. At least if
you follow the naming conventions, you can manipulate the
loggers by name. I would have said something similar to
you, Donkey, but Arne's solution was pretty elegant and
straight-forward.




Should probably experiment with this a bit before rolling
your own.

Agreed. I don't know what I was thinking.. if anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

selective (inheriting?) dir()? 2
Logging problem 32
Question about loggers 26
logging issues 0
Additional logging questions 17
Logging Question 20
selective logger disable/enable 10
Missing logging output in Python 1

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top