Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

[scriptrunner] Logs stop working when accessing value of scripted field

Erik Buchholz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2020

If I access a scripted field with a scriptrunner script the log stops working.

I thought, I could save some time and use the value of the scripted field and it is there but the logs stop working.

Can you confirm this behavior? Is this a bug?

See the following code.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

import groovy.transform.BaseScript
import com.onresolve.scriptrunner.runner.customisers.ContextBaseScript

@BaseScript ContextBaseScript script
Issue issue = getIssueOrDefault('TEST-123')

final CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
final String cfName = "my scripted custom field"
CustomField cField = customFieldManager.getCustomFieldObjectsByName(cfName)?.first()
log.error("Log still works: You can see this.")
def cfValue = issue.getCustomFieldValue(cField)
log.error("Log stops working if custom field is a scripted field: You can't see this.")
log.error("Custom field value: '${cfValue}' of class '${cfValue.getClass()}'.")
return "Custom field value: '${cfValue}' of class '${cfValue.getClass()}'."

You can run this script from console. Replace "my scripted custom field" with the name of your scripted field and 'TEST-123' with an issue that has the field and an value inside.

1 answer

0 votes
Vincent Lee May 17, 2020

Hi @Erik Buchholz ,

I meet the similar problem that Log stop working after I call function.

Did you solve this problem?

Erik Buchholz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 18, 2020

Hi @Vincent Lee ,

I solved the problem in the way, that I don't use the scripted field in the other script.

I moved the code of the scripted field to a method of a helper class. Then I call this method from the scripted field script and the other one.

Best regards,
Erik

Vincent Lee May 18, 2020

Hi @Erik Buchholz ,

Although I try your method that moved the code to a method of class, it still not write log after excute that function.

But still thanks for yuor reply.

 

Best regards,

Vincent

Erik Buchholz
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 18, 2020

Hi @Vincent Lee ,

sorry, if my explanation was not clear where you should put the method and the helper class. I have three files.

First the file with the helper class.

package example.package.helper

import org.apache.log4j.Logger

class HelperClass {
String calculationMethod() {
String result
Logger log = Logger.getLogger(getClass())
log.error("First log message from inside the calculation method.")
// calculate custom scripted field
log.error("Last log message from inside the calculation method.")
return result
}
}

The second file with the code for the scripted field

package example.package.field

import example.package.helper.HelperClass

import org.apache.log4j.Logger

Logger log = Logger.getLogger(getClass())

HelperClass helperClass = new HelperClass()
log.error("Logging here is not necassary because we just call the method and the method has it's own logging.")
return helperClass.calculationMethod()

The third file contains the new script that uses the value of the scripted field.

package example.package.action

import example.package.helper.HelperClass

import org.apache.log4j.Logger

Logger log = Logger.getLogger(getClass())

log.error("We can see this log entry.")
HelperClass helperClass = new HelperClass()
String customfield = helperClass.calculationMethod()
log.error("We can also see this log entry.")
// do some further scripting

If you still don't see the logs, it could be that you need to check the log level in this example for example.package. Can you see the first log entry?

If you don't have script files but inline scripts please check the documentation about how to use files e.g. https://scriptrunner.adaptavist.com/6.0.2/jira/#_script_roots

Like Vincent Lee likes this

Suggest an answer

Log in or Sign up to answer