Forum Discussion

JPT_ENT's avatar
JPT_ENT
Occasional Contributor
16 years ago

Re: Groovy scripts and logging

Hello All,

A quick question on logging details from within a groovy class...

I'm using soapUI Pro, and am logging test results in a groovy script using the log.info commnd. This logs the output under the "Script Log" tab on the UI. Within the same groovy script, I'm calling a groovy class which also has logging implemented in it. However, the command log.info isn't recognised for the groovy class, so I've tried using SoapUI.log.info. It does log the output, but it logs it to the "SoupUI Log" in the UI.

How can I log to the Script Log from within a groovy class?

Cheers
JPT

5 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    I think you would have to pass a reference to the current logger to the class in some way.
  • JPT_ENT's avatar
    JPT_ENT
    Occasional Contributor
    Thanks for the reply...That sounds sensible, but I haven't got a clue how I'd go about doing that.

    There must be an easier way to log directly to the Scripts Log. If not, I've just wasted most of my day setting up groovy classes in the script library.
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Could you just add a parameter to your method calls for the log reference, or make a constructor that takes the log as a parameter?
  • JPT_ENT's avatar
    JPT_ENT
    Occasional Contributor
    Yes, it's not problem for me to pass a parameter through to the method calls, it's just that I don't know how to confgure the logger after doing so. I think all the logging is handled by log4j which is something I don't have much experiance with. I certainly don't know how I'd go about getting it to point to an alternative log file.

    I think I'll have to take the logging out of my classes and put it back into groovy for the time being.


    Thanks for you help...
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Sorry, I must have misunderstood. I thought you wanted the logging from your Groovy class to go to the Script log tab.

    If I create this class in my script library

    class MyClass {
    def log

    MyClass(def log) { this.log = log }
    def logMe(def msg) { log.info msg }
    }


    and call it in a Groovy script step

    import MyClass 
    def m = new MyClass(log)
    m.logMe("Hello")


    I see the output in the Script log.