Customizing WSO2 Log Files

Pamoda Wimalasiri
3 min readOct 4, 2019

--

http://bestbinauralbeats.org/privacy-policy/

Logging is one of the most important aspects of a production-grade server. A properly configured logging system is vital for identifying errors, security threats, and usage patterns.

All WSO2 products are shipped with log4j logging capabilities that generate administrative activities and server-side logs.

Log4j Log Level

The log level can be set specifically for each appender in the log4j.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.

  • OFF: The highest possible log level. This is intended for disabling logging.
  • FATAL: Severe error events that will presumably lead the application to abort.
  • ERROR: Error events that might still allow the application to continue running.
  • WARN: Potentially harmful situations. Maybe the use of deprecated APIs, poor use of API, possible errors
  • INFO: Informational messages that highlight the progress of the application at coarse-grained level.
  • DEBUG: Fine-grained informational events that are most useful to debug an application.
  • TRACE: Provides additional details on the behavior of events and services.

Carbon Logs of WSO2 Identity Server

The Carbon log, wso2carbon.log is a log file that covers all the management features of a product. Carbon logs are configured in the log4j.properties file (stored in the <PRODUCT_HOME>/repository/conf directory). You can easily configure carbon logs using the management console of your product also. All the changes made to log4j through the management console persists in the WSO2 Registry. However, if you modify the log4j.properties file and restart the server, the earlier log4j configuration that persisted in the registry will be overwritten.

Managing Log Growth

Log growth also can be managed by configuring the log4j.properties file accordingly.

  • Time-based log rotation
    That is, at the end of the log rotation period, a new file will be created with the appended logs and archived. The name of the archived log file will always contain the date on which the file is archived
  • Size-based log rotation
    If the size of the log file is exceeding the maximum value defined, the content is copied to a backup file and the logs are continued to be added to a new empty log file. You can also limit the maximum number of backup files.
log4j.appender.CARBON_LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.CARBON_LOGFILE.MaxFileSize=10MB
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=20

Writing to your own file

As I have mentioned above, all the server logs of the WSO2 IS will be printed to wso2carbon.log file. The following are the configs in the log4j.properties file relevant to the carbon logs.

log4j.appender.CARBON_LOGFILE=org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppender
# Log file will be overridden by the configuration setting in the DB
# This path should be relative to WSO2 Carbon Home
log4j.appender.CARBON_LOGFILE.File=${carbon.home}/repository/logs/${instance.log}/wso2carbon${instance.log}.log
log4j.appender.CARBON_LOGFILE.Append=true
log4j.appender.CARBON_LOGFILE.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
log4j.appender.CARBON_LOGFILE.layout.ConversionPattern=TID: [%T] [%S] [%d] %P%5p {%c} - %x %m %n
log4j.appender.CARBON_LOGFILE.layout.TenantPattern=%U%@%D [%T] [%S]
log4j.appender.CARBON_LOGFILE.threshold=DEBUG

In this configuration, you can change log4j.appender.CARBON_LOGFILE.File property to write the logs to a different file you want.

Writing specific logs to a different file

For troubleshooting purposes, you may need to log the events of a specific class or a package to a different file. So that you can easily track the logs.

In that case, you can define your own custom appender and use this appender for the required class/package.

As an example let’s say that you want to write the DEBUG logs of the org.wso2.carbon.identity.application.authentication.framework package to a file named, framework.log file. You can achieve this by adding the following configs to the log4j.properties file. I have created a new appender with the name CUSTOM_LOGGER.

log4j.appender.CUSTOM_LOGGER=org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppender
log4j.appender.CUSTOM_LOGGER.File=${carbon.home}/repository/logs/framework.log
log4j.appender.CUSTOM_LOGGER.Append=true
log4j.appender.CUSTOM_LOGGER.layout=org.wso2.carbon.utils.logging.TenantAwarePatternLayout
log4j.appender.CUSTOM_LOGGER.layout.ConversionPattern=TID: [%T] [%S] [%d] %P%5p {%c} - %x %m %n
log4j.appender.CUSTOM_LOGGER.layout.TenantPattern=%U%@%D [%T] [%S]
log4j.appender.CUSTOM_LOGGER.threshold=DEBUG
log4j.logger.org.wso2.carbon.identity.application.authentication.framework=DEBUG,CUSTOM_LOGGER

When you make these changes, save the file and restart the server, you can see that the logs from the given package will be written to the separate file.

That’s it for today.😄 See you soon with another interesting blog. 👋

--

--

Pamoda Wimalasiri

Associate Technical Lead @ WSO2 focused on the IAM domain