Configuring Log4j2 Properties¶
All WSO2 products are shipped with the log4j2 logging capabilities, which generates administrative activities and server side logs. The log4j2.properties
file, which governs how logging is performed by the server can be found in the MI_HOME/conf
directory.
There are three main components when configuring log4j2: Loggers, Appenders, and Layouts.
Setting the log level¶
The log level can be set specifically for each appender in the log4j2.properties
file by setting the threshold value. If a log level is not specifically given for an appender as explained below, the root log level (INFO) will apply to all appenders by default.
For example, shown below is how the log level is set to DEBUG for the CARBON_LOGFILE
appender:
appender.CARBON_LOGFILE.filter.threshold.level = DEBUG
Listed below are the log levels that can be configured:
Level | Description |
---|---|
OFF | The highest possible log level. This is intended for disabling logging. |
FATAL | Indicates server errors that cause premature termination. These logs are expected to be immediately visible on the command line that you used for starting the server. |
ERROR | Indicates other runtime errors or unexpected conditions. These logs are expected to be immediately visible on the command line that you used for starting the server. |
WARN | Indicates the use of deprecated APIs, poor use of API, possible errors, and other runtime situations that are undesirable or unexpected but not necessarily wrong. These logs are expected to be immediately visible on the command line that you used for starting the server. |
INFO | Indicates important runtime events, such as server startup/shutdown. These logs are expected to be immediately visible on the command line that you used for starting the server . It is recommended to keep these logs to a minimum. |
DEBUG | Provides detailed information on the flow through the system. This information is expected to be written to logs only. Generally, most lines logged by your application should be written as DEBUG logs. |
TRACE | Provides additional details on the behavior of events and services. This information is expected to be written to logs only. |
Setting the log pattern¶
The log pattern defines the output format of the log file. This is the layout pattern that describes the log message format.
Identifying forged messages: The conversion character 'u' can be used in the pattern layout to log a UUID. For example, the log pattern can be [%u] [%d] %5p {%c} - %m%ex%n, where [%u] is the UUID.
Setting the Sys Log Host¶
The IP address of the system log server. The syslog server is a dedicated log server for many applications. It runs in a particular TCP port in a separate machine, which can be identified by an IP address.
Setting the Facility¶
The log message type sent to the system log server.
Setting the Threshold¶
Filters log entries based on their level. For example, threshold set to 'WARN' will allow the log entry to pass into appender. If its level is 'WARN', 'ERROR' or 'FATAL', other entries will be discarded. This is the minimum log level at which you can log a message. See descriptions of the available log levels .
Configuring Log4j2 Appenders¶
This section allows you to configure appenders individually. Log4j2 allows logging requests to print to multiple destinations. These output destinations are called 'Appenders'. You can attach several appenders to one logger.
Note
If the output destination is in another environment (such as a cloud storage), you need to use custom log appenders.
- CARBON_CONSOLE: Logs to the console when the server is running.
- CARBON_LOGFILE: Writes the logs to MI_HOME/repository/logs/wso2carbon.log .
- SERVICE_APPENDER: Writes service invocations to
MI_HOME/repository/logswso2-MI-service.log
. - ERROR_LOGFILE: Writes warning/error messages to the
MI_HOME/repository/logs/wso2-MI-service.log
. - TRACE _ APPENDER: Writes tracing/debug messages to the
MI_HOME/repository/logs/wso2-MI-trace.log
for tracing enabled services. - CARBON _ MEMORY:
- CARBON_SYS_LOG: Allows separating the software that generates messages, from the system that stores them and the software that reports and analyzes them.
- CARBON_TRACE_LOGFILE:
Configuring Log4j2 Loggers¶
A Logger is an object used to log messages for a specific system or application component.
The logger element must have a name attribute specified. It may also have a level attribute and an additivity attribute specified. The level may be configured with one of the following values: TRACE
, DEBUG
, INFO
, WARN
, ERROR
, ALL
or OFF
.
- Name: The name of a logger.
- Level: Allows to configure level (threshold). After you specify the level for a certain logger, a log request for that logger will only be enabled if its level is equal or higher to the logger’s level. If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. Refer to the hierarchy of levels given above. See descriptions of the available log levels .
-
Additivity: Allows to inherit all the appenders of the parent Logger if set as 'True'.
-
Add the logger in the
log4j2.properties
file:logger.<Logger_Name>.name = <Component_name> logger.<Logger_Name>.type = INFO
For example:
logger.VFSTransportSender.name = org.apache.synapse.transport.vfs.VFSTransportSender logger.VFSTransportSender.level = WARN
-
Then, add the logger to the list of loggers by referring the logger name:
loggers = AUDIT_LOG, SERVICE_LOGGER, VFSTransportSender,
Using Custom Log Appenders¶
Custom log appenders for Log4j2 can be used to store application logs in various environments/systems such as cloud storages.
However, since WSO2 Micro Integrator works in an OSGi environment, such Log4j2 extensions cannot be used as they are. Therefore, you need to modify those extensions to be compatible with WSO2 Micro Integrator. Follow the steps given below to modify an existing Log4j2 extension:
-
In the custom log appender, open the
pom.xml
file of the module that contains theLog4j2Appender
class. -
Under the
build
section, addmaven-compiler-plugin
andmaven-bundle-plugin
as follows.<plugins> ... <plugin> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <id>log4j-plugin-processor</id> <goals> <goal>compile</goal> </goals> <phase>process-classes</phase> <configuration> <proc>only</proc> <annotationProcessors> <annotationProcessor> org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor </annotationProcessor> </annotationProcessors> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> <Bundle-Name>${project.artifactId}</Bundle-Name> <Fragment-Host>org.ops4j.pax.logging.pax-logging-log4j2</Fragment-Host> <Export-Package> <PACKAGE_CONTAINS_THE_APPENDER_CLASS> </Export-Package> <DynamicImport-Package>*</DynamicImport-Package> <Import-Package></Import-Package> <Include-Resource>${project.build.directory}/classes/</Include-Resource> </instructions> </configuration> </plugin> ...
-
Rebuild the related module and copy the built JAR file from the
target
directory to<MI_HOME>/dropins
directory. -
Configure the custom appender in the
log4j2.properties
file as follows.appender.log4j2Custom.type = Log4j2Appender appender.log4j2Custom.name = log4j2Custom appender.log4j2Custom.layout.type = PatternLayout appender.log4j2Custom.layout.pattern = [%d] %5p {%c} - %m%ex%n
-
The custom appender should be added to the list of registered appenders in the
log4j2.properties
file as shown below.appenders = log4j2Custom, ....
-
Restart the server.