When it comes to troubleshooting the performance of Java applications, JVM metrics are not enough. To fully understand Java concepts, you also need Java Logs and traces. In this article, let us focus on Java Application logs.
In this blog, we will be focusing on:
What is Logging/Logger
Java Logging Libraries
How to use Logging in Java Application
Logging Levels
What is Logging
Logging is an API that provides the ability to trace out the errors of the applications. When an application generates the logging call, the Logger records the event in the LogRecordfile. After that, it sends it to the corresponding handlers or appenders. Before sending it to the console or file, the appenders format that logs record by using the formatter or layouts.
Java Logging Libraries / Frameworks
SLF4J
java.util.logging
Logback
Log4j
Log4j2
SLF4J :
SLF4J is known as the Simple Logging Facade for Java or SLF4J. It is an abstraction layer for various logging frameworks, allowing users to choose a logging framework during the deployment time, rather than during the development time.
This enables quick and easy change of logging framework of choice when it is needed.
Java util.logging:
The java.util.logging package provides the classes and interfaces for Java core logging facilities.
It comes bundled with the Java Development Kit and is available for every developer using Java since Java 1.4.
Logback:
Logback started as an intended successor to the first version of Log4j.
It is built out of three main modules and integrates with Servlet containers such as Tomcat or Jetty to provide HTTP-access log functionality.
Log4j:
Log4j is one of the most widely known Java logging libraries and the predecessor for such projects as Logback or Log4j 2.
But, Log4j reached its end of life on the 5th of August 2015 and users are recommended to use Log4j 2.
Log4j 2:
Log4j 2 is the newest in the list of all the mentioned Java logging frameworks.
It incorporates lots of improvements over its predecessor and promises to provide Logback improvements while fixing issues with some of its architectural problems. If you are starting a new project and you are looking for a library of choice Log4j 2 should be the main focus.
How to Log Using the Java Logging API
Java contains the Java logging API which allows you to configure what type of log messages are written.
The API comes with a standard set of key elements that we should know about to be able to proceed and discuss more than just the basic logging.
The java.util.logging package provides the Logger class to log application messages.
Logger:
The logger is the main entity that an application uses to make logging calls, to capture LogRecords to the appropriate handler.
The LogRecord (or logging event) is the entity used to pass the logging requests between the framework that is used for logging and the handlers that are responsible for log shipping.
The Logger object is usually used for a single class or a single component to provide context-bound to a specific use case.
Adding logging to our Java application is usually about configuring the library of choice and including the Logger. That allows us to add logging into the parts of our application that we want to know about.
Handler:
The handler is used to export the LogRecord entity to a given destination.
Those destinations can be the memory, console, files, and remote locations via sockets and various APIs. There are different standard handlers. Below are a few examples of such handlers:
ConsoleHandler
FileHandler
SyslogHandler
public class ExampleHandler extends Handler {
@Override
public void publish(LogRecord logRecord) {
System.out.println(String.format("Log level: %s, message: %s",
logRecord.getLevel().toString(), logRecord.getMessage()));
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}
Logging Levels
Java log levels are a set of standards for the logging message. Tells how important the log record is.
For example, the following log levels are sorted from least to most important with some explanation
TRACE:
Trace is only used in rare cases where you need full visibility of what is happening in the application.
In most cases, the TRACE level will be very verbose, but you can also expect a lot of information about the application. Example: Use for annotating the steps in an algorithm that are not relevant in everyday use.
LOGGER.trace("This is a TRACE log level message");
DEBUG:
Debug is less granular than the TRACE level.
The DEBUG level should be used for information that can be useful for troubleshooting.
LOGGER.debug("This is a DEBUG log level message");
INFO:
Info is known as the standard level of log information. INFO level gives you information about a certain process that finished with success or failure information.
For example: “Created a user {} with id {}”
LOGGER.info("This is a INFO log level message");
WARN:
The WARN Log usually indicates a state of the application that might be problematic or that it detected an unusual execution.
For example, a message was not parsed correctly, because it was not correct.
The code execution is continuing, but we could log that with the WARN level to inform us and others that potential problems are happening.
LOGGER.warn("This is a WARN log level message");
ERROR:
Log level that indicates an issue with a system that prevents certain functionality from working.
For example, if you provide login via social media as one way of logging into your system, the failure of such a module is an **ERROR-**level log.
LOGGER.error("This is a ERROR log level message");
FATAL:
- The FATAL log level indicates that your application encountered an event that prevents it from working or a crucial part of it from working.
for example, an inability to connect to a database that your system relies on or to an external payment system that is needed to check out the cart in your e-commerce system. Also, The FATAL error doesn’t have the representation in SLF4J.
Here comes the end of the basics of Java Logging. Hope this is helpful.
Thank you for reading the article. Please like, share and comment. it will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!!!