Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5F29218874 for ; Fri, 8 Jan 2016 21:31:55 +0000 (UTC) Received: (qmail 40377 invoked by uid 500); 8 Jan 2016 21:31:55 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 40350 invoked by uid 500); 8 Jan 2016 21:31:55 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 40335 invoked by uid 99); 8 Jan 2016 21:31:55 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 08 Jan 2016 21:31:55 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id AE22118050A for ; Fri, 8 Jan 2016 21:31:54 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 4.651 X-Spam-Level: **** X-Spam-Status: No, score=4.651 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_REPLY=1, HTML_MESSAGE=3, KAM_INFOUSMEBIZ=0.75, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd3-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id 2u0gAhWeihEK for ; Fri, 8 Jan 2016 21:31:41 +0000 (UTC) Received: from mail-io0-f176.google.com (mail-io0-f176.google.com [209.85.223.176]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with ESMTPS id 985C42059F for ; Fri, 8 Jan 2016 21:31:40 +0000 (UTC) Received: by mail-io0-f176.google.com with SMTP id 1so252293391ion.1 for ; Fri, 08 Jan 2016 13:31:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=7xxqukwS005ylltntZ8AxsWP1XbaEzvfBxkxZU/hBy8=; b=gHYVcJ9EZNc8tcxATlbSDNOsd0kP6U8abm6RjasjHdUhAKl1LCxigcdlAgNQ0vs7ez 3XBZolVgEzyPL3rqRT5540ARh/gTe16KBme7ipv7Lel+aBE77Tvx8aV0z7M7uFdvH/gc 0JPNc460dil3XCn+lbJEWisM4L6hVloHudFISh5t3jhpUG4+gUAikZekAiaHmmxu5maA qQF7troZBQdC2kfMyGGZe8KK6RRhuby4sXoGDElzkmZrvYaCEZbXNAToORYCs8bAh1Mj PaZBB41aUSdBqhhwJRo0RkbcJ/Pgxw72IZEVa+WQ23ABnSokbWQ+dxYJhS2+bDfvMmnY 9yCA== MIME-Version: 1.0 X-Received: by 10.107.136.208 with SMTP id s77mr100022921ioi.142.1452288699607; Fri, 08 Jan 2016 13:31:39 -0800 (PST) Received: by 10.79.21.197 with HTTP; Fri, 8 Jan 2016 13:31:39 -0800 (PST) In-Reply-To: References: Date: Fri, 8 Jan 2016 16:31:39 -0500 Message-ID: Subject: Re: @Log annotation inside Groovy Script From: Shil Sinha To: users@groovy.apache.org Content-Type: multipart/alternative; boundary=001a113ea6ee0d30950528d95026 --001a113ea6ee0d30950528d95026 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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=E2=80=99m 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=E2=80=99t think you can use the logge= r in > static methods. > > > > What you can do is =E2=80=98override=E2=80=99 the groovy Script class (th= e class that your > script is an instance of, kindof) by use the BaseScript annotation > > Example: > > Define your wrapper like this=E2=80=A6 > > > > -bash-4.1$ cat MyScript.groovy > > abstract class MyScript extends Script { > > java.util.logging.Logger log > > @Override def run () { > > log =3D java.util.logging.Logger.getLogger('') > > // any other startup stuff here=E2=80=A6. > > runMyScript() > > // any other shutdown stuff here=E2=80=A6 > > } > > 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=E2=80=A6 > > > > @groovy.transform.BaseScript(MyScript) > > import groovy.util.* > > log.info 'hello' > > log.severe 'hello' > > > > sample run=E2=80=A6. > > > > -bash-4.1$ groovy -Djava.util.logging.config.file=3Dlogging.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=E2=80=99s 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 on= e > 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 > > > --001a113ea6ee0d30950528d95026 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Gerald,

You can apply AST transforma= tions to script classes using http= ://docs.groovy-lang.org/latest/html/documentation/#_ast_transformation_cust= omizer .=C2=A0

In your case, you'd need a = script (called config.groovy in this example) that looks like this:

withConfig= (configuration) { ast(groovy.util.loggi= ng.Log) }
<= br>
When you run a script that you want @Log app= lied to (MyScript.groovy in this example), you'd call it as foll= ows:

groo= vy -configscript config.groovy MyScript.groovy

<= div>

On Fri, Jan 8, 2016 at 2:55 PM, Nelson, Erick [HDS] &l= t;Erick.Nels= on@hdsupply.com> wrote:

I=E2=80=99m not sure if t= here is a better way to handle script logging.

The annotations is for cl= asses and you cannot apply it to your script class.

Another =C2=A0disadvantag= e is that I don=E2=80=99t think you can use the logger in static methods.

=C2=A0

What you can do is =E2=80= =98override=E2=80=99 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=E2=80=A6

=C2=A0

-bash-4.1$ cat MyScript.g= roovy

abstract class MyScript e= xtends Script {

=C2=A0=C2=A0=C2=A0 java.u= til.logging.Logger log

=C2=A0=C2=A0=C2=A0 @Overr= ide def run () {

=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 log =3D java.util.logging.Logger.getLogger('')

=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 // any other startup stuff here=E2=80=A6.<= /span>

=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 runMyScript()

=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0// any other shutdown stuff here=E2=80=A6<= /p>

=C2=A0=C2=A0=C2=A0 }

=C2=A0=C2=A0=C2=A0 abstra= ct def runMyScript()

}

=C2=A0

Note that I have added a = script variable called log that you can then use in your script.<= /u>

=C2=A0

Then in your script do so= mething like this=E2=80=A6

=C2=A0

@groovy.transform.BaseScr= ipt(MyScript)

import groovy.util.*

log.info 'hello'

log.severe 'hello'= ;

=C2=A0

sample run=E2=80=A6.

=C2=A0

-bash-4.1$ groovy -Djava.= util.logging.config.file=3Dlogging.properties test.groovy

Jan 8, 2016 11:53:04 AM j= ava.util.logging.LogManager$RootLogger log

INFO: hello=

Jan 8, 2016 11:53:04 AM j= ava.util.logging.LogManager$RootLogger log

SEVERE: hello

=C2=A0

The advantage here is tha= t you can now add any other startup and shutdown code in the base script cl= ass=E2=80=99s run method.

=C2=A0

From: Gerald W= iltse [mailto:je= rrywiltse@gmail.com]
Sent: Friday, January 08, 2016 11:01 AM
To: use= rs@groovy.apache.org
Subject: @Log annotation inside Groovy Script

=C2=A0

I just got @Log to work in some test classes, then l= earned that =C2=A0@Log annotations do not work in scripts. Is this true? If= so, is there some workaround that exists?=C2=A0 I assume this is true for = all @annotations and scripts, so it seems pretty sad if there's no way to do it.=C2=A0

=C2=A0

I'm basing that conclusion on this post, and my = own error.=C2=A0

=C2=A0

If that is the case, maybe someone can help me with = a solution :=C2=A0

=C2=A0

Our use case is this:=C2=A0

=C2=A0

Our scripts are launched on a scheduled basis by the= JVM

Several class files are called throughout the execut= ion of each script

For each execution, we want the script and any calle= d classes to share one log

This means all logging configuration would have to b= e programmatic / dynamic

=C2=A0

The hope is that the script can define the log based= on variables at execution time, and then the classes will somehow "In= herit" the variable from the script that called it.=C2=A0

=C2=A0

Does this seem possible?=C2=A0

=C2=A0

Regards,

Jerry

=C2=A0


--001a113ea6ee0d30950528d95026--