Logging in Java
Java provides the ability to capture the log files.
The need for Log capture
There are multiple reasons why we may need to capture the application activity.
- Recording unusual circumstances or errors that may be happening in the program
- Getting the info about whats going in the application
The details which can be obtained from the logs can vary. Sometimes, we may want a lot of details regarding the issue, or sometimes some light information only.
Like when the application is under development and is undergoing testing phase, we may need to capture a lot of details.
The log levels control the logging details. They determine the extent to which depth the log files are generated. Each level is associated with a numeric value and there are 7 basic log levels and 2 special ones.
We need to specify the desired level of logging every time, we seek to interact with the log system. The basic logging levels are:
|SEVERE||1000||Indicates some serious failure|
|FINE||500||General developer info|
|FINER||400||Detailed developer info|
|FINEST||300||Specialized Developer Info|
Severe occurs when something terrible has occurred and the application cannot continue further. Ex like database unavailable, out of memory.
Warning may occur whenever the user has given wrong input or credentials.
Info is for the use of administrators or advanced users. It denotes mostly the actions that have lead to a change in state for the application.
Configuration Information may be like what CPU the application is running on, how much is the disk and memory space.
Fine Finer and Finest provide tracing information. When what is happening/ has happened in our application.
FINE displays the most important messages out of these.
FINER outputs a detailed tracing message and may include logging calls regarding method entering, exiting, throwing exceptions.
FINEST provides highly detailed tracing message.
Furthermore, there are two special Logging levels
Capturing everything may mean every field declaration, definition, every method call, every assignment performed etc.
Java’s Log System
The log system is centrally managed. There is only one application wide log manager which manages both the configuration of the log system and the objects that do the actual logging.
The Log Manager Class provides a single global instance to interact with log files. It has a static method which is named getLogManager
The logger class provides methods for logging. Since LogManager is the one doing actual logging, its instances are accessed using the LogManager‘s getLogger method.
The global logger instance is accessed through Logger class’ static field GLOBAL_LOGGER_NAME. It is provided as a convenience for making casual use of the Logging package.
May 12, 2018 7:56:33 AM DemoLogger makeSomeLog INFO: My first Log Message May 12, 2018 7:56:33 AM GfG main INFO: This is a log message
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.