How to use logging from inside Jira plugins?

What is the proper way of logging from inside Jira (5.x) plugins?

I found at least two articles and I am not sure which approach is the correct one.

Update: I am looking to obtain a proper and well documented way of implementing logging in Jira plugins, one that will have to match the following requirements:

  • Sample code to add to your plugin (including modifications to atlassian-plugin.xml or pom.xml, if needed)
  • Logged messaged should be visible inside atlassian-jira.log file
  • Not to interfere with logging from other plugins - I am asking this because the approach of inserting a log4j.properties file with your plugin seems to work agains this.
  • Be able to see all messages from your plugin when you run in development mode (example atlas-run)
  • Be able to see warnings or above when running on Jira, and this without having to tune Jira logging configuration.

Where I got so far:

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
...
public class MyJiraServlet extends HttpServlet {

    private static final Logger log = LogManager.getLogger("atlassian.plugin");
...
}

log.info("MyPlugin init"); // should see this on devel
log.error("MyPlugin error");  // should see this even on production
...
 
The problem is that now I goet tons of errors in the console, which I suppose should be real logs from my plugin

 

[INFO] [talledLocalContainer] log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
[INFO] [talledLocalContainer] log4j:ERROR The class "org.apache.log4j.Appender" was loaded by 
[INFO] [talledLocalContainer] log4j:ERROR [109.0] whereas object of type 
[INFO] [talledLocalContainer] log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [WebappClassLoader
[INFO] [talledLocalContainer]   delegate: false
[INFO] [talledLocalContainer]   repositories:
[INFO] [talledLocalContainer]     /WEB-INF/classes/
[INFO] [talledLocalContainer] ----------> Parent Classloader:
[INFO] [talledLocalContainer] org.apache.catalina.loader.StandardClassLoader@30fffd2c
[INFO] [talledLocalContainer] ].
[INFO] [talledLocalContainer] log4j:ERROR Could not instantiate appender named "console".
[INFO] [talledLocalContainer] log4j:ERROR A "com.atlassian.jira.logging.MultiTenantJiraHomeAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
[INFO] [talledLocalContainer] log4j:ERROR The class "org.apache.log4j.Appender" was loaded by 
[INFO] [talledLocalContainer] log4j:ERROR [109.0] whereas object of type 
[INFO] [talledLocalContainer] log4j:ERROR "com.atlassian.jira.logging.MultiTenantJiraHomeAppender" was loaded by [WebappClassLoader
[INFO] [talledLocalContainer]   delegate: false
[INFO] [talledLocalContainer]   repositories:
[INFO] [talledLocalContainer]     /WEB-INF/classes/
[INFO] [talledLocalContainer] ----------> Parent Classloader:
[INFO] [talledLocalContainer] org.apache.catalina.loader.StandardClassLoader@30fffd2c
[INFO] [talledLocalContainer] ].
[INFO] [talledLocalContainer] log4j:ERROR Could not instantiate appender named "filelog".
[INFO] [talledLocalContainer] log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
[INFO] [talledLocalContainer] log4j:ERROR The class "org.apache.log4j.Appender" was loaded by 
[INFO] [talledLocalContainer] log4j:ERROR [109.0] whereas object of type 
[INFO] [talledLocalContainer] log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [WebappClassLoader
[INFO] [talledLocalContainer]   delegate: false
[INFO] [talledLocalContainer]   repositories:
[INFO] [talledLocalContainer]     /WEB-INF/classes/
[INFO] [talledLocalContainer] ----------> Parent Classloader:
[INFO] [talledLocalContainer] org.apache.catalina.loader.StandardClassLoader@30fffd2c
[INFO] [talledLocalContainer] ].

 

 

2 answers

This widget could not be displayed.

You can use the approach used in the listener example. Atlassian plugins mostly follow that.

And yes, INFO level is filtered. WARN is the default. You can change it to INFO temporarily under Administration > Logging & Profiling or permentantly under log4j.properties.

Better to change only for your package.

I followed your advise but I get an error now, at runtime:

ERROR admin 829x688x1 yjfxoz 10.80.118.154 /plugins/servlet/testrun/ [atlassian.plugin.servlet.DefaultServletModuleManager] Unable to create servlet
[INFO] [talledLocalContainer] com.atlassian.util.concurrent.LazyReference$InitializationException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.myjiraplugin.MyJiraServlet': Instantiation of bean failed; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/felix/framework/ModuleImpl$ModuleClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of org/apache/catalina/loader/WebappClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type LoggerFactory; used in the signature
[INFO] [talledLocalContainer] 	at com.atlassian.util.concurrent.LazyReference.getInterruptibly(LazyReference.java:152)
[INFO] [talledLocalContainer] 	at com.atlassian.util.concurrent.LazyReference.get(LazyReference.java:115)

Is there a conflict for slf4j?

I renounced from using slf4j and used log4j - it seems to be better but still doesn't work. Please see the updated question.

Hi Jobin,

If i use Logger in my class, when i do a mvn package (in eclipse) it complains about the missing class files. So i added the dependency. But i guess this is what is stopping the messages from logging in the atlassian-jira.log. How to i remove the dependency and still compile the code?

This widget could not be displayed.

I have seen the errors that is reported in your console. This happens when there is a conflict of log4j jar.

For example, it is there both in JIRA_INSTALL_DIR/lib and in WEB-INF/lib or there is a plugin which has log4j embedded in it (because of a meven dependency).

Here is the result of looking for jar files with this class http://pastebin.com/raw.php?i=8bA5WK6i

Did I mention that I removed the target directory and I just run atlas-run after this, testrun is my plugin.

And here is the result of searching for .LoggerFactory http://pastebin.com/raw.php?i=N1k3pK4L

And these are the dependencies from pom.xml http://pastebin.com/PAvkMsjE

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 3 hours ago in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

14 views 0 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you