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.
Log Levels
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:
Level | Value | Used for |
SEVERE | 1000 | Indicates some serious failure |
WARNING | 900 | Potential Problem |
INFO | 800 | General Info |
CONFIG | 700 | Configuration Info
|
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
OFF | Integer.MAX_VALUE | Capturing nothing |
ALL | Integer.MIN_VALUE | Capturing Everything |
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
Logger Class
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.
JAVA
// Java program to illustrate logging in Java
// The following code shows a basic example how logging
// works in Java
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.*;
class DemoLogger {
private final static Logger LOGGER =
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
// Get the Logger from the log manager which corresponds
// to the given name <Logger.GLOBAL_LOGGER_NAME here>
// static so that it is linked to the class and not to
// a particular log instance because Log Manage is universal
public void makeSomeLog()
{
// add some code of your choice here
// Moving to the logging part now
LOGGER.log(Level.INFO, "My first Log Message");
// A log of INFO level with the message "My First Log Message"
}
}
public class GfG {
public static void main(String[] args)
{
DemoLogger obj = new DemoLogger();
obj.makeSomeLog();
// Generating some log messages through the
// function defined above
LogManager lgmngr = LogManager.getLogManager();
// lgmngr now contains a reference to the log manager.
Logger log = lgmngr.getLogger(Logger.GLOBAL_LOGGER_NAME);
// Getting the global application level logger
// from the Java Log Manager
log.log(Level.INFO, "This is a log message");
// Create a log message to be displayed
// The message has a level of Info
}
}
Output;
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
Similar Reads
XMLFormatter in Java Logging API
In the software development cycle, always it is good to record the set of actions that are getting done. Recording the actions are called Logging. Logging in Java by using java.util.logging package(default) logs the data. Additionally, we have third-party frameworks likeLog4j, Logback, and tinylog,
4 min read
Java Programming Basics
Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming console
4 min read
JRE in Java
Java Runtime Environment (JRE) is an open-access software distribution that has a Java class library, specific tools, and a separate JVM. In Java, JRE is one of the interrelated components in the Java Development Kit (JDK). It is the most common environment available on devices for running Java prog
4 min read
SimpleFormatter in Java Logging API
Logging is much useful for any software application and that helps to find out the execution of the project at multiple scenarios. In Java, java.util package is having the logging utility and the following imports are much necessary for logging. import java.util.logging.ConsoleHandler; import java.u
6 min read
Java Networking
When computing devices such as laptops, desktops, servers, smartphones, and tablets and an eternally-expanding arrangement of IoT gadgets such as cameras, door locks, doorbells, refrigerators, audio/visual systems, thermostats, and various sensors are sharing information and data with each other is
15+ min read
Java IO Tutorial
Java programming language comes with a variety of APIs that helps the developers to code more efficiently. One of those APIs is Java IO API. Java IO API helps the users to read and write data. In simple words, we can say that the Java IO helps the users to take the input and produce output based on
15+ min read
StrictMath log() Method In Java
The Java.lang.StrictMath.log() is an inbuilt method of StrictMath class which is used to calculate the natural logarithm i.e., log with base e, of a given double value. It gives rise to three special results: It returns a positive infinity when the argument is positive infinity.It returns NaN when t
2 min read
System.out.println in Java
Java System.out.println() is used to print an argument that is passed to it. Parts of System.out.println()The statement can be broken into 3 parts which can be understood separately: System: It is a final class defined in the java.lang package.out: This is an instance of PrintStream type, which is a
5 min read
Fast Output in Java
While doing problems in various coding platforms in some questions we end up with (TLE). At that point of time even if we use fast input then also, sometimes (TLE) remains in Java. At that time if we use fast output with fast input it can reduce the time taken. The fast output is generally used when
3 min read
Logger warning() method in Java with Examples
The warning() method of a Logger class used to Log a WARNING message.This method is used to pass WARNING types logs to all the registered output Handler objects. WARNING Message: Warning may occur whenever the user has given wrong input or credentials. There are two types of warning() method dependi
2 min read