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
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read