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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,455,281
Community Members
 
Community Events
175
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

Atlassian Community Events