setting java log levels

2019-08-18 02:26发布

问题:

I am using java.util.logging.Logger for logging. Currently I am creating one log file for each class.

Can someone please suggest how to create a log file for a package instead of each class. And how to set the levels for each of these. I am also not able to find how to change the levels for all the loggers at once.

So, essentially I am asking three questions here. Sorry about that. 1. Creating log file one for each package. 2. Setting common log level for the logs. 3. Setting log levels for each logger.

Vogella explains it well, but few things are confusing like what does he mean when he says

(Logger.GLOBAL_LOGGER_NAME)

Just found partial answer here

回答1:

Creating log file one for each package.

You can create a custom handler and attach it to the root logger or if you know all of the packages that you will use then attach a filehandler with a file name the same as the package name then attach it to logger for that package.

Setting common log level for the logs.

This is all described in the LogManager documentation. Pick the logger name that is the root of all of your packages and set the level on that logger. All child loggers will assume that level.

Setting log levels for each logger.

This is also described in the LogManager documentation. You can set the level of your root logger and then set the level of the child loggers and control where the output goes.

In all cases watch out for weak references to loggers.



回答2:

  1. My log setting file (log4j.xml) is in src/main/resources folder in an enterprise project.
    1. My common settings is regarding to packages. And all classes and subpackages in this package use this setting.
    2. Log level? It depends on what would you like to see? INFO writes a lot of information, maybe help you. I note that if you have database connection then you set logging in other place.