[Sifting Appender ] Programmatically

Hello, i have created a new configuration with some logger but without appender. I would like to create appender programmatically, to add them to their logger. And finally use MDC context to set which appender will log the messages... For moment, i have create FileAppender, Pattern & Encoder, Filter, Discriminator. I would like to link the FileAppender to SifitingAppender. How to do that ? there is no setparent or set*appender methods.... Any idea ? Thanks.

*How do you create a siftingappender with a fileappender manually ?* *Here my code :* LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("<%d(yyyy-MM-dd HH:mm:ss=> | %.-1level | %msg %n"); encoder.start(); FileAppender<ILoggingEvent> fAppender = new FileAppender<ILoggingEvent>(); fAppender.setContext(loggerContext); fAppender.setName(application + "_" + user); fAppender.setFile(logUser); fAppender.setAppend(true); fAppender.setEncoder(encoder); fAppender.start(); MDCBasedDiscriminator discriminator = new MDCBasedDiscriminator(); discriminator.setKey("userid"); discriminator.setDefaultValue("unknown"); discriminator.start(); LevelFilter lFilter = new LevelFilter(); lFilter.setLevel(Level.INFO); lFilter.start(); // HOW TO ATTACH fAppender on sAppender ???? SiftingAppender sAppender = new SiftingAppender(); sAppender.setContext(loggerContext); sAppender.setName(application + "_" + user + "_SIFTING"); sAppender.addFilter(lFilter); sAppender.setDiscriminator(discriminator); sAppender.start(); Logger logger = loggerContext.getLogger("test.mypackage"); logger.setAdditive(false); logger.setLevel(Level.ALL); logger.addAppender(sAppender); Thank you….

Plz, need help on this subject.... Is it possible to attach the fileappender to the siftingappender exactly as in XML configuration ? Thank you. 2013/2/14 M. MychaL <mychal.13@gmail.com>
*How do you create a siftingappender with a fileappender manually ?*
*Here my code :*
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("<%d(yyyy-MM-dd HH:mm:ss=> | %.-1level | %msg %n");
encoder.start();
FileAppender<ILoggingEvent> fAppender = new FileAppender<ILoggingEvent>();
fAppender.setContext(loggerContext);
fAppender.setName(application + "_" + user);
fAppender.setFile(logUser);
fAppender.setAppend(true);
fAppender.setEncoder(encoder);
fAppender.start();
MDCBasedDiscriminator discriminator = new MDCBasedDiscriminator();
discriminator.setKey("userid");
discriminator.setDefaultValue("unknown");
discriminator.start();
LevelFilter lFilter = new LevelFilter();
lFilter.setLevel(Level.INFO);
lFilter.start();
// HOW TO ATTACH fAppender on sAppender ????
SiftingAppender sAppender = new SiftingAppender();
sAppender.setContext(loggerContext);
sAppender.setName(application + "_" + user + "_SIFTING");
sAppender.addFilter(lFilter);
sAppender.setDiscriminator(discriminator);
sAppender.start();
Logger logger = loggerContext.getLogger("test.mypackage");
logger.setAdditive(false);
logger.setLevel(Level.ALL);
logger.addAppender(sAppender);
Thank you….

M. MychaL <mychal.13@...> writes:
Plz, need help on this subject....Is it possible to attach the fileappender to the siftingappender exactly as in XML configuration ?Thank you.
Without having done this, I presume that you need to set up an AppenderFactory (via setAppenderFactory), since that is what will dynamically create the appenders. It might be useful if you said why you were trying to do this though. If you're just using MDC discrimination, why not do it in the config file? Stuart

Because we have to create a new log dynamically and with MDC discriminator. I made some search on AppenderFactory but dunno how to use it.... We have all, except the link between SiftingAppender and FileAppender :( How is framework working to read configuration ? Is it not possible to have a method to set the parent appender ? Thank you. Envoyé de mon iPhone Le 28 mars 2013 à 14:18, Stuart Rossiter <stuart.p.rossiter@gmail.com> a écrit :
M. MychaL <mychal.13@...> writes:
Plz, need help on this subject....Is it possible to attach the fileappender to the siftingappender exactly as in XML configuration ?Thank you.
Without having done this, I presume that you need to set up an AppenderFactory (via setAppenderFactory), since that is what will dynamically create the appenders.
It might be useful if you said why you were trying to do this though. If you're just using MDC discrimination, why not do it in the config file?
Stuart
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

On 28 March 2013 16:17, MychaL <mychal.13@gmail.com> wrote:
Because we have to create a new log dynamically and with MDC discriminator.
That's where I was confused. That is what the standard sifting appender config file setup is for: dynamically creating logs based on MDC keys. So, unless you were using some other discrimination (or something very fancy in terms of when files were created or something), why would you programmatically create a sifting appender? (Even in the former case, you would still configure the sifting appender in the config file, just provide your own discrimination class.)
I made some search on AppenderFactory but dunno how to use it....
We have all, except the link between SiftingAppender and FileAppender :(
How is framework working to read configuration ? Is it not possible to have a method to set the parent appender ?
The factory design pattern is pretty common in such situations (create some context-specific class dynamically), and I assume that the framework definitely uses it internally. You seem to be doubting that the framework 'really' works like this. (?) Unfortunately, I don't have time to look at coding a simple factory template. Maybe someone else can help?
Thank you.
Le 28 mars 2013 à 14:18, Stuart Rossiter <stuart.p.rossiter@gmail.com> a écrit :
M. MychaL <mychal.13@...> writes:
Plz, need help on this subject....Is it possible to attach the
fileappender to
the siftingappender exactly as in XML configuration ?Thank you.
Without having done this, I presume that you need to set up an AppenderFactory (via setAppenderFactory), since that is what will dynamically create the appenders.
It might be useful if you said why you were trying to do this though. If you're just using MDC discrimination, why not do it in the config file?
Stuart
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
-- ________________________________ Stuart Rossiter stuart.p.rossiter@gmail.com Research Fellow: EPSRC Care Life Cycle Project http://www.southampton.ac.uk/clc
participants (3)
-
M. MychaL
-
MychaL
-
Stuart Rossiter