Configuring log4j in Confluence to send specific entries to a different log file

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Purpose

The Confluence administrator relies on the application logs to monitor its instance and to troubleshoot issues. From Confluence 7.11, we already send entries relating to indexing, security, outgoing mail and Synchrony to separate files, but it may be helpful to send other specific entries to a file different from atlassian-confluence.log.

For example, if the Confluence administrator wants to debug Import/Export operations without flooding the default application logs, you could configure log4j to send these messages to a file named atlassian-confluence-import-export.log.

This type of configuration can be achieved in two ways:

  1. Configuring log4j to send all messages related to specific classes to a different file.
  2. Configuring log4j to send all messages matching a specific pattern to a different file.

The next section of this document describes both scenarios with different examples.

Solution

The below Sections explain with examples how to configure log4j to redirect entries from the default application log to a different file.

Redirecting all messages from specific classes

In this example, we will redirect import / export related messages to a different log file.

  1. Identify your log4j.properties file and save a backup copy.
    • We will perform modifications in the log4j.properties file, so it is recommended to save a backup of that file prior to any changes.
    • This file is located at <Confluence Installation>/confluence/WEB-INF/classes/log4j.properties

  2. Determine the following information about the new log file you are creating.
    • The name of the file – in our example it is atlassian-confluence-import-export.log.
    • The folder where it will be stored – in our example it is the Confluence local home folder, which is /var/atlassian/confluence/logs.
    • The maximum file size – in our example it is 20MB.
    • The maximum number of historical files to keep – in our example we will keep 5 files.
    • The appender name – in our example we will define it as importexportlog.

  3. Configure the new log file in log4j.properties.
    • Add the following entries at the bottom of the file changing the value of each parameter based on the definitions from the previous step.

    • Refer to the Apache Documentation for detailed information about this configuration.

      #####################
      ####### Custom import export log
      #####################
      log4j.appender.importexportlog=org.apache.log4j.RollingFileAppender
      log4j.appender.importexportlog.File=/var/atlassian/confluence/logs/atlassian-confluence-import-export.log
      log4j.appender.importexportlog.Threshold=DEBUG
      log4j.appender.importexportlog.MaxFileSize=20480KB
      log4j.appender.importexportlog.MaxBackupIndex=5
      log4j.appender.importexportlog.layout=com.atlassian.confluence.util.PatternLayoutWithContext
      log4j.appender.importexportlog.layout.ConversionPattern=%d %p [%t] [%c{4}] %M %m%n
  4. Determine the full package name of classes that you want messages to be sent to the new log file.
    • In our example, the classes related to import and export are highlighted below.

      com.atlassian.confluence.importexport.actions.ImportLongRunningTask
      com.atlassian.confluence.importexport.impl.PdfExporter
  5. Determine the logging level for each class from the previous step.
    • In this example, we want messages from DEBUG and below for all classes.

  6. Add at the bottom of the file a configuration to redirect messages to the new log file.
    • For each class, there should be two entries following the example below.

      log4j.logger.<package name>=<logging level>, <appender name>
      log4j.additivity.<package name>=false
    • In our example, we should use the following configuration.

      #####################
      ####### Import export definitions
      #####################
      log4j.logger.com.atlassian.confluence.importexport.actions.ImportLongRunningTask=DEBUG, importexportlog
      log4j.additivity.com.atlassian.confluence.importexport.actions.ImportLongRunningTask=false
      log4j.logger.com.atlassian.confluence.importexport.impl.PdfExporter=DEBUG, importexportlog
      log4j.additivity.com.atlassian.confluence.importexport.impl.PdfExporter=false
  7. Save the log4j.properties file and restart Confluence.
    • If running Confluence Data Center in a cluster you will need to follow these steps on each node.


Following the above configuration will divert import and PDF export entries to the new log file (atlassian-confluence-import-export.log).

Redirecting all messages matching a specific pattern

In this example, we will redirect messages matching a specific pattern to a separate log file. We need first to filter out these messages from the default log file and then create a new, separated log file to it.
For this purpose, we will use the same example presented in Configuring log4j to filter out unwanted messages in Confluence Data Center application log.


  1. Follow the steps in this KB to filter out the target messages from the default Confluence application log.
    1. Don't restart Confluence after finishing this configuration since we will still touch the log4j configuration in the next steps.

  2. Determine the following information about the new log file you are creating.
    • The name of the file – in our example it is atlassian-confluence-brokenpipe.log.
    • The folder where it will be stored – in our example it is the Confluence local home folder, which is /var/atlassian/confluence/logs.
    • The maximum file size – in our example it is 20MB.
    • The maximum number of historical files to keep – in our example we will keep 5 files.
    • The appender name – in our example we will define it as brokenpipelog.

  3. Configure the new log file in log4j.properties.
    • Add the following entries at the bottom of the file changing the value of each parameter based on the definitions from the previous step.

    • Refer to the Apache Documentation for detailed information about this configuration.

      #####################
      ####### Custom broken pipe entries log
      #####################
      log4j.appender.brokenpipelog=org.apache.log4j.RollingFileAppender
      log4j.appender.brokenpipelog.File=/var/atlassian/confluence/logs/atlassian-confluence-brokenpipe.log
      log4j.appender.brokenpipelog.Threshold=DEBUG
      log4j.appender.brokenpipelog.MaxFileSize=20480KB
      log4j.appender.brokenpipelog.MaxBackupIndex=5
      log4j.appender.brokenpipelog.layout=com.atlassian.confluence.util.PatternLayoutWithContext
      log4j.appender.brokenpipelog.layout.ConversionPattern=%d %p [%t] [%c{4}] %M %m%n
  4. Add at the bottom of the file a configuration to accept the target messages on the new log file.
    • For each matching pattern, there should be three entries following the example below.

      log4j.appender.<log name>.filter.<sequential number>=org.apache.log4j.varia.StringMatchFilter
      log4j.appender.<log name>.filter.<sequential number>.StringToMatch=<matching pattern>
      log4j.appender.<log name>.filter.<sequential number>.AcceptOnMatch=true
    • In our example, we should use the following configuration.

      #####################
      ####### Custom broken pipe entries filters
      #####################
      log4j.appender.brokenpipelog.filter.1=org.apache.log4j.varia.StringMatchFilter
      log4j.appender.brokenpipelog.filter.1.StringToMatch=for servlet [file-server] threw exception
      log4j.appender.brokenpipelog.filter.1.AcceptOnMatch=true
  5. Add at the end of the filtering configuration an entry to deny any other message as below.

    log4j.appender.<log name>.filter.<sequential number>=org.apache.log4j.varia.DenyAllFilter
  6. At the end, the new set of configuration in log4j.properties should look like the below example.

    #####################
    ####### Custom broken pipe entries log
    #####################
    log4j.appender.brokenpipelog=org.apache.log4j.RollingFileAppender
    log4j.appender.brokenpipelog.File=/var/atlassian/confluence/logs/atlassian-confluence-brokenpipe.log
    log4j.appender.brokenpipelog.Threshold=DEBUG
    log4j.appender.brokenpipelog.MaxFileSize=20480KB
    log4j.appender.brokenpipelog.MaxBackupIndex=5
    log4j.appender.brokenpipelog.layout=com.atlassian.confluence.util.PatternLayoutWithContext
    log4j.appender.brokenpipelog.layout.ConversionPattern=%d %p [%t] [%c{4}] %M %m%n
    #####################
    ####### Custom broken pipe entries filters
    #####################
    log4j.appender.brokenpipelog.filter.1=org.apache.log4j.varia.StringMatchFilter
    log4j.appender.brokenpipelog.filter.1.StringToMatch=for servlet [file-server] threw exception
    log4j.appender.brokenpipelog.filter.1.AcceptOnMatch=true
    log4j.appender.brokenpipelog.filter.2=org.apache.log4j.varia.DenyAllFilter
  7. Edit the log4j.rootLogger (this is at the top of the log4j.properties file) attribute to include the new log file.

    log4j.rootLogger=WARN, confluencelog, errorlog, <log name>


    • This will be the configuration in our example.

      log4j.rootLogger=WARN, confluencelog, errorlog, brokenpipelog
  8. Save the log4j.properties file and restart Confluence.
    • If running Confluence Data Center in a cluster you will need to follow these steps on each node.


See Also

Last modified on Aug 25, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.