
- log4j - Home
- log4j - Overview
- log4j - Installation
- log4j - Architecture
- log4j - Configuration
- log4j - Sample Program
- log4j - Logging Methods
- log4j - Logging Levels
- log4j - ConsoleAppender
- log4j - Logging in Database
Log4j - Formatting Layouts
- log4j - Log Formatting
- log4j - CSV Parameter Layout
- log4j - CSV Log Event Layout
- log4j - HTML Layout
- log4j - Pattern Layout
- log4j - Json Template Layout
Log4j - File Appenders
- log4j - Logging in Files
- log4j - FileAppender
- log4j - Separate Folder per Month
- log4j - Daily log File Generation
log4j Resources
log4j - Logging Methods
Logger class provides a variety of methods to handle logging activities. The Logger class does not allow us to instantiate a new Logger instance but we can use LogManager which provides many static methods for obtaining a Logger object, like following −
- public static Logger getRootLogger();
- public static Logger getLogger(String name);
The first of the two methods returns the application instance's root logger and it does not have a name.
Any other named Logger object instance is obtained through the second method by passing the name of the logger. The name of the logger can be any string you can pass, usually a class or a package name as we have used in the last chapter and it is mentioned below −
static Logger log = LogManager.getLogger(log4jDemo.class.getName());
Logging Methods
Once we obtain an instance of a named logger, we can use several methods of the logger to log messages. The Logger class has the following methods for printing the logging information.
# | Methods and Description |
---|---|
1 |
public void debug(Object message)
It prints messages with the level Level.DEBUG. |
2 |
public void error(Object message)
It prints messages with the level Level.ERROR. |
3 |
public void fatal(Object message)
It prints messages with the level Level.FATAL. |
4 |
public void info(Object message)
It prints messages with the level Level.INFO. |
5 |
public void warn(Object message)
It prints messages with the level Level.WARN. |
6 |
public void trace(Object message)
It prints messages with the level Level.TRACE. |
Example - Usage of Log Levels
All the levels are defined in the org.apache.logging.log4j.Level class and any of the above mentioned methods can be called as follows −
Log4jDemo.java
package com.tutorialspoint; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4jDemo{ /* Get actual class name to be printed on */ private static final Logger LOGGER = LogManager.getLogger(); public static void main(String[] args){ LOGGER.trace("Trace Message!"); LOGGER.debug("Debug Message!"); LOGGER.info("Info Message!"); LOGGER.warn("Warn Message!"); LOGGER.error("Error Message!"); LOGGER.fatal("Fatal Message!"); } }
Update the log4j2.properties to use the Console Appender.
Log4j2.properties
appender.0.type = Console appender.0.name = CONSOLE appender.0.layout.type = PatternLayout appender.0.layout.pattern = %p - %m%n rootLogger.level = DEBUG rootLogger.appenderRef.0.ref = CONSOLE
Output
When you compile and run Log4jDemo program, it would generate the following result −
DEBUG - Debug Message! INFO - Info Message! WARN - Warn Message! ERROR - Error Message! FATAL - Fatal Message!
All the debug messages make more sense when they are used in combination with levels. We will cover levels in the next chapter and then, you would have a good understanding of how to use these methods in combination with different levels of debugging.