How to configure the properties of a specific FileHandler
Asked Answered
C

3

6

The Java logging configuration file lets me define the properties of a named logger, e.g.

name.heikoseeberger.heikotron.level = FINE
name.heikoseeberger.heikotron.handlers = java.util.logging.FileHandler

So far, so good. Now I would like to configure that particular FileHandler, e.g. with a specific output file. Unfortunately I only know how to configure the "global" FileHandler, which is already present in the configuration file:

java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

I don't want to configure this one, but the instance which is associated with my custom Logger. I already tried the following, but without success:

name.heikoseeberger.heikotron.java.util.logging.FileHandler.pattern = %h/heikotron.log
name.heikoseeberger.heikotron.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

Is it possible at all to set the properties of specific FileHandler instances? If yes, how should these be identified/named?

Chromosphere answered 10/5, 2012 at 10:39 Comment(0)
M
1

This is done by using the config option described in the top level class documentation of the LogManger. Create a public named class with a public constructor and invoke all of the java calls you need to make to configure your handler. Then in your logging properties direct the LogManager to load your class you created to configure your handler. Otherwise you can to subclass file handler which will create a custom namespace to configure.

Mortality answered 1/8, 2013 at 15:51 Comment(1)
'A property "config". This property is intended to allow arbitrary configuration code to be run.' So this could be used to load another config file that has class to filename mappingsTetrapterous
U
0

I do not think it possible. If you review the source code for the FileHandler you will soon determine that it uses the string "java.util.logging.FileHandler.pattern" to determine the pattern of the file to use for logging purposes

private void configure() {
        LogManager manager = LogManager.getLogManager();

        String cname = getClass().getName();

        pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");
        limit = manager.getIntProperty(cname + ".limit", 0);
        //...
    }

As such, the configuration that you are putting in the file is not even been taken into account by the Handler.

It appears to me that handlers are unaware of the existence of any particular logger (i.e. name.heikoseeberger.heikotron), they just know how to publish a given LogRecord.

As far as I can see, the handlers of a particular logger are created by the LogManager, by reflectively invoking their default constructor, as such, when a given handler is being created, it is unaware of for which particular logger it has been requested, that is why all their properties are set through their own class names and not through the logger's name.

Upgrade answered 11/5, 2012 at 4:37 Comment(0)
V
0

If I understand right you are trying to write in different log files using java.util.logging package. This can't be done out of the box without extending it.

If you can't switch to another logging framework like Logback, check answer to java util logging.properties: How to log to two different files and see if it fits your needs.

Vish answered 28/5, 2012 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.