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

Jeremy Schrecengost September 3, 2019

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

Suggest an answer

Log in or Sign up to answer
1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 3, 2019

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)

TAGS
AUG Leaders

Atlassian Community Events