How to configure Logback to send specific entries to a different log file
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 Bitbucket Server logs provide a lot of information, and in some cases, you may want to have specific events logged in a separate file for easy viewing or monitoring.
Please note:
- The files you are about to modify are maintained in the <Bitbucket Server Installation> directory, so these changes will need to be re-applied manually when Bitbucket Server is upgraded. Do not copy this modified file into the new installation.
- A Bitbucket Server restart after these changes have been made is mandatory.
- For versions of Bitbucket prior to 7.0, the file to modify will be named logback.xml instead of logback-spring.xml.
- If you change the location of your log files, they will no longer be included when you generate a support zip. This means you'll need to attach your logs to any support requests manually.
Solution
These sorts of modifications can be accomplished by editing <Bitbucket Server Installation>/atlassian-bitbucket/WEB-INF/classes/logback-spring.xml
(or logback.xml
if you are using a Bitbucket version prior to 7.0).
The example below will create a separate access log named atlassian-bitbucket-access-gitclone.log.
The new log file will receive only git clone-related entries by checking only for events containing the string SSH - git-upload-pack.
Create a new appender which is a copy of the bitbucket.accesslog appender. Copy and add the lines below the bitbucket.accesslog appender:
<appender name="bitbucket.accesslog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <charset>UTF-8</charset> <pattern>${accesslog.format}</pattern> </encoder> <file>${log.dir}/atlassian-bitbucket-access.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${log.dir}/atlassian-bitbucket-access-%d.%i.log</fileNamePattern> <maxFileSize>25MB</maxFileSize> <maxHistory>10</maxHistory> </rollingPolicy> </appender> <!-- This will create a separate access log for git clone operations. --> <appender name="bitbucket.accesslog.gitclone" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator> <expression>return ((String)mdc.get("a-request-action")).contains("SSH - git-upload-pack");</expression> </evaluator> <OnMismatch>DENY</OnMismatch> <OnMatch>ACCEPT</OnMatch> </filter> <encoder> <charset>UTF-8</charset> <pattern>${accesslog.format}</pattern> </encoder> <file>${log.dir}/atlassian-bitbucket-access-gitclone.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${log.dir}/atlassian-bitbucket-access-gitclone-%d.%i.log</fileNamePattern> <maxFileSize>25MB</maxFileSize> <maxHistory>10</maxHistory> </rollingPolicy> </appender>
Add a reference to the new appender named bitbucket.accesslog.gitclone inside the bitbucket.access-log logger
Update the block of the bitbucket.access-log logger from this:
<!-- Enable access logs and direct it to its own separate appender --> <logger name="bitbucket.access-log" level="INFO" additivity="false"> <appender-ref ref="bitbucket.accesslog"/> </logger>
To this:
<!-- Enable access logs and direct it to its own separate appender --> <logger name="bitbucket.access-log" level="INFO" additivity="false"> <appender-ref ref="bitbucket.accesslog"/> <appender-ref ref="bitbucket.accesslog.gitclone"/> </logger>