I suggest you take a look at an article entitled βA Brief Introduction to log4jβ . It provides a brief explanation of journal levels and demonstrates how they can be used in practice. The basic idea of ββlog levels is that you want to customize how detailed the logs are, depending on the situation. For example, if you are trying to fix a problem, you need the logs to be very detailed. In production, you can only see warnings and errors.
The log level for each component of your system is usually controlled by a parameter in the configuration file, so it is easy to change. Your code will contain various logging statements at different levels. When responding to an Exception you can raise Logger.error . If you want to print the value of a variable at any given point, you can call Logger.debug . This combination of custom logging level and logging in your program allows you to fully control how your application will record its activities.
In the case of log4j, at least ordering the log levels:
DEBUG < INFO < WARN < ERROR < FATAL
Here is a brief example from this article that demonstrates how journal levels work.
// get a logger instance named "com.foo" Logger logger = Logger.getLogger("com.foo"); // Now set its level. Normally you do not need to set the // level of a logger programmatically. This is usually done // in configuration files. logger.setLevel(Level.INFO); Logger barlogger = Logger.getLogger("com.foo.Bar"); // This request is enabled, because WARN >= INFO. logger.warn("Low fuel level."); // This request is disabled, because DEBUG < INFO. logger.debug("Starting search for nearest gas station."); // The logger instance barlogger, named "com.foo.Bar", // will inherit its level from the logger named // "com.foo" Thus, the following request is enabled // because INFO >= INFO. barlogger.info("Located nearest gas station."); // This request is disabled, because DEBUG < INFO. barlogger.debug("Exiting gas station search");
William Brendel Feb 26 '10 at 2:41
source share