logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benjamin Jaton <benjamin.ja...@gmail.com>
Subject Re: Log4j2 script to generate hash
Date Fri, 11 Nov 2016 17:09:18 GMT
Reported here: https://issues.apache.org/jira/browse/LOG4J2-1693

On Thu, Nov 10, 2016 at 5:15 PM, Matt Sicker <boards@gmail.com> wrote:

> Please do open an issue. Otherwise, this will probably get lost in the
> mailing lists.
>
> On 10 November 2016 at 18:33, Benjamin Jaton <benjamin.jaton@gmail.com>
> wrote:
>
> > I have a weird problem where using a global script seems to cause log4j
> to
> > not be able to resolve my properties any more:
> >
> > Log4j2 2.7
> >
> > The code:
> >         System.setProperty("log4j.configurationFile",
> > "/usr/local/apps/test.json");
> >         LogManager.getLogger(Test.class).error("test");
> >
> > The conf:
> > {
> >   "configuration" : {
> >     "status" : "warn",
> >     "scripts": {
> >       "ScriptFile": { "name":"test.filter", "path": "/tmp/test.groovy" }
> >     },
> >     "properties" : {
> >       "property" : [ {
> >         "name" : "server.log.level",
> >         "value" : "INFO"
> >       } ]
> >     },
> >     "appenders" : {
> >       "appender" : [ {
> >         "type" : "Console",
> >         "name" : "STDOUT",
> >         "PatternLayout" : {
> >           "pattern" : "%m (Logger log level = ${server.log.level})%n"
> >         }
> >       } ]
> >     },
> >     "loggers" : {
> >       "root" : {
> >         "level" : "${server.log.level}",
> >         "AppenderRef" : {
> >           "ref" : "STDOUT"
> >         }
> >       }
> >     }
> >   }
> > }
> >
> > The output without the highlighted section:
> >   test (Logger log level = INFO)
> >
> > The output with the highlighted section:
> >   2016-11-10 16:30:25,351 main WARN Error while converting string
> > [${server.log.level}] to type [class org.apache.logging.log4j.Level].
> > Using
> > default value [null].
> >   test (Logger log level = ${server.log.level})
> >
> > The script /tmp/test.groovy only contains "return true"
> >
> > I think I'll open a bug for this if you're ok with it.
> >
> > Thanks
> > Ben
> >
> >
> > On Thu, Nov 10, 2016 at 3:59 PM, Benjamin Jaton <
> benjamin.jaton@gmail.com>
> > wrote:
> >
> > > Ah, indeed, I have access to a variable named "logger", excellent.
> > >
> > > Here is the script I am trying to use:
> > >
> > > -------
> > > import java.security.MessageDigest
> > > import org.apache.logging.log4j.ThreadContext
> > >
> > > def genmd5(String s) {
> > >     MessageDigest.getInstance("MD5").digest(s.bytes).
> > > encodeHex().toString()
> > > }
> > >
> > > ThreadContext.put("md5message", genmd5(message.getFormattedMessage()))
> > > return true
> > > -------
> > >
> > > Another question: is there a was to put the script in the JSON
> > > configuration file directly?
> > > The doc uses XML format with CDATA, but I'm not sure how that would
> > > translate in JSON.
> > >
> > > Thanks!
> > > Ben
> > >
> > > On Thu, Nov 10, 2016 at 3:49 PM, Remko Popma <remko.popma@gmail.com>
> > > wrote:
> > >
> > >> Can you show your script?
> > >>
> > >> Looking at the code, the documentation is wrong and the Logger object
> is
> > >> bound to variable "logger" instead.
> > >>
> > >> Which variables are available depends on which filter method is
> called.
> > >> This depends on where the filter is configured. The LogEvent is not
> > >> available for global filters. "logger" should be available for global
> > >> filters.
> > >>
> > >> https://github.com/apache/logging-log4j2/blob/master/log4j-
> > >> core/src/main/java/org/apache/logging/log4j/core/filter/
> > ScriptFilter.java
> > >>
> > >>
> > >>
> > >>
> > >> Sent from my iPhone
> > >>
> > >> > On 11 Nov 2016, at 7:15, Benjamin Jaton <benjamin.jaton@gmail.com>
> > >> wrote:
> > >> >
> > >> > Sure, here it is:
> > >> >
> > >> > {
> > >> >  "configuration" : {
> > >> >    "status" : "warn",
> > >> >    "scripts": {
> > >> >      "ScriptFile": { "name":"test.filter", "path":
> > >> > "/usr/local/apps/test.groovy" }
> > >> >    },
> > >> >    "ScriptFilter": {
> > >> >      "onMatch": "ACCEPT",
> > >> >      "onMisMatch": "DENY",
> > >> >      "ScriptRef": { "ref": "test.filter" }
> > >> >    },
> > >> >    "appenders" : {
> > >> >      "appender" : [ {
> > >> >        "type" : "Console",
> > >> >        "name" : "STDOUT",
> > >> >        "PatternLayout" : {
> > >> >          "pattern" : "%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1.}:%L -
> > >> > [%X{md5message}] %m%n"
> > >> >        }
> > >> >      } ]
> > >> >    },
> > >> >    "loggers" : {
> > >> >      "root" : {
> > >> >        "level" : "INFO",
> > >> >        "additivity" : "false",
> > >> >        "includeLocation" : "true",
> > >> >        "AppenderRef" : {
> > >> >          "ref" : "STDOUT"
> > >> >        }
> > >> >      }
> > >> >    }
> > >> >  }
> > >> > }
> > >> >
> > >> >> On Thu, Nov 10, 2016 at 9:01 AM, Remko Popma <
> remko.popma@gmail.com>
> > >> wrote:
> > >> >>
> > >> >> Can you show your configuration?
> > >> >>
> > >> >> Sent from my iPhone
> > >> >>
> > >> >>>> On 11 Nov 2016, at 1:56, Benjamin Jaton <
> benjamin.jaton@gmail.com>
> > >> >>> wrote:
> > >> >>>
> > >> >>> I tried all the variables in the doc, no luck:
> > >> >>>
> > >> >>> Caused by: groovy.lang.MissingPropertyException: No such
> property:
> > >> >>> loggerName for class: Script2
> > >> >>>   at
> > >> >>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(
> > >> >> ScriptBytecodeAdapter.java:53)
> > >> >>>   at
> > >> >>> org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.
> > getProperty(
> > >> >> PogoGetPropertySite.java:52)
> > >> >>>   at
> > >> >>> org.codehaus.groovy.runtime.callsite.AbstractCallSite.
> > >> >> callGroovyObjectGetProperty(AbstractCallSite.java:307)
> > >> >>>   at Script2.run(Script2.groovy:8)
> > >> >>>   at
> > >> >>> org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(
> > >> >> GroovyScriptEngineImpl.java:343)
> > >> >>>   ... 12 more
> > >> >>>
> > >> >>>> On Wed, Nov 9, 2016 at 10:54 PM, Remko Popma <
> > remko.popma@gmail.com>
> > >> >> wrote:
> > >> >>>>
> > >> >>>> According to this https://logging.apache.org/
> > >> >>>> log4j/2.x/manual/filters.html#Script you should be able
to use
> the
> > >> >>>> variable named loggerName in global script filters.
> > >> >>>>
> > >> >>>> Sent from my iPhone
> > >> >>>>
> > >> >>>>>> On 10 Nov 2016, at 4:31, Benjamin Jaton <
> > benjamin.jaton@gmail.com>
> > >> >>>>> wrote:
> > >> >>>>>
> > >> >>>>> Hmm, I'm not sure, I tried to do this in groovy:
> > >> >>>>>
> > >> >>>>> println(args[0])
> > >> >>>>>
> > >> >>>>> But I get:
> > >> >>>>> Caused by: groovy.lang.MissingPropertyException: No
such
> > property:
> > >> >> args
> > >> >>>> for
> > >> >>>>> class: Script2
> > >> >>>>>  at
> > >> >>>>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(
> > >> >>>> ScriptBytecodeAdapter.java:53)
> > >> >>>>>  at
> > >> >>>>> org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.get
> > >> Property(
> > >> >>>> PogoGetPropertySite.java:52)
> > >> >>>>>  at
> > >> >>>>> org.codehaus.groovy.runtime.callsite.AbstractCallSite.
> > >> >>>> callGroovyObjectGetProperty(AbstractCallSite.java:307)
> > >> >>>>>  at Script2.run(Script2.groovy:8)
> > >> >>>>>  at
> > >> >>>>> org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(
> > >> >>>> GroovyScriptEngineImpl.java:343)
> > >> >>>>>  ... 12 more
> > >> >>>>>
> > >> >>>>> Then I tried:
> > >> >>>>>
> > >> >>>>> println(parameters)
> > >> >>>>>
> > >> >>>>> But it returns 'null'
> > >> >>>>>
> > >> >>>>>> On Tue, Nov 8, 2016 at 5:18 PM, Remko Popma <
> > remko.popma@gmail.com
> > >> >
> > >> >>>> wrote:
> > >> >>>>>>
> > >> >>>>>> All filter methods have a Logger object as the
first parameter.
> > Can
> > >> >> you
> > >> >>>>>> use that?
> > >> >>>>>>
> > >> >>>>>> Sent from my iPhone
> > >> >>>>>>
> > >> >>>>>>> On 9 Nov 2016, at 9:45, Benjamin Jaton <
> > benjamin.jaton@gmail.com>
> > >> >>>> wrote:
> > >> >>>>>>>
> > >> >>>>>>> As far as I know, there is no way for a global
filter to know
> > >> which
> > >> >>>>>> logger
> > >> >>>>>>> we're working it for.
> > >> >>>>>>> Which in my case is not the best since I would
compute a hash
> > for
> > >> all
> > >> >>>> the
> > >> >>>>>>> messages of all my loggers, instead of just
the one I need
> them
> > >> for.
> > >> >>>>>>>
> > >> >>>>>>> On Tue, Nov 8, 2016 at 4:30 PM, Benjamin Jaton
<
> > >> >>>> benjamin.jaton@gmail.com
> > >> >>>>>>>
> > >> >>>>>>> wrote:
> > >> >>>>>>>
> > >> >>>>>>>> Ah, I can't use 'logEvent' but I can use
'message', perfect,
> > >> thank
> > >> >>>> you!
> > >> >>>>>>>>
> > >> >>>>>>>>> On Tue, Nov 8, 2016 at 4:21 PM, Remko
Popma <
> > >> remko.popma@gmail.com
> > >> >>>
> > >> >>>>>> wrote:
> > >> >>>>>>>>>
> > >> >>>>>>>>> Yes Filters can be applied at different
stages of the
> logging
> > >> >>>> pipeline.
> > >> >>>>>>>>> The Filter interface has separate
methods for these stages.
> > >> >> Different
> > >> >>>>>>>>> parameters are passed to these methods.
> > >> >>>>>>>>>
> > >> >>>>>>>>> Sent from my iPhone
> > >> >>>>>>>>>
> > >> >>>>>>>>>>> On 9 Nov 2016, at 8:57, Benjamin
Jaton <
> > >> benjamin.jaton@gmail.com>
> > >> >>>>>>>>>> wrote:
> > >> >>>>>>>>>>
> > >> >>>>>>>>>> Ah, interesting. I seem to be
able to make modification
> using
> > >> this
> > >> >>>>>>>>> global
> > >> >>>>>>>>>> filter.
> > >> >>>>>>>>>>
> > >> >>>>>>>>>> However I don't seem to have access
to the log event that
> > way?
> > >> >>>>>>>>>>
> > >> >>>>>>>>>> Caused by: groovy.lang.MissingPropertyException:
No such
> > >> >> property:
> > >> >>>>>>>>> logEvent
> > >> >>>>>>>>>> for class: Script2
> > >> >>>>>>>>>> at
> > >> >>>>>>>>>> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(
> Scr
> > >> >>>>>>>>> iptBytecodeAdapter.java:53)
> > >> >>>>>>>>>> at
> > >> >>>>>>>>>> org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.
> get
> > >> >>>>>>>>> Property(PogoGetPropertySite.java:52)
> > >> >>>>>>>>>> at
> > >> >>>>>>>>>> org.codehaus.groovy.runtime.callsite.AbstractCallSite.
> callGr
> > >> >>>>>>>>> oovyObjectGetProperty(AbstractCallSite.java:307)
> > >> >>>>>>>>>> at Script2.run(Script2.groovy:8)
> > >> >>>>>>>>>> at
> > >> >>>>>>>>>> org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(
> Groov
> > >> >>>>>>>>> yScriptEngineImpl.java:343)
> > >> >>>>>>>>>> ... 12 more
> > >> >>>>>>>>>>
> > >> >>>>>>>>>>> On Tue, Nov 8, 2016 at 3:47
PM, Remko Popma <
> > >> >> remko.popma@gmail.com
> > >> >>>>>
> > >> >>>>>>>>> wrote:
> > >> >>>>>>>>>>>
> > >> >>>>>>>>>>> If you make it a global filter
it will get applied before
> > the
> > >> >>>>>> LogEvent
> > >> >>>>>>>>> is
> > >> >>>>>>>>>>> constructed. Please try that.
> > >> >>>>>>>>>>>
> > >> >>>>>>>>>>> Remko
> > >> >>>>>>>>>>>
> > >> >>>>>>>>>>> Sent from my iPhone
> > >> >>>>>>>>>>>
> > >> >>>>>>>>>>>> On 9 Nov 2016, at 7:02,
Benjamin Jaton <
> > >> >> benjamin.jaton@gmail.com>
> > >> >>>>>>>>> wrote:
> > >> >>>>>>>>>>>>
> > >> >>>>>>>>>>>> I am applying it on the
root logger.
> > >> >>>>>>>>>>>>
> > >> >>>>>>>>>>>>> On Tue, Nov 8, 2016
at 12:47 PM, Matt Sicker <
> > >> boards@gmail.com
> > >> >>>
> > >> >>>>>>>>> wrote:
> > >> >>>>>>>>>>>>>
> > >> >>>>>>>>>>>>> Where are you applying
the filter? On the appender or
> > >> somewhere
> > >> >>>>>> else?
> > >> >>>>>>>>>>>>>
> > >> >>>>>>>>>>>>> On 8 November 2016
at 13:13, Benjamin Jaton <
> > >> >>>>>>>>> benjamin.jaton@gmail.com>
> > >> >>>>>>>>>>>>> wrote:
> > >> >>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>> hello,
> > >> >>>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>> I was playing
with log4j2 filters to generate a hash in
> > >> groovy
> > >> >>>>>> that
> > >> >>>>>>>>> I
> > >> >>>>>>>>>>>>> would
> > >> >>>>>>>>>>>>>> put in the threadcontext
map, and then use in my log
> > >> pattern:
> > >> >>>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>> %d{yyyy-MM-dd
HH:mm:ss} %-5p %c{1.}:%L - %m
> [%X{myhash}]
> > >> >>>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>> But the hash is
printed on the NEXT log message.
> > >> >>>>>>>>>>>>>> My understanding
is that at this point the log message
> is
> > >> >>>> already
> > >> >>>>>>>>>>>>> formatted
> > >> >>>>>>>>>>>>>> and final.
> > >> >>>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>> Is there another
way I can use a custom script where
> the
> > >> log
> > >> >>>> event
> > >> >>>>>>>>> can
> > >> >>>>>>>>>>>>>> actually be modified?
> > >> >>>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>> Thanks,
> > >> >>>>>>>>>>>>>> Ben
> > >> >>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>
> > >> >>>>>>>>>>>>>
> > >> >>>>>>>>>>>>> --
> > >> >>>>>>>>>>>>> Matt Sicker <boards@gmail.com>
> > >> >>>>>>>>>>>
> > >> >>>>>>>>>>> ------------------------------
> > ------------------------------
> > >> >>>>>> ---------
> > >> >>>>>>>>>>> To unsubscribe, e-mail: log4j-user-unsubscribe@
> > >> >> logging.apache.org
> > >> >>>>>>>>>>> For additional commands, e-mail:
log4j-user-help@logging.
> > >> >>>> apache.org
> > >> >>>>>>>>>
> > >> >>>>>>>>> ------------------------------
> ------------------------------
> > >> >>>> ---------
> > >> >>>>>>>>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging
> > >> .apache.org
> > >> >>>>>>>>> For additional commands, e-mail: log4j-user-help@logging.
> > >> >> apache.org
> > >> >>>>>>
> > >> >>>>>> ------------------------------------------------------------
> > >> ---------
> > >> >>>>>> To unsubscribe, e-mail: log4j-user-unsubscribe@
> > logging.apache.org
> > >> >>>>>> For additional commands, e-mail: log4j-user-help@logging.apache
> > >> .org
> > >> >>
> > >> >> ------------------------------------------------------------
> > ---------
> > >> >> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > >> >> For additional commands, e-mail: log4j-user-help@logging.
> apache.org
> > >> >>
> > >> >>
> > >>
> > >
> > >
> >
>
>
>
> --
> Matt Sicker <boards@gmail.com>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message