February 25th, 2009

Jira Plugin & Log4J

I came across this problem lately when I was working on a Jira plugin. A Jira plugin does not rely on a OSGi container or some similar plugin framework. The plugin consists mainly of a jar file that gets deployed with (I mean inside…) the Jira Java EE web application.

Logging is an issue in such an environment: the surrounding application configures the logging API – there is no way to provide log4j configuration fragments.

The best way I found to solve the problem was (again) to use the native logging API to the configuration manually. Since Jira has already setup the logging API, all I needed was to add some categories with some levels, or, if I need to, create my own appenders. That’s all possible with the API exposed by log4j.

So I created a Jira component module inside my plugin that does the configuration magic. Jira uses PicoContainer (lightweight, embedable, configurable, POJO based IOC/DI component runtime) as the runtime for their own and for components provided by plugins. As components get initialized along with the Jira application it is good place to implement the required logging configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package x.y;
 
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
public class LogConfigurationComponent {
 
	private static final Logger LOG = Logger.getLogger(LogConfigurationComponent.class);
 
	public LogConfigurationComponent() {
 
		Level level = Level.toLevel("DEBUG");
		Logger logger = Logger.getLogger("x.y.somethingcool");
 
		Level oldLevel = logger.getLevel();
		logger.setLevel(level);
 
		String message = "Changing loglevel for logger "
				+ logger.getName() + " from " + oldLevel.toString()
				+ " to " + level.toString();
 
		LOG.info(message);	
	}
}

Just by adding this component to the atlassian-plugin.xml in my plugin I solved my problems with distributed logging configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
<atlassian-plugin key="x.y.coolthings"
	name="CoolThings">
	<plugin-info>
		<description>Simple plugin</description>
		<version>1.0</version>
		<vendor name="PGT Technology Scouting" url="http://www.pgt.de" />
	</plugin-info>
 
	<component key="logConfig" name="The logging configuration for this plugin"
		class="x.y.LogConfigurationComponent">
	</component>
	<!-- ... -->
</atlassian-plugin>

1 comment to Jira Plugin & Log4J

  • Ulrich

    I can’t see how the logging of the jira plugin could be configured by the global log4j.properties file. You need to rebuild every time you want to change the log level.
    Can the plugin (classloader) count on the log4j.properties resource file being available?
    I have the following problem : the jira plugin v2 classload does not “see” the global log4j.properties file. It works fine with a local /src/main/resources/log4j.properties file though. Again – this gets packaged into the plugin jar and log levels can’t be changed outside the plugin.

Leave a Reply