Expand my Community achievements bar.

SOLVED

Logging in Groovy script

Avatar

Level 2

Hi,

Is there a way to enable logger in Groovy Script? I want to print Groovy script response in the logger file.

 

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @mvelicheti,

Groovy console provides OOTB logger. So there is no need to add any imports, or create logger instance manually.

gc-logger.jpg

As you can see it is available under log variable. Below is sample code that presents how to use it.

 

log.info "Info message"
log.error "Error message"
log.debug "Debug message"
log.trace "Trace message"

 

in error.log file you can expect entries like this

 

*INFO* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Info message
*ERROR* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Error message
*DEBUG* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Debug message
*TRACE* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Trace message

 

Please be aware that what will be logged, depends also on error.log level. You can change log level that will be stored under error.log under /system/console/slinglog

Last but not least, log object represents org.slf4j.Logger class, so you can use any method exposed by it.

 

In case you would like to collect groovy console logs in separate/custom file. This can be done like this:

  1. Open Log Support view under Web Console - /system/console/slinglog
  2. Add new Logger like below
    gc-custom-file.jpg

Base on above configuration groovy console logs will be stored under groovy-console.log file.

View solution in original post

3 Replies

Avatar

Employee Advisor

Hi @mvelicheti ,

 

You need to import the dependent classes for logging, Something like this 

 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

def Logger logger = LoggerFactory.getLogger(getClass());

logger.info("Your log statement")

 

This would print your logs in the error.log file.

 

Hope this helps!

 

Regards,

Nitesh

Avatar

Correct answer by
Community Advisor

Hi @mvelicheti,

Groovy console provides OOTB logger. So there is no need to add any imports, or create logger instance manually.

gc-logger.jpg

As you can see it is available under log variable. Below is sample code that presents how to use it.

 

log.info "Info message"
log.error "Error message"
log.debug "Debug message"
log.trace "Trace message"

 

in error.log file you can expect entries like this

 

*INFO* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Info message
*ERROR* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Error message
*DEBUG* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Debug message
*TRACE* [127.0.0.1 [1685283081856] POST /bin/groovyconsole/post.json HTTP/1.1] groovyconsole Trace message

 

Please be aware that what will be logged, depends also on error.log level. You can change log level that will be stored under error.log under /system/console/slinglog

Last but not least, log object represents org.slf4j.Logger class, so you can use any method exposed by it.

 

In case you would like to collect groovy console logs in separate/custom file. This can be done like this:

  1. Open Log Support view under Web Console - /system/console/slinglog
  2. Add new Logger like below
    gc-custom-file.jpg

Base on above configuration groovy console logs will be stored under groovy-console.log file.

Avatar

Level 2

Thanks for your reply. This is working and logs are printed in custom log file