2

I use a property file for java.util.logging and want to log all classes under package:

aaa.bbb.ccc.*

the normal way (i.e. info, fine, finer) but class

aaa.bbb.ccc.ddd.MyClass

in its own logging file "My Class.log" with level finer.

The config should be done only via a properties file. How would this look like?

I tried various ways (e.g. different handlers) but not any succeeded: It never worked that both log files are written to.

To make the problem more concrete - the config I tried:

handler.performance.class=com.logging.handler.FileHandler
handler.performance.file=${LOGGING_ROOT}/performance.log
handler.performance.level=FINE

handler.fine.class=com.logging.handler.FileHandler
handler.fine.file=${LOGGING_ROOT}/finer.log
handler.fine.level=FINE

handler.async.class=com.logging.handler.AsyncBufferHandler
handler.async.level=ALL
handler.async.targets=fine

handler.asyncperf.class=com.logging.handler.AsyncBufferHandler
handler.asyncperf.level=ALL
handler.asyncperf.targets=performance

com.myapp.handlers=async,console
com.myapp.useParentHandlers=false

com.myapp.common.logging.handlers=asyncperf
com.myapp.common.logging.useParentHandlers=false

The class I want to log to this separate performance log is located beneath com.myapp.common.logging...

2
  • 2
    Specify, what logging system do you use. Commented May 31, 2012 at 9:29
  • just curious: where is LOGGING_ROOT defined? Commented Aug 19, 2020 at 17:47

3 Answers 3

4

Found the solution - it was a wrong initialization:

The logger should be initialized with:

Logger.getLogger(MyClass.class.getName())

Then the config:

com.myapp.common.logging.MyClass.handlers=asyncperf
com.myapp.common.logging.MyClass.useParentHandlers=false

logs all logging messages of this class in the specified separate file as desired!

Sign up to request clarification or add additional context in comments.

Comments

1

Define two File appenders for the two target files

Define one root logger to use the first appender

Define a second logger for the special class, to use the other appender

set additivity of the logger to false in order to make any message go to one but not both files

2 Comments

I don't use log4j (unfortunately). Just plain java.util.logging.
Sorry, kind of assumed log4j. Don't know why.
0

I think that you have problem because you can't configure 2 default FileHandlers, only one of them. So try to implement your personal subclass of FileHandler and configure it as a separate handler. I don't remember if we can configure logger for separate class or only for package, so try also configure handlers to package of MyClass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.