Logging in a plugin for Bitbucket server?

I’m having trouble configuring a logger during plugin development for Bitbucket server.
I’m not getting any output to neither the console nor a file.

I have a src/aps/log4j.properties like below,

log4j.rootLogger=INFO, console, filelog

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=ALL
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss} %5p [%t] - %m%n

log4j.appender.filelog=org.apache.log4j.RollingFileAppender
log4j.appender.filelog.layout=org.apache.log4j.PatternLayout
log4j.appender.filelog.layout.ConversionPattern=%d{HH:mm:ss} %5p [%t] - %m%n
log4j.appender.filelog.File=C:\\temp\info.log
log4j.appender.filelog.MaxFileSize=512KB
log4j.appender.filelog.MaxBackupIndex=3
# Plugin logging
log4j.logger.com.company.impl = DEBUG, console, filelog

and in the pom.xml (along with dependencies for both slf4j-api, slf4j-simple and log4j under project.build.plugins.plugin.configuation I have

<log4jProperties>src/aps/log4j.properties</log4jProperties>

And finally in my JavaClass I have sth like:

package com.company.impl;
public class myclass {
    private static final Logger log = LoggerFactory.getLogger(myclass.class);

    public myclass(){
        log.info("Constructed");
    }
}

It feels like I’m missing something incredibly basic, but I can’t put my finger on it and “Bitbucket log4j.properties” is not giving me anything onhere or Google.

Pretty sure bitbucket uses Logback, not log4j?

That’s great! I’m not married to log4j.

How do I get logging with logback?

I tried adding the following to a src/main/resources/logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}
                %X{sourceThread} - %msg%n</Pattern>
        </layout>
    </appender>
    <appender name="filelog" class="ch.qos.logback.core.FileAppender">
        <file>C:/temp/log.log</file>
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>
    <!-- default is DEBUG -->
    <root level="DEBUG">
        <appender-ref ref="console" />
        <appender-ref ref="filelog" />
    </root>
</configuration>

But to no avail :frowning_face:

You can place log4j.properties files in src/main/resources folder and the below dependency

log4j log4j 1.2.17

in your pom.xml file. It worked for me.

To enable debugging of your Bitbucket plugin using Logback:

  1. Create the directory src/test/resources/bitbucket-home
  2. Create a file src/test/resources/bitbucket-home/logback.xml [1] and add logger elements as desired, eg I have:
<included>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.format}</pattern>
        </encoder>
    </appender>
    <!--When starting up Elasticsearch in-process (for dev):
    We need to set the logging level of org.elasticsearch.bootstrap to ERROR because otherwise
    it will log a misleading warning (with a stacktrace) about JNA being unavailable.-->
    <logger name="org.elasticsearch.bootstrap" level="ERROR"/>
    <logger name="com.declarativesystems" level="DEBUG"/>

    <root>
        <appender-ref ref="console"/>
    </root>
</included>
  1. atlas-clean && atlas-run as normal - you will now be able to see the logs from your plugin on the console.

Log4J
I’m using Bitbucket 5.16 which uses Logback. I couldn’t get log4j working at all:

Log4j 1.2 in pom.xml causes my whole plugin to fail:

Caused by: org.osgi.framework.BundleException: Unresolved constraint in bundle com.declarativesystems.bitbucket.credentialscanner [126]: Unable to resolve 126.0: missing requirement [126.0] osgi.wiring.package; (osgi.wiring.package=com.ibm.uvm.tools)
        at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)
        ... 22 common frames omitted

Using the OSGi version

        <dependency>
            <groupId>org.apache.logging.log4j.osgi</groupId>
            <artifactId>log4j-core-osgi-reduced</artifactId>
            <scope>provided</scope>
            <version>2.0-rc1</version>
        </dependency>

Doesn’t break anything but doesn’t give any debug output either

HTH

[1] This is a homedir override, as discussed in https://ecosystem.atlassian.net/projects/AMPS/issues/AMPS-1505 … I would never have found this workaround without this ticket!