Enable logging in WSO2 Identity Server with Log4j2

Hasini Witharana
Identity Beyond Borders
3 min readSep 24, 2019

We need logging to debug our codes. Sometimes these log statements will save your day :).

Log4j is a logging library used in Java. By using Log4j we can enable logging at runtime without modifying the application binary. Logging behaviour such as logging level, enabling logging for a specific component can be controlled by editing a configuration file. Moreover, the Log4j is designed to contain the log statements in the code without causing a heavy performance cost.

On August 5, 2015, Log4j 1.x reached the end of life. For more information please refer to the blog.

Now the new version is Log4j 2. Apache Log4j 2 is an upgrade to Log4j 1.x. We will see what are the improvements of Log4j 2 over Log4j 1.x

Why Log4j 2?

There are several new features introduced in Log4j 2 compared to Log4j 1.x

1. API Separation

The API for Log4j is separate from the implementation. The Log4j API is a logging facade that may be used with the Log4j implementation, but may also be used with other logging implementations.

2. Improved Performance

Log4j 2 contains next-generation Asynchronous Loggers. Log4j 2 significantly outperforms Log4j 1.x, Logback and java.util.logging, especially in multi-threaded applications.

3. Support for multiple APIs

Log4j 2 provides support for the Log4j 1.2, SLF4J, Commons Logging and java.util.logging (JUL) APIs.

Please refer to the documentation to find more interesting features of Log4j 2.

Enable logging in WSO2 Identity Server with Log4j2

WSO2 Identity Server (WSO2 IS-5.9.0) uses Log4j2 for logging requirements. Before IS-5.9.0 we used Log4j 1.x.

You can find the log4j2.properties file under <IS-HOME>/repository/conf directory.

How to add a new Appender?

This is a sample format of an appender

appender.<Appender_name_you _define>.type = <Appender_type>

appender.<Appender_name_you _define>.name = <Real _appender_name>

appender.<Appender_name_you _define>.layout.type = <Layout_type>

After introducing the appender add the appender to the appenders list

appenders = Appender_x, <Appender_name_you _define>

Below are two examples of a Console Appender and a RollingFile Appender

  1. Console Appender

appender.CARBON_CONSOLE.type = Console

appender.CARBON_CONSOLE.name = CARBON_CONSOLE

appender.CARBON_CONSOLE.layout.type = PatternLayout

appender.CARBON_CONSOLE.layout.pattern = [%d] %5p {%c} — %m%ex%n

appender.CARBON_CONSOLE.filter.threshold.type = ThresholdFilter

appender.CARBON_CONSOLE.filter.threshold.level = DEBUG

2. RollingFile Appender

appender.CARBON_LOGFILE.type = RollingFile

appender.CARBON_LOGFILE.name = CARBON_LOGFILE

appender.CARBON_LOGFILE.fileName = ${sys:carbon.home}/repository/logs/wso2carbon.log

appender.CARBON_LOGFILE.filePattern = ${sys:carbon.home}/repository/logs/wso2carbon-%d{MM-dd-yyyy}.log

appender.CARBON_LOGFILE.layout.type = PatternLayout

appender.CARBON_LOGFILE.layout.pattern = TID: [%tenantId] [%appName] [%d] %5p {%c} — %m%ex%n

appender.CARBON_LOGFILE.policies.type = Policies

appender.CARBON_LOGFILE.policies.time.type = TimeBasedTriggeringPolicy

appender.CARBON_LOGFILE.policies.time.interval = 1

appender.CARBON_LOGFILE.policies.time.modulate = true

appender.CARBON_LOGFILE.policies.size.type = SizeBasedTriggeringPolicy

appender.CARBON_LOGFILE.policies.size.size=10MB

appender.CARBON_LOGFILE.strategy.type = DefaultRolloverStrategy

appender.CARBON_LOGFILE.strategy.max = 20

appender.CARBON_LOGFILE.filter.threshold.type = ThresholdFilter

appender.CARBON_LOGFILE.filter.threshold.level = DEBUG

How to add a new Logger

This is a sample format of a logger.

logger.<Logger_name_you _define>.name = <Real _logger_name>

logger.<Logger_name_you _define>.level =<Log_level>

logger.<Logger_name_you _define>.appenderRef.<Appender_name_you _define>.ref = <Real _appender_name>

logger.<Logger_name_you _define>.additivity = false

After introducing the logger add the logger to the loggers list.

loggers = Logger_x, <Logger_name_you _define>

Below is an example of a Logger.

logger.org-apache-axis2-wsdl-codegen-writer-PrettyPrinter.name = org.apache.axis2.wsdl.codegen.writer.PrettyPrinter

logger.org-apache-axis2-wsdl-codegen-writer-PrettyPrinter.level = ERROR

logger.org-apache-axis2-wsdl-codegen-writer-PrettyPrinter.appenderRef.CARBON_LOGFILE.ref = CARBON_LOGFILE

logger.org-apache-axis2-wsdl-codegen-writer-PrettyPrinter.appenderRef.CARBON_MEMORY.ref = CARBON_MEMORY

I hope that everyone got some idea about why we moved to Log4j2 and how to configure loggers and appenders using Log4j2.properties file. Thank you for reading.

--

--

Hasini Witharana
Identity Beyond Borders

PhD Candidate @ University of Florida. Security Research Intern @ Intel. Software Engineer @ WSO2.