Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Asked in Jira Service Desk

Calling all Insight users, we need your help!

Hello Insight users,  As part of our (Mindville's) acquisition by Atlassian, our training team is looking to build some new Insight training materials. It would really helpful if you can ...

162 views 1 1
View question

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