This document provides an overview of logging in Java, focusing on the Log4j logging framework. It discusses logging concepts like log levels, appenders, and layouts. It then provides examples of configuring Log4j through properties files, including setting log levels and outputs. The document also presents examples of integrating Log4j in Java code through loggers and handling different log levels.
4. Loggin Oveview
▪ Logging is the process of writing log messages during the
execution of a program to a central place.
▪ This logging allows you to report and persist error and
warning messages as well as info messages (e.g., runtime
statistics) so that the messages can later be retrieved and
analyzed.
▪ The object which performs the logging in applications is
typically just called Logger.
5. Loggin Oveview
▪ To create a logger in your Java code, you can use the following
snippet.
import java.util.logging.Logger;
… … …
// Assumes the current class is called logger
private final static Logger LOGGER =
Logger.getLogger(MyClass.class.getName());
▪ The Logger you create is actually a hierarchy of Loggers, and
a . (dot) in the hierarchy indicates a level in the hierarchy.
7. Log4j Introduction
▪ Log4j initially developed in the framework of "Apache Jakarta
Project".
▪ Separated into a journaling project.
▪ Has been the de facto standard.
▪ Apache log4j is a Java-based logging utility.
▪ The log4j team has created a successor to log4j with version
number 2.0.
▪ log4j 2.0 was developed with a focus on the problems of log4j
1.2, 1.3.
▪ You can define three main components:
– Loggers, Appenders and Layouts.
8. Simplest Example
package com.softserve.edu;
import org.apache.log4j.Logger;
public class App {
public static final Logger LOG =
Logger.getLogger(App.class);
public static void main(String[] args) {
System.out.println("The Start");
LOG.info("Hello World!");
}
}
9. Loggin Oveview
▪ If the program is run
log4j:WARN No appenders could be found for
logger (com.softserve.edu.App).
log4j:WARN Please initialize the log4j system
properly.
▪ There are three ways to configure log4j: with a properties file,
with an XML file and through Java code
▪ For example, configure log4j to output to the screen.
▪ Will be used configuration files of two types:
– log4j.properties and
– log4j.xml
11. Loggin Levels
▪ The following list defines the log levels and messages in log4j,
in decreasing order of severity
▪ OFF: The highest possible rank and is intended to turn off
logging.
▪ FATAL: Severe errors that cause premature termination. Expect
these to be immediately visible on a status console.
▪ ERROR: Other runtime errors or unexpected conditions.
Expect these to be immediately visible on a status console.
12. Loggin Levels
▪ WARN: Use of deprecated APIs, poor use of API, 'almost'
errors, other runtime situations that are undesirable or
unexpected, but not necessarily "wrong". Expect these to be
immediately visible on a status console.
▪ INFO: Interesting runtime events (startup/shutdown). Expect
these to be immediately visible on a console, so be
conservative and keep to a minimum.
▪ DEBUG: Detailed information on the flow through the system.
Expect these to be written to logs only.
▪ TRACE: Most detailed information. Expect these to be written
to logs only.
16. Appenders
▪ The actual outputs are done by Appenders.
▪ There are numerous Appenders available, with descriptive
names, such as
– FileAppender, ConsoleAppender, SocketAppender,
SyslogAppender, NTEventLogAppender and even
SMTPAppender.
▪ Multiple Appenders can be attached to any Logger, so it's
possible to log the same information to multiple outputs; for
example to a file locally and to a socket listener on another
computer.
17. Appenders
▪ org.apache.log4j.ConsoleAppender
– the most frequently used.
▪ org.apache.log4j.FileAppender
– writes messages to the file.
▪ org.apache.log4j.DailyRollingFileAppender
– creates a new file, add the year, month and day to the
name.
▪ org.apache.log4j.RollingFileAppender
– creates a new file when the specified size, adds to the file
name index, 1, 2, 3.
▪ org.apache.log4j.net.SMTPAppender
– sending e-mails.
19. Layouts
▪ An Appender uses a Layout to format a LogEvent into a form
that meets the needs of whatever will be consuming the log
event.
▪ In Log4j 1.x and Logback Layouts were expected to
transform an event into a String.
▪ In Log4j 2 Layouts return a byte array.
▪ This allows the result of the Layout to be useful in many more
types of Appenders.
20. PatternLayout
▪ %d{ABSOLUTE}
– Displays time; ABSOLUTE – in format HH:mm:ss,SSS
▪ %5p
– Displays the log level (ERROR, DEBUG, INFO, etc.); use 5
characters, the rest padded with spaces;
▪ %t
– Displays the name of the thread;
▪ %c{1}
– class name with the package (indicates how many levels to
display);
21. PatternLayout
▪ %M
– The method name;
▪ %L
– Line number;
▪ %m
– Message that is sent to the log;
▪ %n
– Newline.
▪ %highlight{pattern}{style}
– Adds ANSI colors to the result of the enclosed pattern
based on the current event's logging level.
– %highlight{%d [%t]}
25. Output Settings
▪ Output in the log for specific classes and packages
log4j.logger.<PACKAGE_NAME>=<LEVEL>
log4j.logger.<PACKAGE_NAME>.<CLASS_NAME>=
<LEVEL>, <LOGGER_NAME>
▪ How to write the log;
– Specify for the package and class,
– usage level, additional appender
28. Case studies
package com.softserve.edu;
import com.softserve.training.Calc;
import com.softserve.training.Some;
public class App {
public static final Logger logger =
Logger.getLogger(App.class); // LoggerFactory
public static void main(String[] args) {
System.out.println("Hello from App:");
App app = new App();
Calc calc = new Calc();
Some some = new Some();
30. Case studies
package com.softserve.training;
import com.softserve.edu.App;
public class Calc {
public static final Logger logger =
Logger.getLogger(Calc.class); // LoggerFactory
public void calcMethod() {
logger.error("Calc Error");
logger.warn("Calc Warning");
logger.info("Calc Info");
logger.debug("Calc Debug");
}
}
31. Case studies
package com.softserve.training;
import com.softserve.edu.App;
public class Some {
public static final Logger logger =
Logger.getLogger(Some.class); // LoggerFactory
public void someMethod() {
logger.error("Some Error");
logger.warn("Some Warning");
logger.info("Some Info");
logger.debug("Some Debug");
}
}