Setting up java Logger for a specific package
Asked Answered
G

1

8

could anybody explain to me, how to set up java Logger for various classes from a concrete package ?

for example: if I get this one and set it up

Logger logger = Logger.getLogger("com.google.api.client.*");
        logger.setLevel(Level.CONFIG);
        logger.addHandler(new Handler() {

            @Override
            public void close() throws SecurityException {
            }

            @Override
            public void flush() {
            }

            @Override
            public void publish(LogRecord record) {
                // default ConsoleHandler will take care of >= INFO
                if (record.getLevel().intValue() < Level.INFO.intValue()) {
                    System.out.println(record.getMessage());
                }
            }
        });

there are conditions like this

Logger.getLogger(HttpTransport.class.getName()).isLoggable(Level.CONFIG);

in the library where HttpTransport is part of com.google.api.client.*

But the problem is, that

Logger.getLogger(HttpTransport.class.getName()).isLoggable(Level.CONFIG); 

is false ... like if a different logger was obtained

How else should I set it for all classes from the same package? if there are conditions for loggers for concrete classes like HttpTransport.

Gargantua answered 13/3, 2011 at 18:28 Comment(0)
T
7

You do not want the .* in your package string. Change

Logger logger = Logger.getLogger("com.google.api.client.*");

to

Logger logger = Logger.getLogger("com.google.api.client");
Trapeze answered 13/3, 2011 at 18:38 Comment(2)
OMG :-) I spent 30 minutes with this one... Thank youGargantua
It's better to use Client.class.getName() than a string as the class name, so if you refactor it won't break your logging.Hibernate

© 2022 - 2024 — McMap. All rights reserved.