Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I control logging in an Atlassian Stash Plugin?

Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 9, 2013

I know atlassian stash uses logback, not log4j. Using the plugin SDK with jira or other atlassian products, you can do this in your POM:

<log4jProperties>${basedir}/log4j.properties</log4jProperties>

What is the equivalent for stash? How can I control logger levels for my debug instance without having to manually copy files around? How can I bundle logging inside my plugin? (e.g. write a special log to a special file appender?) There are no examples I can find in the atlassian docs.

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 5, 2013

I now have a working example of configuring logback in a plugin which utilizes a logback.xml on your classpath. See:

https://github.com/palantir/stashbot/blob/master/src/main/java/com/palantir/stash/stashbot/logger/StashbotLoggerFactory.java

For a code example of how to configure loggers. For an example logback.xml, see:

https://github.com/palantir/stashbot/blob/master/src/main/resources/logback.xml

Thanks!

-Carl

2 votes
Zeeshan Maqbool January 30, 2014

Thanks again to Charles O'Farrell who guided me to correct solution, here I will outline if someone else wants to do that:

  • ${PLUGIN_FOLDER}/pom.xml - added following dependency
    • <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <!-- <version>1.0.11</version> version from stash-parent/pom.xml -->
      <scope>provided</scope>
      </dependency>
  • ${PLUGIN_FOLDER}/src/main/resources/atlassian-plugin.xml - add following component
    • <component key="stashbotLoggerFactory" class="com.chegg.plugin.prercv.YOURLoggerFactory"/>
  • ${PLUGIN_FOLDER}/src/main/resources/logback.xml - Created pretty similar to yours containing only one appender with similar settings (creating ROOT on package)
  • ${PLUGIN_FOLDER}/src/main/java/YOURLoggerFactory.java - Pretty much similar to what StashbotLoggerFactory is.

You will find the logs under ${PLUGIN_FOLDER}/target/container/tomcat7x/cargo-stash-home/YOUR.log

Justin Shapiro
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 26, 2016

Anyone else try this?  I tried to follow along but every time I upgrade my plugin I get an additional line in the log. I mean if I install it it logs one line to the file if I then upgrade, it logs two identical lines to the file, upgrade again, three etc. 

1 vote
ThomasA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

Hi Carl,

There are two options for modifying the logback config:

- Add a logback.xml file to the <stash home> directory, this will be merged with Stash's logback config, however this is not portable; the file will need to added once for every Stash server.

- Configure logback in your plugin Java code. You can use the ApplicationPropertiesService to get the home directory to ensure you are logging in the correct place.

Hope that helps,

Tom.

Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2013

Thanks, this is helpful, but I am still having problems. I don't quite understand what my logging commands should look like, or how I should configure the logging system. I tried to do something like what I found here:
http://stackoverflow.com/questions/7967165/how-to-configure-logback-with-java-code-to-set-log-level

but I still am not seeing log messages. Do you maybe have a code example? What should it look like when my code gets a logger? How do I tell a particular logger to output to the atlassian-stash.log (or console when I run atlas-debug)? Should I use the same logger all across my plugin, or can I have a separate logger for each class? Should I add a dependency on logback to my pom.xml and use it directly? The link above tries to avoid that using reflection and it is a royal pain...

cofarrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 30, 2013

Hi Carl,

I'm curious about exactly what you're trying to do? If you're just trying to set the levels of a logger then you can easily specify the levels in our stash-config.properties file, or at runtime using REST:

https://confluence.atlassian.com/display/STASH/Stash+debug+logging

What are you hoping to use custom appenders for? If you want to define your own customer appenders, as Thomas says you can put an extra logback.xml in the home directory:

&lt;included&gt;
&lt;appender name="myappender"&gt;
    ....
&lt;/appender&gt;
&lt;!-- I'm assuming you want to output a specific package --&gt;
&lt;logger name="my.org.something" level="DEBUG"&gt;
    &lt;appender-ref ref="myappender" /&gt;
&lt;/logger&gt;
&lt;/included&gt;

http://logback.qos.ch/manual/configuration.html

Cheers,

Charles

Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 5, 2013

Charles,

My goal was to have the plugin log by default, without having to touch stash's configuration at all. I wanted it to be zero-touch, install-the-plugin-and-it-just-works. See my accepted answer for how I ended up doing it.

Thanks!
-Carl

0 votes
Zeeshan Maqbool January 29, 2014

Carl Myers -

I have trying to create a separate log for my plugin, I looked at your code and replicated these steps, let me know if I missed anything:

  • ${PLUGIN_FOLDER}/pom.xml - added one line to load log4j.prop
    • <log4jProperties>${basedir}/log4j.properties</log4jProperties>
  • ${PLUGIN_FOLDER}/log4j.properties - added the same file as yours except updating the last line with my className in it
  • ${PLUGIN_FOLDER}/src/main/resources/logback.xml - Created pretty similar to yours containing only one appender with my className in it

Did I miss anything ?

The plugin can be executed but I dont see any log file getting generated through this setup.

0 votes
Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 29, 2013

Despite Thomas' very helpful answer, I am still having problems figuring out how to configure logback. A working example plugin that uses logback to do logging would be great... failing that, maybe some code samples of what I need to do. I've googled and tried to find logback code examples but that code doesn't seem to be working for me...

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events