简体   繁体   中英

Java logging all except one

I have the following logging.properties file:

handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

java.util.logging.FileHandler.pattern = /tmp/file.log
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.append = true 
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$td.%1$tm.%1$tY %1$tH:%1$tM:%1$tS [%4$s] %5$s%6$s%n

fileclass = INFO, FileHandler

Currently everything goes to the console, plus 'fileclass' package into the file, but I want 'fileclass' package to be excluded from the console. I could define main package to go to the console, but the main program doesnt have the package.

Is it possible to have such case within logging.properties: - Everything goes to the console, except 'fileclass' which goes to the file

This can be done a couple ways.

Let's assume you want to leave the default console handler. You can remove java.util.logging.FileHandler from the global handlers list, thus leaving you with the following:

handlers=java.util.logging.ConsoleHandler

This will cause all applications to log to the console. To complete your case, we must ensure 'fileclass' does not log to the console, yet does log to a file.

You can do this programmatically or declaratively:

Programmatically

Within the 'fileclass' app. Create a custom logger. eg

Logger logger = Logger.getLogger(FileClass.class.getName());

// set logger level
logger.setLevel(Level.INFO);

FileHandler fileHandler = new FileHandler("file.log");

// Set the handler level
// NOTE: This setting will ignore INFO records sent by the logger
fileHandler.setLevel(Level.WARNING);
logger.addHandler(fileHandler);

Now that you've added a FileHandler manually. All that is left to do is disconnect the java.util.logging.ConsoleHandler from your 'fileclass'. You can do this by calling logger.removeHandler(consoleHandler) , where consoleHandler is the instance of java.util.logging.ConsoleHandler

Declaratively

You can also declaratively add a file handler and disable parents and global handlers

# disable parents, e.g. in this case the console handler
com.some.package.useParentHandlers=false 

# add the file handler
com.some.package.logger.MyLogger.level=INFO
com.some.package.MyFileHandler.pattern=%h/CoolLog%g.log
com.some.package.handler.MyFileHandler.limit=20000000
com.some.package.handler.MyFileHandler.count=20

This will enable you add add a file handle specifically for that application, however it won't remove the console handler since its declared globally.

You may want to take a look at my blog posts on Java Logging:

I haven't used Java Util Logger but normally that's related to "Additivity" of logger.

The way to achieve what you want is, set additivity of fileclass logger to false (which means excluding usage of parent appenders/handlers. Then add only the appender/handler you want for fileclass

The way to control additivity in Java Util Logging is by using useParentHandlers in your config. So it looks like:

fileclass.useParentHandlers=false
fileclass = INFO, FileHandler

By doing so, fileclass logger is not going to inherit handlers of its parent, and only use whatever you set to it.

One thing to note is, because logger is hierarchical. Therefore it will affect fileclass logger and all its children logger. For example if you have a logger called fileclass.foo , it will also be using only the FileHandler , which may or may not be what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM