- In Java, logging frameworks are provided by the package java.util.logging and used to write log messages on to a central location
- The log messages can be error messages, informational messages or configuration messages
Logging is divided into three parts:
Logger
– Receives the message that is then passed to the logging framework
– The framework then calls the formatter including the message
Formatter
– Formats the message for output
– The framework the passes the output to the handler
Handler (Appender)
– The appropriate handler dispositions the output
– Examples of Java Logging Frameworks:
– Apache Commons Logging
– Log4J
– Java Logging API
– SLF4J
Example for Logging Exceptions
- The code below gives an example of logging an exception to a log file

Logging Levels
- Logging plays a major role in web applications as it is the only way to track the server’s activities. Logging levels based on Java log4j are as follows:
DEBUG
- The lowest restricted Java logging level
- The messages logged in this level are used to debug the application
- It is used only in the development and testing environments
WARN
- This level is more restricted than the INFO level
- The messages logged in this level are used to give waring messages
E.g. Connections lost between client and serve, Database connection lost etc.
FATAL
- This level indicates severe error conditions that leads the application to abort
- After this, the application may eventually crash or get stopped
INFO
- This level is more restricted than the DEBUG Java logging level
- The messages logged in this level are used to give information to the programmers
E.g. The server has started, incoming and outgoing messages
ERROR
- This level is more restricted than the WARN level
- Error and exception messages are logged in this level
- ERRORS should always be printed in Java
OFF
- This level has the highest possible rank and it is intended to turn off logging in Java