#log4j #logging #Interview Questions
Interview Questions
Logging
- What types of logs exist?
- What are the components of the log4j logging system?
- What is a Logger in log4j?
- What is an Appender in log4j?
- What is a Layout in log4j?
- List the logging levels in log4j. What is their priority order?
- What configuration methods exist for log4j?
What types of logs exist?
- System;
- Security;
- Application (Business).
A user logs into the application, and their password is verified. This action pertains to security. Next, they launch a module. This event is at the application level. The module then requests additional data from another module and makes further calls—these are system actions.
What are the components of the log4j logging system?
The logging system consists of three main parts:
- A logging controller - logger;
- A mechanism that writes to the log - appender;
- A format definition for added logs - layout.
What is a Logger in log4j?
A Logger is an object of the class org.apache.log4j.Logger
, which serves as the control interface for logging messages and can set the level of detail. The logger checks whether a message needs to be processed, and if logging is necessary, it passes the message to the appender; if not, processing of that message is terminated.
What is an Appender in log4j?
An Appender is a named event logging object that implements the org.apache.log4j.Appender
interface and writes events to the log. The appender can call various helper tools—like a layout, filter, or error handler (if defined and needed). During this process, the necessity of recording the message is definitively established, and the message is given its final content and form.
In log4j, a log can represent:
- Console;
- File;
- Socket;
- An object of a class implementing
java.io.Writer
orjava.io.OutputStream
; - JDBC storage;
- JMS topic;
- NT Event Log;
- SMTP;
- Syslog;
- Telnet.
The most commonly used log4j appenders include:
org.apache.log4j.ConsoleAppender
- for console output;org.apache.log4j.FileAppender
- for file logging;org.apache.log4j.DailyRollingFileAppender
- for file logging with file rotation at specified intervals;org.apache.log4j.RollingFileAppender
- for file logging with file rotation upon reaching a certain size;org.apache.log4j.varia.ExternallyRolledFileAppender
- an extension of RollingFileAppender that rotates files based on commands received from a specified port;org.apache.log4j.net.SMTPAppender
- for message sending via SMTP;org.apache.log4j.AsyncAppender
- allows asynchronous logging using a separate thread, where messages are only recorded when a certain level of the intermediate buffer is filled.org.apache.log4j.nt.NTEventLogAppender
- for logging to NT Event Log;org.apache.log4j.net.SyslogAppender
- for logging to Syslog;org.apache.log4j.jdbc.JDBCAppender
- for JDBC storage logging;org.apache.log4j.lf5.LF5Appender
- sends messages to a special GUI interface called LogFactor5;org.apache.log4j.net.SocketAppender
- for broadcasting messages to a specified address and port;org.apache.log4j.net.SocketHubAppender
- for sending messages to multiple remote servers connected via a specified port;org.apache.log4j.net.TelnetAppender
- for message sending via the Telnet protocol;org.apache.log4j.net.JMSAppender
- for logging messages in JMS.
What is a Layout in log4j?
A Layout is a subclass of org.apache.log4j.Layout
that provides the ability to format messages before adding them to the log.
There are the following types of layouts in log4j:
org.apache.log4j.SimpleLayout
- produces a string containing only the output level and the message;org.apache.log4j.HTMLLayout
- formats the message as an HTML table element;org.apache.log4j.xml.XMLLayout
- formats the message in XML format;org.apache.log4j.TTCCLayout
- appends time, thread, logger name, and nested diagnostic context information to the output message;org.apache.log4j.PatternLayout
/org.apache.log4j.EnhancedPatternLayout
- allows custom formatting of messages using user-defined templates.
List the logging levels in log4j. What is their priority order?
- OFF - no logging;
- FATAL - severe error;
- ERROR - error;
- WARN - warning;
- INFO - informational message;
- DEBUG - detailed debugging message;
- TRACE - tracing all messages.
The priority order among the logging levels is as follows:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
What configuration methods exist for log4j?
To begin working with log4j, you need to provide it with a configuration. This can be done in several ways:
- Create the configuration programmatically, i.e., obtain a logger, set the logging level, attach an appender, and specify the formatting method.
- Provide a file or URL as an argument when starting the Java machine using
-Dlog4j.configuration=path/to/configuration/file
, then read it in the program usingPropertyConfigurator.configure(...)
/DOMConfigurator.configure(...)
for.properties
orXML
formats, respectively. - Load the configuration from an XML file or
.properties
file: log4j searches for the configuration file in the classpath. It first looks forlog4j.xml
, and if it does not find it, it looks forlog4j.properties
.