Friday, October 21, 2016

How to Limit the WSO2 Service Log File Size

Limit the file size of wso2carbon.log

By default wso2carbon.log file is rotated on daily basis. But if we want to rotate the log files based on file size we can do as follows.

  • Change the log4j.appender.CARBON_LOGFILE appender in the /repository/conf/log4j.properties file as follows. 

log4j.appender.CARBON_LOGFILE=org.apache.log4j.RollingFileAppender

  • Add following two properties under the RollingFileAppender. You can change the MaxFileSize and MaxBackupIndex as Required.

log4j.appender.CARBON_LOGFILE.MaxFileSize=10MB 
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=20


MaxFileSize = Maximum allowed file size (in bytes) before rolling over. Suffixes "KB", "MB" and "GB" are allowed.
MaxBackupIndex  = Maximum number of backup files to keep
  • Now the configuration looks like below

# CARBON_LOGFILE is set to be a DailyRollingFileAppender using a PatternLayout.
#log4j.appender.CARBON_LOGFILE=org.wso2.carbon.utils.logging.appenders.CarbonDailyRollingFileAppender
log4j.appender.CARBON_LOGFILE=org.apache.log4j.RollingFileAppender
# 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
# ConversionPattern will be overridden by the configuration setting in the DB
log4j.appender.CARBON_LOGFILE.layout.ConversionPattern=TID: [%T] [%S] [%d] %P%5p {%c} - %x %m {%c}%n
log4j.appender.CARBON_LOGFILE.layout.TenantPattern=%U%@%D [%T] [%S]
log4j.appender.CARBON_LOGFILE.threshold=DEBUG
log4j.appender.CARBON_LOGFILE.MaxFileSize=10MB
log4j.appender.CARBON_LOGFILE.MaxBackupIndex=20

If the size of the log file is exceeding the value defined in the MaxFileSize property, then the content is copied to a backup file and the logs are continued to be added to a new empty log file.For example if MaxBackupIndex is two you will see 3 files maximum. The wso2carbon.log.2 log file contains the oldest logs and wso2carbon.log contains the latest logs.


wso2carbon.log.2
wso2carbon.log.1
wso2carbon.log

Disable Http Access Logs

HTTP Requests/Responses are logged in the access log(s) and are helpful to monitor your application's usage activities, such as the persons who access it, how many hits it receives, what the errors are etc. This information is useful for troubleshooting. As the runtime of WSO2 products are based on Apache Tomcat, you can use the AccessLogValve variable in Tomcat to configure HTTP access logs in WSO2 products.
The /repository/conf/tomcat/catalina-server.xml contains the valve related to the access logs as below. If you want to disable access logs comment out the below configuration.


<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs"
 prefix="http_access_" suffix=".log"
pattern="combined"/>

Rotate Http Access Logs

We can achieve this by using "fileDateFormat". It allows a customized timestamp in the access log file name. The file is rotated whenever the formatted timestamp changes. The default value is yyyy-MM-dd. We can change it to rotate every hour by adding fileDateFormat to catalina-server.xml as follows. Tomcat itself does not do any housekeeping on the old files, so it is possible to have a cron job set up on the system to archive older files into a backup directory and/or delete them.

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs"
prefix="http_access_" suffix=".log"
pattern="combined" fileDateFormat="yyyy-MM-dd.HH"/>

2 comments:

  1. Thanks for sharing useful information. how can we automatically delete wso2 logs after certain period regardless of file size ? Kindly me know possible solution

    ReplyDelete