Csanád Vereckei
Csanád Vereckei

Reputation: 65

Log4j - Add filter to appender in Groovy

I don't really have any relevant groovy experience so far and now I am trying to log only a few messages into a file from my grails app. I have set up log4j (1.2.17) in Config.groovy and a simple file appender is up and running.

Now what I want to do is filter the messages and only log the ones that match a regex. I think I should use a regexfilter for this, but I am simply unable to add the filter to the mentioned appender.

What I have now

    log4j = {
        appenders {
            console....
            file name: 'file', file: '*.log', layout: pattern(conversionPattern: "..."), filter: 
                regexFilter(pattern: "...", onMatch: "ACCEPT", onMismatch: "DENY")
        }
    }

Upvotes: 1

Views: 144

Answers (1)

Csanád Vereckei
Csanád Vereckei

Reputation: 65

So I've tried what daggett suggested, but with no luck.

After a few (a lot) of trying, I've finally came up with a working solution, so if anyone is interested this works for me:

Logger.rootLogger.allAppenders.find{it instanceof FileAppender}?.addFilter({event -> event.getMessage().matches(***) ? Filter.ACCEPT : Filter.DENY} as Filter)

Just put it in the Config.groovy file.

Upvotes: 1

Related Questions