Advanced Logging
What is the variable 'log'?
The log
variable is injected into every ScriptRunner inline script, and its value is equivalent to:
groovydef log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
log
is an instance of a Logger.
Using your own logger
It might be preferable to use your own logger so that you can adjust logging levels separately from ScriptRunner as a whole.
In an inline script, or file, you can do this using the following code:
groovyimport org.apache.log4j.Logger def log = Logger.getLogger("com.acme.workflows") log.warn("Workflow function running...")
If you use classes the simplest way to get a logger is to use the @Log4j
annotation:
groovypackage com.acme.workflows import groovy.util.logging.Log4j @Log4j class Foo { void utilityMethod() { log.warn "Foo.utilityMethod" } }
In the above example, the log
instance is automatically created and will have the category com.acme.workflows.Foo
, that is, the fully qualified class name.
This is the best way of logging, as it will automatically wrap calls to the logger in the relevant guarding function, e.g. if (log.isDebugEnabled()) {log.debug(…)}
Bamboo uses log4j 1x, if browsing the documentation be sure you are not looking at the documentation for log4j 2x.
Log Levels
A log message will only be printed if the "level" is the same or higher than the configured level for that category. This is a longer explanation here.
Take a note of the default log level before making any changes. The default log level for most categories is WARN
. Therefore, if you wanted to use log.debug
, the category level would need to be set to DEBUG
or TRACE
.
Temporarily enable ScriptRunner logging under System>Logging and Profiling in the Administration menu. Set the com.onresolve
package to DEBUG under Default Loggers.
This sets the logging level until the instance is restarted or the logging level is changed manually (to the default WARN for example).
You can only do this if there was already an entry for the category in log4j.properties.
You can change logging levels permanently by adjusting the log4j.properties files - see the Bamboo documentation.
Logging to Build Logs Tab
You can write to the Build Logs tab by using the addBuildLogEntry
method from the buildLogger
binding variable. The buildLogger
is available for the scriptable and custom script condition tasks as you can see in the example below:
buildLogger.addBuildLogEntry("Add here some log to show in build Logs tab")
The buildLogger
binding variable is an instance of BuildLogger.