Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,244
Community Members
 
Community Events
165
Community Groups

Trying to create a function(method) that logs based on a variable in groovy for scriptrunner

So I have a post function that I do a lot of logging in when on my dev environment (mostly just to debug stuff as I make changes)

example:

log.warn("The file ${jurFile.absolutePath} has ${jurFile.length()} bytes")

 

When I move the code to production I do not want these logs, I used to just comment them out as I moved to production but some scripts have a lot of logging and need to be edited more than I like. It has become a hassle to comment then un-comment.

 

Had a great idea to wrap them in a bool and I could just change that but then the code looks sloppier

ex:

If(LogBool)

{log.warn("The file ${jurFile.absolutePath} has ${jurFile.length()} bytes")}

 

next great idea was to create a function that I pass in "The file ${jurFile.absolutePath} has ${jurFile.length()} bytes" and the function checks the bool and logs as needed. I have never made a function in groovy and I am not even sure if I can do that in a post function. I have tried a few things but nothing seems to work.

 

class Log2
{
           public warn2(str)
           {
                  if(true)
                          {
                                log.warn(str)
                           }
             }
}

gives me this error

groovy.lang.MissingMethodException: No signature of method: static Log2.warn2() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [The file E:\EDrive\jira-home\scripts\data\TEST.txt has 2354 bytes]
Possible solutions: warn2(java.lang.String), wait(), any(), wait(long), any(groovy.lang.Closure), grep()

 

I have tried to qualify "str" as String and I get the same thing. What am I doing wrong?

 

1 answer

0 votes

You could instead instantiate a new log with a class name that you control.

def myLog = log.getLogger("com.acme.myspecialLog")
mylog.debug "this is a debug line"
mylog.info "this is an info line"
//you can still use the default log class
log.info "this is a scriptrunner log"

Then, from logging and profiling page in your jira admin, just set a different level for your test environment. Note, that unless you add your custom class to the log4 properties (see this page) you will need to add that class manually to jira logging and profiling after each restart.

Alternatively, set the level programmatically using your baseUrl. I would install this as a service that runs daily (rather than running this every time your script runs).

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import org.apache.log4j.Level
def myLog = log.getLogger("com.acme.myspecialLog")
def baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
if(baseUrl.contains("test"){
myLog.setLevel(Level.DEBUG)
} else {
myLog.setLevel(Level.INFO)
}

By the way, the log levels, if you are not familiar are TRACE DEBUG INFO WARN ERROR FATAL OFF.

Whichever level you set a logger to, means that calls to the log will only be included if they are at that level or below (to the right). The typical default level (for unspecified packages is WARN)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

147 views 0 3
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you