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

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

Multiple Classes ... Should I inject, or make Global?

Hello,

I am starting to create "packages" so that my scripting is more stable and more scalable. I am using Jira Server / Scriptrunner as my means of executing said scripts.

For instance, I created a "logger" package to control default behavior on what gets logged and what does not.  "debug" and "info" are normally toggled off, but "error" is normally toggled on

e.g.:

import utils.LogThis

log = new LogThis()
log.enableDebug()
log.debug("This will log because I just turned debug on")
log.info("This will NOT log because info is toggled off by default")

Backstory: I have a "controller" script that imports two other classes.  Logger and Assignee.  Logger is what I just explained, and the Assignee Class handles logic to "re-assign" an issue to another person.  Simple enough.

Issue: I want to set the "log level" for my class in the initial script and have both Logger and Assignee instruction to respect that state.  This is not going to happen if I am "newing" up LogThis in each of the two classes.

Normally, to solve this, I would "inject" the Logger into each class when I "new it up". 

assignee = new Assignee(log, $issue, $assignee)

Is that best way to handle this in Groovy?  I like injection and would normally make a "system" object where I store any / all frameworky objects and just inject that as SOP.

system = new System()

system.log = new LogThis()

system.log.enableDebug()

assignee = newAssignee(system, $issue, $assignee)

As each "script" is compiled to an object, I am thinking I can not really "global" the LogThis as it is probably not available to another "imported" script just by not typecasting / def'ing the variable.

Just looking for "Best Practices" on something like this.   

Thanks in advance

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

551 views 15 18
Read article

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