i would like to know how to use the java.util.logging api, in order to have log message written in different log file depending on the level used. If the level is INFO then i would like to have the message written in /log/info.log and so on. The 3 defined level are severe, warning and info.
i would like to know how to use the java.util.logging api, in order to
Share
You use custom
Handlersto write the Log Records.Here is a simple, but complete example you can build upon.
and here is how to use it
you will get three files in
/tmpeach with only the messages for each particular log level in them.Note, I like the Dependency Injection style approach of requiring the
Levelin the constructor so you can’t “forget” to call.setLevel()when using this sub-class. I also disabled.setLevel()because calling it and changing would break the semantics of the subclass”Just for completeness you can use a
java.util.logging.Filterto acomplish the same thing. It isn’t as encapsulated but it is an alternative. It is more code and more verbose, thus more to not get right.Personally I still like the sub-class approach better, it is less error prone and more self documenting as of its purpose and intent.