groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerrywil...@gmail.com
Subject Re: @Log annotation inside Groovy Script
Date Mon, 11 Jan 2016 14:34:53 GMT
How does the child script or class get the name of the script which called it (or more importantly,
the name of the logger). It has to be passed in, or retrieved via stacktrace. Correct?

Gerald Wiltse
GSI, Inc. | Full Service JD Edwards. Guaranteed.
(d) 248.327.4661
(c) 248.893.9110
(iPhone)

> On Jan 11, 2016, at 3:57 AM, Alessio Stalla <alessiostalla@gmail.com> wrote:
> 
> Hi,
> you don't have to pass the logger around. You can configure it once in the main script
and ask for it in child scripts. Every time you invoke java.util.logging.Logger.getLogger('my-script-logger'),
the same logger is returned. This is true also if you use log4j or logback (better) instead
of java.util.logging.
> 
>> On 9 January 2016 at 02:15, Gerald Wiltse <jerrywiltse@gmail.com> wrote:
>> Thank you both for your feedback.  In my case, I don't think either suggestion will
work.  Our scripts are all executed by a third party application. We do not have control over
most environment items, nor can I pass startup options to the scripts. This puts me in a corner
case, which makes me feel like I'm going against the grain on this. 
>> 
>> For this case, everything must take place in the script, or in calls I make from
the script to other scripts, and then subsequent classes. I already call children scripts,
and pass in objects through binding.  (def shell = new GroovyShell(bindings).parse( scriptBody
)) 
>> 
>> I'm considering a few alternatives for logging: 
>> 
>> 1. Creating a logger in step 1, passing it along to scripts and classes i call
>>     a.   Tedious, yet seemingly straightforward
>>     b.   I assume possible, correct if wrong
>> 2. Creating a logger in step 1, using reflection/stacktrace to get references back
to it
>>     a.   Seems like I could make it work, but I don't even know if actually possible
>>     b.   From what i've read, seems pretty ill-advised for efficiency reasons
>> 
>> To be honest, I don't like either, and it seems that there is no elegant way to do
the logging the way I am envisioning. Usually that means "You're doing it wrong".  I guess
here, that means the third party app didn't intend for people to be instantiating, calling
custom classes, etc.  It was intended to be more basic.  They also don't support grape and
a few other items that have made me engineer some things that would have otherwise been simple
in a vanilla groovy environment. 
>> 
>> Thanks again for everyone's help. 
>> 
>> 
>> 
>> Gerald R. Wiltse
>> jerrywiltse@gmail.com
>> 
>> 
>>> On Fri, Jan 8, 2016 at 4:31 PM, Shil Sinha <shil.sinha@gmail.com> wrote:
>>> Hi Gerald,
>>> 
>>> You can apply AST transformations to script classes using http://docs.groovy-lang.org/latest/html/documentation/#_ast_transformation_customizer
. 
>>> 
>>> In your case, you'd need a script (called config.groovy in this example) that
looks like this:
>>> 
>>> withConfig(configuration) {
>>>    ast(groovy.util.logging.Log)
>>> }
>>> 
>>> When you run a script that you want @Log applied to (MyScript.groovy in this
example), you'd call it as follows:
>>> 
>>> groovy -configscript config.groovy MyScript.groovy
>>> 
>>> 
>>> 
>>>> On Fri, Jan 8, 2016 at 2:55 PM, Nelson, Erick [HDS] <Erick.Nelson@hdsupply.com>
wrote:
>>>> I’m not sure if there is a better way to handle script logging.
>>>> 
>>>> The annotations is for classes and you cannot apply it to your script class.
>>>> 
>>>> Another  disadvantage is that I don’t think you can use the logger in static
methods.
>>>> 
>>>>  
>>>> 
>>>> What you can do is ‘override’ the groovy Script class (the class that
your script is an instance of, kindof) by use the BaseScript annotation
>>>> 
>>>> Example:
>>>> 
>>>> Define your wrapper like this…
>>>> 
>>>>  
>>>> 
>>>> -bash-4.1$ cat MyScript.groovy
>>>> 
>>>> abstract class MyScript extends Script {
>>>> 
>>>>     java.util.logging.Logger log
>>>> 
>>>>     @Override def run () {
>>>> 
>>>>         log = java.util.logging.Logger.getLogger('')
>>>> 
>>>>         // any other startup stuff here….
>>>> 
>>>>         runMyScript()
>>>> 
>>>>       // any other shutdown stuff here…
>>>> 
>>>>     }
>>>> 
>>>>     abstract def runMyScript()
>>>> 
>>>> }
>>>> 
>>>>  
>>>> 
>>>> Note that I have added a script variable called log that you can then use
in your script.
>>>> 
>>>>  
>>>> 
>>>> Then in your script do something like this…
>>>> 
>>>>  
>>>> 
>>>> @groovy.transform.BaseScript(MyScript)
>>>> 
>>>> import groovy.util.*
>>>> 
>>>> log.info 'hello'
>>>> 
>>>> log.severe 'hello'
>>>> 
>>>>  
>>>> 
>>>> sample run….
>>>> 
>>>>  
>>>> 
>>>> -bash-4.1$ groovy -Djava.util.logging.config.file=logging.properties test.groovy
>>>> 
>>>> Jan 8, 2016 11:53:04 AM java.util.logging.LogManager$RootLogger log
>>>> 
>>>> INFO: hello
>>>> 
>>>> Jan 8, 2016 11:53:04 AM java.util.logging.LogManager$RootLogger log
>>>> 
>>>> SEVERE: hello
>>>> 
>>>>  
>>>> 
>>>> The advantage here is that you can now add any other startup and shutdown
code in the base script class’s run method.
>>>> 
>>>>  
>>>> 
>>>> From: Gerald Wiltse [mailto:jerrywiltse@gmail.com] 
>>>> Sent: Friday, January 08, 2016 11:01 AM
>>>> To: users@groovy.apache.org
>>>> Subject: @Log annotation inside Groovy Script
>>>> 
>>>>  
>>>> 
>>>> I just got @Log to work in some test classes, then learned that  @Log annotations
do not work in scripts. Is this true? If so, is there some workaround that exists?  I assume
this is true for all @annotations and scripts, so it seems pretty sad if there's no way to
do it. 
>>>> 
>>>>  
>>>> 
>>>> I'm basing that conclusion on this post, and my own error. 
>>>> 
>>>> http://stackoverflow.com/questions/15981182/logging-in-groovy-script
>>>> 
>>>>  
>>>> 
>>>> If that is the case, maybe someone can help me with a solution : 
>>>> 
>>>>  
>>>> 
>>>> Our use case is this: 
>>>> 
>>>>  
>>>> 
>>>> Our scripts are launched on a scheduled basis by the JVM
>>>> 
>>>> Several class files are called throughout the execution of each script
>>>> 
>>>> For each execution, we want the script and any called classes to share one
log
>>>> 
>>>> This means all logging configuration would have to be programmatic / dynamic
>>>> 
>>>>  
>>>> 
>>>> The hope is that the script can define the log based on variables at execution
time, and then the classes will somehow "Inherit" the variable from the script that called
it. 
>>>> 
>>>>  
>>>> 
>>>> Does this seem possible? 
>>>> 
>>>>  
>>>> 
>>>> Regards,
>>>> 
>>>> Jerry
>>>> 
> 

Mime
View raw message