You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Mahler, Stefan" <St...@icongmbh.de> on 2017/02/27 20:00:07 UTC

Profile controlled filtering

Hello,

we want replace our legacy proprietary logging framework.

One important feature of our current logging framework is the so called 'profile controlled filtering'.
There exist different filtering profiles like 'production', 'test' or 'development'. The profile to use
is selected by a system property named 'STAGE'.
Each filtering profile has its filter combination:
* production: Log level is ERROR or higher, except, if marker is HEARTBEAT, then the log level should be INFO or higher.
* test: Log level is WARNING or higher, except if marker is one of [HEARTBEAT, DB], then the log level is DEBUG or higher.
* development: Log level is INFO, except if marker is one of [HEARTBEAT, DB, ENTRY].
Additionally there is a default filter combination. Default filter combination is used if STAGE is unknown or no STAGE is defined.

A single filter combination can be defined with CompositeFilter. E.g. for the production profile the follow CompositeFilter can be used:
      <Filters>
        <ThresholdFilter level="INFO" onMatch="NEUTRAL" onMismatch="DENY" />
        <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL" />
        <MarkerFilter marker="HEARTBEAT" onMatch="ACCEPT" onMismatch="DENY" />
      </Filters>


But how can I select the profile (this means here the CompositeFilter) by System-Lookup?
Is there any recommended way for profile controlling?

For all profiles the same appenders are used. The only difference is the filter combination.

Thanks a lot!
Stefan Mahler



--
Stefan Mahler                           http://www.icongmbh.de
Application Development
icon Systemhaus GmbH            Tel. +49 711 806098-167
Hauptstätter Straße 70
70178 Stuttgart                         Fax. +49 711 806098-299


Geschäftsführer: Uwe Seltmann, Andreas Schwegler
HRB Stuttgart 17655
USt-IdNr.: DE 811944121

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Profile controlled filtering

Posted by Matt Sicker <bo...@gmail.com>.
Definitely don't recommend implementing the interfaces directly as they're
not guaranteed to be backwards compatible, but the abstract classes
generally are.

On 27 February 2017 at 14:43, Remko Popma <re...@gmail.com> wrote:

> One option is to create a custom Log4j2 Filter. Your custom Filter would
> only be "active" if it detects it's in the correct environment, otherwise
> it would return NEUTRAL from all `filter(...)` methods.
>
> CompositeFilter is final so you can't subclass it but perhaps you can
> create a wrapper instead.
>
> Here is a link to the manual page that explains how to implement Log4j2
> plugins: http://logging.apache.org/log4j/2.x/manual/plugins.html
> This section is specifically about implementing custom filters:
> http://logging.apache.org/log4j/2.x/manual/extending.html#Filters
>
> You probably want to subclass AbstractFilter rather than implementing the
> Filter interface from scratch:
> https://github.com/apache/logging-log4j2/blob/master/
> log4j-core/src/main/java/org/apache/logging/log4j/core/
> filter/AbstractFilter.java
>
>
> Sent from my iPhone
>
> > On Feb 27, 2017, at 21:00, Mahler, Stefan <St...@icongmbh.de>
> wrote:
> >
> > Hello,
> >
> > we want replace our legacy proprietary logging framework.
> >
> > One important feature of our current logging framework is the so called
> 'profile controlled filtering'.
> > There exist different filtering profiles like 'production', 'test' or
> 'development'. The profile to use
> > is selected by a system property named 'STAGE'.
> > Each filtering profile has its filter combination:
> > * production: Log level is ERROR or higher, except, if marker is
> HEARTBEAT, then the log level should be INFO or higher.
> > * test: Log level is WARNING or higher, except if marker is one of
> [HEARTBEAT, DB], then the log level is DEBUG or higher.
> > * development: Log level is INFO, except if marker is one of [HEARTBEAT,
> DB, ENTRY].
> > Additionally there is a default filter combination. Default filter
> combination is used if STAGE is unknown or no STAGE is defined.
> >
> > A single filter combination can be defined with CompositeFilter. E.g.
> for the production profile the follow CompositeFilter can be used:
> >      <Filters>
> >        <ThresholdFilter level="INFO" onMatch="NEUTRAL" onMismatch="DENY"
> />
> >        <ThresholdFilter level="ERROR" onMatch="ACCEPT"
> onMismatch="NEUTRAL" />
> >        <MarkerFilter marker="HEARTBEAT" onMatch="ACCEPT"
> onMismatch="DENY" />
> >      </Filters>
> >
> >
> > But how can I select the profile (this means here the CompositeFilter)
> by System-Lookup?
> > Is there any recommended way for profile controlling?
> >
> > For all profiles the same appenders are used. The only difference is the
> filter combination.
> >
> > Thanks a lot!
> > Stefan Mahler
> >
> >
> >
> > --
> > Stefan Mahler                           http://www.icongmbh.de
> > Application Development
> > icon Systemhaus GmbH            Tel. +49 711 806098-167
> > Hauptstätter Straße 70
> > 70178 Stuttgart                         Fax. +49 711 806098-299
> >
> >
> > Geschäftsführer: Uwe Seltmann, Andreas Schwegler
> > HRB Stuttgart 17655
> > USt-IdNr.: DE 811944121
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
>



-- 
Matt Sicker <bo...@gmail.com>

Re: Profile controlled filtering

Posted by Remko Popma <re...@gmail.com>.
One option is to create a custom Log4j2 Filter. Your custom Filter would only be "active" if it detects it's in the correct environment, otherwise it would return NEUTRAL from all `filter(...)` methods. 

CompositeFilter is final so you can't subclass it but perhaps you can create a wrapper instead. 

Here is a link to the manual page that explains how to implement Log4j2 plugins: http://logging.apache.org/log4j/2.x/manual/plugins.html
This section is specifically about implementing custom filters: http://logging.apache.org/log4j/2.x/manual/extending.html#Filters

You probably want to subclass AbstractFilter rather than implementing the Filter interface from scratch:
https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/AbstractFilter.java


Sent from my iPhone

> On Feb 27, 2017, at 21:00, Mahler, Stefan <St...@icongmbh.de> wrote:
> 
> Hello,
> 
> we want replace our legacy proprietary logging framework.
> 
> One important feature of our current logging framework is the so called 'profile controlled filtering'.
> There exist different filtering profiles like 'production', 'test' or 'development'. The profile to use
> is selected by a system property named 'STAGE'.
> Each filtering profile has its filter combination:
> * production: Log level is ERROR or higher, except, if marker is HEARTBEAT, then the log level should be INFO or higher.
> * test: Log level is WARNING or higher, except if marker is one of [HEARTBEAT, DB], then the log level is DEBUG or higher.
> * development: Log level is INFO, except if marker is one of [HEARTBEAT, DB, ENTRY].
> Additionally there is a default filter combination. Default filter combination is used if STAGE is unknown or no STAGE is defined.
> 
> A single filter combination can be defined with CompositeFilter. E.g. for the production profile the follow CompositeFilter can be used:
>      <Filters>
>        <ThresholdFilter level="INFO" onMatch="NEUTRAL" onMismatch="DENY" />
>        <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL" />
>        <MarkerFilter marker="HEARTBEAT" onMatch="ACCEPT" onMismatch="DENY" />
>      </Filters>
> 
> 
> But how can I select the profile (this means here the CompositeFilter) by System-Lookup?
> Is there any recommended way for profile controlling?
> 
> For all profiles the same appenders are used. The only difference is the filter combination.
> 
> Thanks a lot!
> Stefan Mahler
> 
> 
> 
> --
> Stefan Mahler                           http://www.icongmbh.de
> Application Development
> icon Systemhaus GmbH            Tel. +49 711 806098-167
> Hauptstätter Straße 70
> 70178 Stuttgart                         Fax. +49 711 806098-299
> 
> 
> Geschäftsführer: Uwe Seltmann, Andreas Schwegler
> HRB Stuttgart 17655
> USt-IdNr.: DE 811944121
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 

RE: Profile controlled filtering

Posted by Laurent Hasson <ld...@CapsicoHealth.com>.
Maybe there is a smarter way to do this in Log4J, but the low-tech approach
I have deployed in the past was to simply have 2 different xml configuration
files (we only had Prod Vs. Dev) and deploy the one that matched the
setting.

But these days, we unify everything and it's basically just a logging level
and structure our logging appropriately and use a lot of "if debuglevel=true
..." logic. That makes things simpler imho.

Laurent Hasson
Co-Founder and CTO
CapsicoHealth Inc.

-----Original Message-----
From: Mahler, Stefan [mailto:Stefan.Mahler@icongmbh.de] 
Sent: Monday, February 27, 2017 15:00
To: log4j-user@logging.apache.org
Subject: Profile controlled filtering

Hello,

we want replace our legacy proprietary logging framework.

One important feature of our current logging framework is the so called
'profile controlled filtering'.
There exist different filtering profiles like 'production', 'test' or
'development'. The profile to use is selected by a system property named
'STAGE'.
Each filtering profile has its filter combination:
* production: Log level is ERROR or higher, except, if marker is HEARTBEAT,
then the log level should be INFO or higher.
* test: Log level is WARNING or higher, except if marker is one of
[HEARTBEAT, DB], then the log level is DEBUG or higher.
* development: Log level is INFO, except if marker is one of [HEARTBEAT, DB,
ENTRY].
Additionally there is a default filter combination. Default filter
combination is used if STAGE is unknown or no STAGE is defined.

A single filter combination can be defined with CompositeFilter. E.g. for
the production profile the follow CompositeFilter can be used:
      <Filters>
        <ThresholdFilter level="INFO" onMatch="NEUTRAL" onMismatch="DENY" />
        <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL"
/>
        <MarkerFilter marker="HEARTBEAT" onMatch="ACCEPT" onMismatch="DENY"
/>
      </Filters>


But how can I select the profile (this means here the CompositeFilter) by
System-Lookup?
Is there any recommended way for profile controlling?

For all profiles the same appenders are used. The only difference is the
filter combination.

Thanks a lot!
Stefan Mahler



--
Stefan Mahler                           http://www.icongmbh.de
Application Development
icon Systemhaus GmbH            Tel. +49 711 806098-167
Hauptstätter Straße 70
70178 Stuttgart                         Fax. +49 711 806098-299


Geschäftsführer: Uwe Seltmann, Andreas Schwegler HRB Stuttgart 17655
USt-IdNr.: DE 811944121

---------------------------------------------------------------------
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