Background information
Apache Log4j is an open source project that allows you to configure the log output destination to console, file, GUI component, socket server, NT event recorder, or UNIX Syslog daemon. Apache Log4j also allows you to set the output format for each log and define the level of each log to manage log generation in a more detailed manner. To configure the preceding features, you need to only use a configuration file. You do not need to modify the application code. Log4j consists of the following main components:
Appenders
Appenders are used to define the destinations of logs. You can configure multiple appenders to send logs to different destinations. The following table describes common appenders.
Appender | Description |
ConsoleAppender | Outputs logs to a console. |
FileAppender | Outputs logs to a file. |
DailyRollingFileAppender | Outputs logs to a new file every day. |
RollingFileAppender | Outputs logs to a file with a specific size. When the file reaches the specified size, the system automatically changes the name of the file and generates a new file. |
JDBCAppender | Stores logs to a database. |
Loggers
Loggers are used to define the entry points of logs and capture log information. Each Logger is assigned a log level based on its importance or severity. Log4j defines the following log levels: OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, and ALL (listed in descending order of priority). Log levels can be inherited. Child classes record all log levels of the parent class. The following table describes the log levels.
Log level | Description |
OFF | Disables all logs. |
FATAL | Indicates error events that can cause applications to exit. |
ERROR | Indicates error events that do not affect the running of systems. |
WARN | Indicates potential errors. |
INFO | Focuses on the running of applications. In most cases, this log level is used in scenarios in which logs are managed in a coarse-grained manner. |
DEBUG | Facilitates diagnostic. In most cases, this log level is used in application debugging scenarios. |
TRACE | Outputs the variables during the running of programs and displays the execution process. This log level is used to trace programs. |
ALL | Prints all logs. |
Note
A logger can correspond to multiple appenders. An appender can correspond to only one layout.
In this example, the company wants to obtain the best solution for its e-commerce platform. The company needs to analyze information such as platform stability, system errors, data security, and behavioral data. The behavioral data includes logon methods, logon time, logon duration, browsed pages, dwell time for pages, average order time, and consumption levels. Simple Log Service provides the log collection and log analysis features to help store and analyze logs. The following examples show sample logs that are collected by Simple Log Service.