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 Martin Fernau <m....@cps-net.de> on 2009/05/06 00:17:02 UTC

Adding Filter problem

Hello,

I've a problem adding a Filter at runtime.
My log4j.properties looks like this:

--- cut
log4j.rootLogger=info, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
--- cut

During runtime I want to add a new FileAppender for certain paths like this:

--- cut
FileAppender fileAppender =	new FileAppender(
			new PatternLayout("%d [%t] %-5p %c %x - %m%n"), 
			fileName, true);
fileAppender.setName("file");
Logger logger = Logger.getLogger("some.package");
logger.addAppender(fileAppender);
--- cut

this works very well.
Now I want to chage the output of my logger during runtime to also print DEBUG 
messages.
I tried this code but failed so far:

--- cut
Logger logger = Logger.getLogger("some.package");
FileAppender appender = (FileAppender)logger.getAppender("file");
		
LevelMatchFilter lmf = new LevelMatchFilter();
lmf.setLevelToMatch("DEBUG");
lmf.setAcceptOnMatch(true);
appender.addFilter(lmf);
// Test settings
logger.debug("TEST");
--- cut

But this don't work. The test-Message isn't printed to the FileLogger. Only 
info, error and fatal messages are printed. How can I change the Logging 
meachnics during runtime that debuging messages are printed?

Regards,
Martin

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


Re: Adding Filter problem

Posted by Martin Fernau <m....@cps-net.de>.
Am Donnerstag, 7. Mai 2009 schrieb Curt Arnold:
> On May 5, 2009, at 5:17 PM, Martin Fernau wrote:
> > Hello,
> >
> > I've a problem adding a Filter at runtime.
> > My log4j.properties looks like this:
> >
> > --- cut
> > log4j.rootLogger=info, stdout
> >
> > log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> > log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> > --- cut
> >
> > During runtime I want to add a new FileAppender for certain paths
> > like this:
> >
> > --- cut
> > FileAppender fileAppender =	new FileAppender(
> > 			new PatternLayout("%d [%t] %-5p %c %x - %m%n"),
> > 			fileName, true);
> > fileAppender.setName("file");
> > Logger logger = Logger.getLogger("some.package");
> > logger.addAppender(fileAppender);
> > --- cut
> >
> > this works very well.
> > Now I want to chage the output of my logger during runtime to also
> > print DEBUG
> > messages.
> > I tried this code but failed so far:
> >
> > --- cut
> > Logger logger = Logger.getLogger("some.package");
> > FileAppender appender = (FileAppender)logger.getAppender("file");
> >
> > LevelMatchFilter lmf = new LevelMatchFilter();
> > lmf.setLevelToMatch("DEBUG");
> > lmf.setAcceptOnMatch(true);
> > appender.addFilter(lmf);
> > // Test settings
> > logger.debug("TEST");
> > --- cut
> >
> > But this don't work. The test-Message isn't printed to the
> > FileLogger. Only
> > info, error and fatal messages are printed. How can I change the
> > Logging
> > meachnics during runtime that debuging messages are printed?
> >
> > Regards,
> > Martin
>
> A filter would only come into play once the event got to the
> appender.  Your configuration has specified that all logging requests
> lower than info are discarded which occurs prior to the event being
> packaged into a LoggingEvent and dispatched to the appenders.
>
> The more straight-forward mechanism would be to modify the threshold
> on the root logger programmatically.  No need to use a Filter for this
> case.

Thanks,
I just played around a bit with these and I think I've the solution for me.
If I'm correct (and my first tests went well with this) I only need to do this 
during runtime:

--- cut
Logger l = Logger.getLogger("some.package");
l.setLevel(Level.DEBUG);
--- cut

Regards

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


Re: Adding Filter problem

Posted by Curt Arnold <ca...@apache.org>.
On May 5, 2009, at 5:17 PM, Martin Fernau wrote:

> Hello,
>
> I've a problem adding a Filter at runtime.
> My log4j.properties looks like this:
>
> --- cut
> log4j.rootLogger=info, stdout
>
> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> --- cut
>
> During runtime I want to add a new FileAppender for certain paths  
> like this:
>
> --- cut
> FileAppender fileAppender =	new FileAppender(
> 			new PatternLayout("%d [%t] %-5p %c %x - %m%n"),
> 			fileName, true);
> fileAppender.setName("file");
> Logger logger = Logger.getLogger("some.package");
> logger.addAppender(fileAppender);
> --- cut
>
> this works very well.
> Now I want to chage the output of my logger during runtime to also  
> print DEBUG
> messages.
> I tried this code but failed so far:
>
> --- cut
> Logger logger = Logger.getLogger("some.package");
> FileAppender appender = (FileAppender)logger.getAppender("file");
> 		
> LevelMatchFilter lmf = new LevelMatchFilter();
> lmf.setLevelToMatch("DEBUG");
> lmf.setAcceptOnMatch(true);
> appender.addFilter(lmf);
> // Test settings
> logger.debug("TEST");
> --- cut
>
> But this don't work. The test-Message isn't printed to the  
> FileLogger. Only
> info, error and fatal messages are printed. How can I change the  
> Logging
> meachnics during runtime that debuging messages are printed?
>
> Regards,
> Martin
>

A filter would only come into play once the event got to the  
appender.  Your configuration has specified that all logging requests  
lower than info are discarded which occurs prior to the event being  
packaged into a LoggingEvent and dispatched to the appenders.

The more straight-forward mechanism would be to modify the threshold  
on the root logger programmatically.  No need to use a Filter for this  
case.

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