How to configure Logger in custom plugin development

Nir Zolotorevsky
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 8, 2012

Hi,

I want to use log4j logger or slf4j logger.

I want my logger to use log4j.properties file where I have configured my appenders.

it works in single class implementation when I use :

private static final Logger log = LoggerFactory.getLogger(IssueUpdatedListener.class);
 PropertyConfigurator.configure(jiraHome.getHomePath() + "\\log4j.properties");
log.error("test");

But if I have few classes it doesn't work.

I read that I need to use slf4j and configure location of log4j.properties in pom.xml

but I don't know how to do it exactly

thanks in advance

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Nir Zolotorevsky
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.
October 3, 2012

I used singletone pattern and create my own PluginLogger class as following :

public class PluginLogger {
	
	private static PluginLogger uniqLoggerInstance;
	private static final JiraHome jiraHome = ComponentAccessor.getComponentOfType(JiraHome.class);
	private static Logger log;
	
	private PluginLogger(Class curClass) {
		log=LoggerFactory.getLogger(curClass);
		PropertyConfigurator.configure(jiraHome.getHomePath() + "\\log4j.properties");
	}

	public static synchronized PluginLogger getInstance(Class curClass) {
		if (uniqLoggerInstance == null) {
			uniqLoggerInstance = new PluginLogger(curClass);
		}
		return uniqLoggerInstance;
	}
	
	public void printErrorLog(String input)
	{
		log.error(input);
	}
	public void printWarningLog(String input)
	{
		log.warn(input);
	}
	public void printInfoLog(String input)
	{
		log.info(input);
	}
	public void printDebugLog(String input)
	{
		log.debug(input);
	}

}

Now at my classes I use it as following:

public class MyClass{

private PluginLogger pluginLogger;

protected MyClass()

{

pluginLogger=PluginLogger.getInstance(MyClass.class);

}

//now print to log file

pluginLogger.printErrorLog(" test logging to file");

}

0 votes
Deleted user September 30, 2012

I think you have to pass your own class to the getLogger() method. So one logger per class. And you need to enable all messages in the log4j config file.

TAGS
AUG Leaders

Atlassian Community Events