I am developing an application where i need to use the logger functionality. I have read about different levels of logger which are:
- SEVERE (highest)
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST
I am not able to understand the usage of each logging level.
Can someone give me a good example showing all the logging levels and their usage?
Generally, you don't need all those levels, SEVERE, WARNING, INFO, FINE might be enough. We're using Log4J (not java.util.logging directly) and the following levels (which might differ in name from other logging frameworks):
ERROR: Any error/exception that is or might be critical. Our Logger automatically sends an email for each such message on our servers (usage:
logger.error("message");
)WARN: Any message that might warn us of potential problems, e.g. when a user tried to log in with wrong credentials - which might indicate an attack if that happens often or in short periods of time (usage:
logger.warn("message");
)INFO: Anything that we want to know when looking at the log files, e.g. when a scheduled job started/ended (usage:
logger.info("message");
)DEBUG: As the name says, debug messages that we only rarely turn on. (usage:
logger.debug("message");
)The beauty of this is that if you set the log level to WARN, info and debug messages have next to no performance impact. If you need to get additional information from a production system you just can lower the level to INFO or DEBUG for a short period of time (since you'd get much more log entries which make your log files bigger and harder to read). Adjusting log levels etc. can normally be done at runtime (our JBoss instance checks for changes in that config every minute or so).
This excerpt is from the following awesome post.
PS: Read TRACE as VERBOSE
This tip shows how to use Logger in any java application. Logger needs to configure Formatter and Handler. There are many types of handlers and formatters present. In this example FileHandler is used to store all the log messages in a log file. And Simple formatter is used to format the log messages in human readable form.
some more examples you can find here https://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html
The java.util.logging.Level documentation does a good job of defining when to use a log level and the target audience of that log level.
Most of the confusion with
java.util.logging
is in the tracing methods. It should be in the class level documentation but instead theLevel.FINE
field provides a good overview:One important thing to understand which is not mentioned in the level documentation is that call-site tracing information is logged at
FINER
. If you log a message asFINE
you will be able to configure logging system to see the log output with or without flow control log records surrounding the log message. So useFINE
only when flow control log records are not required as context to understand this log tracing message.In general, most use of
FINER
should be left to call of entering, exiting, and throwing. That will for the most part reserveFINER
for call-site tracing when verbose logging is turned on.Use
FINEST
when the tracing log message you are about to write requires context information about program control flow. You should also use FINEST for tracing messages that produce large amounts of output data.The
CONFIG
works well for assisting system admins with the items listed above.Examples of this are tracing program startup and shutdown.
An example use case could be exceptions thrown from AutoCloseable.close implementations.
For example, if you have transaction in your program where if any one of the steps fail then all of the steps voided then SEVERE would be appropriate to use as the log level.
Those are the levels. You'd consider the severity of the message you're logging, and use the appropriate levels.
It's basically a watermark; the higher the level, the more likely you want to retain the information in the log entry. FINEST would be for messages that are of very little importance, so you'd use it for things you usually don't care about but might want to see in some rare circumstance.
Here is a good introduction to logging in Java: http://www.javapractices.com/topic/TopicAction.do?Id=143
Java comes with a logging API since it's 1.4.2 version: http://download.oracle.com/javase/1.4.2/docs/guide/util/logging/overview.html
You can also use other logging frameworks like Apache Log4j which is the most popular one: http://logging.apache.org/log4j
I suggest you to use a logging abstraction framework which allows you to change your logging framework without re-factoring you code. So you can starts by using Jul (Java Util Logging) then swith to Log4j without changing you code. The most popular logging facade is slf4j: http://www.slf4j.org/
Regards,