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 Pa...@b-source.ch on 2005/06/22 18:21:07 UTC

Log4j & filtering

Ciao,

I'm trying to using filtering in my very simple app.

Follow my java class test:
------------------------------------------------------
package test.log4j;

import org.apache.log4j.Logger;

class MyException extends Exception
{
        public MyException(String msg)
        {
                super(msg);
        }
}

public class TestLog4j
{
        public static void main(String[] args)
        {
                Logger logger = Logger.getLogger(TestLog4j.class);

                logger.info("info - main");
                logger.debug("debug - main");
                logger.warn("warn - main");

                logger.error("error - main");
                logger.fatal("fatal - main");

                MyException ex1 = new MyException("ex for error msg");
                MyException ex2 = new MyException("ex for fatal msg");
               
                logger.error(ex1);
                logger.fatal(ex2);
        }
}
------------------------------------------------------

Follow my log4j.properties

------------------------------------------------------
# Setup the root category
log4j.rootCategory=ERROR,errorAppender
 
# Log only messages of level specified below or above in the following
packages
log4j.category.test.log4j=DEBUG,errorAppender,infoAppender,stdout
log4j.additivity.test.log4j=false
 
# The appenders
log4j.appender.errorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.errorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.errorAppender.layout.ConversionPattern=%d %-5p [%l - %t]
%m%n
log4j.appender.errorAppender.File=C:/projects/workspace/test/log4j/error
.log
log4j.appender.errorAppender.MaxBackupIndex=10
log4j.appender.errorAppender.MaxFileSize=1000KB
#log4j.appender.errorAppender.filter=test.log4j.ErrorFilter
log4j.appender.errorAppender.filter.levelToMatch=ERROR
log4j.appender.infoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.infoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.infoAppender.layout.ConversionPattern=%d %-5p [%l - %t]
%m%n
log4j.appender.infoAppender.File=C:/projects/workspace/test/log4j/info.l
og
log4j.appender.infoAppender.MaxBackupIndex=10
log4j.appender.infoAppender.MaxFileSize=1000KB
 
# The log stdout appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%l - %t] %m%n
------------------------------------------------------

I would like to split messages on two files.
For example in my app I have my logger  (linked  to TestLog4j.class) and
I would like to use only this one logger to log errors and
info/debug/warn events.
In addition I would like to have one file (info.log) that contains all
messages and another file (error.log) that contains ONLY error /fatal
messages.
In this way I can monitor only one file that contains only error /fatal
messages and avoid to filtering messages not critical.
The info log file is also available for developers in order to trace
what application is doing.
I could accomplish this need in a simple way istantiating 2 loggers:
 
  Logger logger      = Logger.getLogger("info."  + TestLog4j.class);
  Logger errorLogger = Logger.getLogger("error." + TestLog4j.class);
...

  Logger logger      = Logger.getLogger("info."  + AnotherClass.class);
  Logger errorLogger = Logger.getLogger("error." + AnotherClass.class);
 
and then
 
  logger.info(...) or .debug(...) etc.
  errorLogger.error(...) or errorLogger.fatal(...)
 
but in this way I've to duplicate the categories and, in the source, to
remeber to call the correct logger according to level of the event.
So I'm trying to understand if I can use filtering to accomplish my need
and if this way is the correct solution to my problem.
 
I thank you in advance
Best regards
Patrizio

Re: Log4j & filtering

Posted by James Stauffer <st...@gmail.com>.
Just put a threshold of error on the error appender and it should work
fine.  You don't need to use different loggers in code.

On 6/22/05, Patrizio.Ferlito@b-source.ch <Pa...@b-source.ch> wrote:
> I would like to split messages on two files.
> For example in my app I have my logger  (linked  to TestLog4j.class) and
> I would like to use only this one logger to log errors and
> info/debug/warn events.
> In addition I would like to have one file (info.log) that contains all
> messages and another file (error.log) that contains ONLY error /fatal
> messages.
> In this way I can monitor only one file that contains only error /fatal
> messages and avoid to filtering messages not critical.
> The info log file is also available for developers in order to trace
> what application is doing.


-- 
James Stauffer
Are you good? Take the test at http://www.livingwaters.com/good/

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


Re: Log4j & filtering

Posted by Javier Gonzalez <ja...@gmail.com>.
On 6/22/05, Patrizio.Ferlito@b-source.ch <Pa...@b-source.ch> wrote:
> I would like to use only this one logger to log errors and
> info/debug/warn events.
> In addition I would like to have one file (info.log) that contains all
> messages and another file (error.log) that contains ONLY error /fatal
> messages.

If I understand correctly what you want to do (use only one logger in
the code, and have all messages in one file and only error-fatal
messages in the other), what you need to do is put thresholds on your
appenders.

Create one appender that logs to info.log and set its threshold to
INFO (or DEBUG if you want debugging output too), and another one with
ERROR as its treshold. Something like this:

log4j.appender.errorAppender.Threshold=ERROR
log4j.appender.infoAppender.Threshold=INFO

this will make errorAppender discard any message with level below
ERROR, and infoAppender discard any message with level below INFO.

-- 
Javier Gonzalez Nicolini

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