
Hi, I want to append automatically an additional log value for every log message which is created. Reason is that I want to add a OSGi symbolic bundle name to every message. I've seen a blog entry which solved it with a marker, but I think it's error prone because you have to add the marker yourself for every log call. Any idea how to do this? Is it possible to set a listener for logged messages which can modify the log message? Cheers, Ingo

Ingo Siebert schrieb:
Hi,
I want to append automatically an additional log value for every log message which is created.
Reason is that I want to add a OSGi symbolic bundle name to every message. I've seen a blog entry which solved it with a marker, think that was my blog ;-) but I think it's error prone because you have to add the marker yourself for every log call.
yes, you have to add it, but you also have to add the logger, the message, perhaps parameter, throwable ;-) in my projects I always follow this pattern: - each bundle has one bundle-Marker with symbolic name of the bundle - each class of a bundle has a static import of this bundle Marker - each class has its own logger then I always use logger.debug(bundleMarker, message) the good thing is that now the bundle name is in the marker and you could easy filter logs
Any idea how to do this? Is it possible to set a listener for logged messages which can modify the log message?
Riena does it the other way: they log using Equinox ExtendedLogService, which also includes a logger - and because its an OSGI LogService you can access the context of the bundle logging the event Riena has Listener to get those LogServices and then logs the bundle name and the logmessage using log4j for me using a Marker works great... and I'm also using Lilith and Joern will add supporting Markers to filter, copy, etc ekke

ekkehard schrieb:
Ingo Siebert schrieb:
Hi,
I want to append automatically an additional log value for every log message which is created.
Reason is that I want to add a OSGi symbolic bundle name to every message. I've seen a blog entry which solved it with a marker,
think that was my blog ;-)
Yes, it was. :-) Thank you for your answer and your blog.
but I think it's error prone because you have to add the marker yourself for every log call.
yes, you have to add it, but you also have to add the logger, the message, perhaps parameter, throwable ;-)
in my projects I always follow this pattern: - each bundle has one bundle-Marker with symbolic name of the bundle - each class of a bundle has a static import of this bundle Marker - each class has its own logger then I always use logger.debug(bundleMarker, message)
the good thing is that now the bundle name is in the marker and you could easy filter logs
I like the filtering too. But it's an additional task for every logging message and that's why it isn't perfect. Every developer has to know it if he starts to work for the project. Additionally, we are all humans and make errors. That's why I asked for an other solution.
Any idea how to do this? Is it possible to set a listener for logged messages which can modify the log message?
Riena does it the other way: they log using Equinox ExtendedLogService, which also includes a logger - and because its an OSGI LogService you can access the context of the bundle logging the event Riena has Listener to get those LogServices and then logs the bundle name and the logmessage using log4j
for me using a Marker works great... and I'm also using Lilith and Joern will add supporting Markers to filter, copy, etc

Hi, I'm not sure if this is the preferred way or best practice... but what I did was extend the RollingFileAppender and override the getFile() method. and this getFile() method retrieves / generates the log file name. please let me know if you have better solutions. thanks, Jenny ============================================= From: Jenny jenny_uy81@yahoo.com Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" logback-user@qos.ch Hello, How can i set the log filename set during the init() of my StartupServlet. I tried using MDC but it's not working for me. What happens is the file is not created. Instead a file with name "%exp{logFileName}" \ is created on my web server's bin directory. In my StartupServlet, I have this init method that calls MDC.put(). public void init() throws ServletException { MDC.put("logFileName", "c:\\testlogs\\test.log"); Logger log = LoggerFactory.getLogger(StartupServlet.class); log.info("This should be logged"); } In my logback.xml, I have a MDC.get(). <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <expression name="logFileName">MDC.get("logFileName")</expression> <file>%exp{logFileName}</file> ... I'm new with logback, thanks in advance for your help! I'm trying to follow Ceki's \ example in his blog http://ceki.blogspot.com/2006/10/repated-configuration-with-joran. \ html thanks, jenny

Hello Jenny, Why are you setting the passing value of "logFileName" MDC? Just set it as a variable, see http://logback.qos.ch/manual/joran.html and search for "Variable substitution". HTH, Jenny wrote:
Hi, I'm not sure if this is the preferred way or best practice... but what I did was extend the RollingFileAppender and override the getFile() method.
and this getFile() method retrieves / generates the log file name.
please let me know if you have better solutions.
thanks, Jenny
============================================= From: Jenny jenny_uy81@yahoo.com Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" logback-user@qos.ch
Hello,
How can i set the log filename set during the init() of my StartupServlet.
I tried using MDC but it's not working for me.
What happens is the file is not created. Instead a file with name "%exp{logFileName}" \ is created on my web server's bin directory.
In my StartupServlet, I have this init method that calls MDC.put().
public void init() throws ServletException { MDC.put("logFileName", "c:\\testlogs\\test.log"); Logger log = LoggerFactory.getLogger(StartupServlet.class); log.info("This should be logged"); }
In my logback.xml, I have a MDC.get().
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <expression name="logFileName">MDC.get("logFileName")</expression> <file>%exp{logFileName}</file> ...
I'm new with logback, thanks in advance for your help! I'm trying to follow Ceki's \ example in his blog http://ceki.blogspot.com/2006/10/repated-configuration-with-joran. \ html
thanks, jenny
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch

Hi Ceki, Variable substition wouldn't accomplish what I want to achieve... or would it? :) My goal is to set the log filename in a dynamic way - retrieve it from an LDAP configuration. I don't want it hardcoded in the logback.xml. My understanding is that variable substition only works if the variable comes from an external properties file or set in system property. thanks, Jenny --- On Thu, 11/13/08, Ceki Gulcu <listid@qos.ch> wrote:
From: Ceki Gulcu <listid@qos.ch> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Thursday, November 13, 2008, 4:11 PM Hello Jenny,
Why are you setting the passing value of "logFileName" MDC? Just set it as a variable, see http://logback.qos.ch/manual/joran.html and search for "Variable substitution".
HTH,
Jenny wrote:
Hi, I'm not sure if this is the preferred way or best practice... but what I did was extend the RollingFileAppender and override the getFile() method.
and this getFile() method retrieves / generates the log file name.
please let me know if you have better solutions.
thanks, Jenny
============================================= From: Jenny jenny_uy81@yahoo.com Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" logback-user@qos.ch
Hello,
How can i set the log filename set during the init() of my StartupServlet.
I tried using MDC but it's not working for me.
What happens is the file is not created. Instead a file with name "%exp{logFileName}" \ is created on my web server's bin directory.
In my StartupServlet, I have this init method that calls MDC.put().
public void init() throws ServletException { MDC.put("logFileName", "c:\\testlogs\\test.log"); Logger log = LoggerFactory.getLogger(StartupServlet.class); log.info("This should be logged"); }
In my logback.xml, I have a MDC.get().
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <expression name="logFileName">MDC.get("logFileName")</expression> <file>%exp{logFileName}</file> ...
I'm new with logback, thanks in advance for your help! I'm trying to follow Ceki's \ example in his blog http://ceki.blogspot.com/2006/10/repated-configuration-with-joran. \ html
thanks, jenny
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user

Hello Jenny, You can dynamically add properties for variable substitution to a logger context. Here is how: LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator jc = new JoranConfigurator(); jc.setContext(loggerContext); loggerContext.reset(); // throw away default configuration // the next line is the whole point of the exercise loggerContext.putProperty("logFileName", valueObtainedFromLDAP); try { jc.doConfigure("path/to/a/configfile.xml"); } catch (JoranException je) { je.printStackTrace(); } StatusPrinter.printIfErrorsOccured(loggerContext); And in configfile.xml mentioned above, you would have <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>%{logFileName}</file> ... </appender> HTH, Jenny wrote:
Hi Ceki,
Variable substition wouldn't accomplish what I want to achieve... or would it? :)
My goal is to set the log filename in a dynamic way - retrieve it from an LDAP configuration. I don't want it hardcoded in the logback.xml.
My understanding is that variable substition only works if the variable comes from an external properties file or set in system property.
thanks, Jenny
--- On Thu, 11/13/08, Ceki Gulcu <listid@qos.ch> wrote:
From: Ceki Gulcu <listid@qos.ch> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Thursday, November 13, 2008, 4:11 PM Hello Jenny,
Why are you setting the passing value of "logFileName" MDC? Just set it as a variable, see http://logback.qos.ch/manual/joran.html and search for "Variable substitution".
HTH,
Jenny wrote:
Hi, I'm not sure if this is the preferred way or best practice... but what I did was extend the RollingFileAppender and override the getFile() method.
and this getFile() method retrieves / generates the log file name.
please let me know if you have better solutions.
thanks, Jenny
============================================= From: Jenny jenny_uy81@yahoo.com Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" logback-user@qos.ch
Hello,
How can i set the log filename set during the init() of my StartupServlet. I tried using MDC but it's not working for me.
What happens is the file is not created. Instead a file with name "%exp{logFileName}" \ is created on my web server's bin directory.
In my StartupServlet, I have this init method that calls MDC.put(). public void init() throws ServletException { MDC.put("logFileName", "c:\\testlogs\\test.log"); Logger log = LoggerFactory.getLogger(StartupServlet.class); log.info("This should be logged"); }
In my logback.xml, I have a MDC.get().
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <append>true</append> <expression name="logFileName">MDC.get("logFileName")</expression> <file>%exp{logFileName}</file> ...
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch

Hi Ceki, I tried your suggestion almost exactly but it didn't work for me. I put that code in my StartupServlet.init(). The only part I changed is loggerContext.reset() to loggerContext.shutdownAndReset(). I see this in the system out. 14:46:51,863 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log file name: %{logFileName} Any idea why it didn't work for me? Jenny --- On Fri, 11/14/08, Ceki Gulcu <listid@qos.ch> wrote:
From: Ceki Gulcu <listid@qos.ch> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Friday, November 14, 2008, 4:59 PM Hello Jenny,
You can dynamically add properties for variable substitution to a logger context. Here is how:
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator jc = new JoranConfigurator(); jc.setContext(loggerContext); loggerContext.reset(); // throw away default configuration
// the next line is the whole point of the exercise loggerContext.putProperty("logFileName", valueObtainedFromLDAP); try { jc.doConfigure("path/to/a/configfile.xml"); } catch (JoranException je) { je.printStackTrace(); } StatusPrinter.printIfErrorsOccured(loggerContext);
And in configfile.xml mentioned above, you would have
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>%{logFileName}</file> ... </appender>
HTH,
Jenny wrote:
Hi Ceki,
Variable substition wouldn't accomplish what I want to achieve... or would it? :)
My goal is to set the log filename in a dynamic way - retrieve it from an LDAP configuration. I don't want it hardcoded in the logback.xml.
My understanding is that variable substition only works if the variable comes from an external properties file or set in system property.
thanks, Jenny
--- On Thu, 11/13/08, Ceki Gulcu <listid@qos.ch> wrote:
From: Ceki Gulcu <listid@qos.ch> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Thursday, November 13, 2008, 4:11 PM Hello Jenny,
Why are you setting the passing value of "logFileName" MDC? Just set it as a variable, see http://logback.qos.ch/manual/joran.html and search for "Variable substitution".
HTH,
Jenny wrote:
Hi, I'm not sure if this is the preferred way or best practice... but what I did was extend the RollingFileAppender and override the getFile() method.
and this getFile() method retrieves / generates the log file name.
please let me know if you have better solutions.
thanks, Jenny
============================================= From: Jenny jenny_uy81@yahoo.com Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" logback-user@qos.ch
Hello,
How can i set the log filename set during the init() of my StartupServlet. I tried using MDC but it's not working for me.
What happens is the file is not created. Instead a file with name "%exp{logFileName}" \ is created on my web server's bin directory.
In my StartupServlet, I have this init method that calls MDC.put(). public void init() throws ServletException { MDC.put("logFileName",
"c:\\testlogs\\test.log");
Logger log = LoggerFactory.getLogger(StartupServlet.class); log.info("This should be logged"); }
In my logback.xml, I have a MDC.get().
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append> <expression
name="logFileName">MDC.get("logFileName")</expression>
<file>%exp{logFileName}</file> ...
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user

Hi Ceki, Its working now. I just replaced the "%" with "$" in the variable substitution :) <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${logFileName}</file> </appender> thanks, Jenny --- On Fri, 11/21/08, Jenny <jenny_uy81@yahoo.com> wrote:
From: Jenny <jenny_uy81@yahoo.com> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Friday, November 21, 2008, 7:07 AM Hi Ceki,
I tried your suggestion almost exactly but it didn't work for me. I put that code in my StartupServlet.init(). The only part I changed is loggerContext.reset() to loggerContext.shutdownAndReset(). I see this in the system out.
14:46:51,863 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log file name: %{logFileName}
Any idea why it didn't work for me?
Jenny
--- On Fri, 11/14/08, Ceki Gulcu <listid@qos.ch> wrote:
From: Ceki Gulcu <listid@qos.ch> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Friday, November 14, 2008, 4:59 PM Hello Jenny,
You can dynamically add properties for variable substitution to a logger context. Here is how:
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator jc = new JoranConfigurator(); jc.setContext(loggerContext); loggerContext.reset(); // throw away default configuration
// the next line is the whole point of the exercise loggerContext.putProperty("logFileName", valueObtainedFromLDAP); try {
jc.doConfigure("path/to/a/configfile.xml");
} catch (JoranException je) { je.printStackTrace(); } StatusPrinter.printIfErrorsOccured(loggerContext);
And in configfile.xml mentioned above, you would have
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>%{logFileName}</file> ... </appender>
HTH,
Jenny wrote:
Hi Ceki,
Variable substition wouldn't accomplish what I want to achieve... or would it? :)
My goal is to set the log filename in a dynamic way - retrieve it from an LDAP configuration. I don't want it hardcoded in the logback.xml.
My understanding is that variable substition only works if the variable comes from an external properties file or set in system property.
thanks, Jenny
--- On Thu, 11/13/08, Ceki Gulcu <listid@qos.ch> wrote:
From: Ceki Gulcu <listid@qos.ch> Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" <logback-user@qos.ch> Date: Thursday, November 13, 2008, 4:11 PM Hello Jenny,
Why are you setting the passing value of "logFileName" MDC? Just set it as a
variable, see http://logback.qos.ch/manual/joran.html and search for "Variable substitution".
HTH,
Jenny wrote:
Hi, I'm not sure if this is the preferred way or best practice... but what I did was extend the RollingFileAppender and override the getFile() method.
and this getFile() method retrieves / generates the log file name.
please let me know if you have better solutions.
thanks, Jenny
=============================================
From: Jenny jenny_uy81@yahoo.com Subject: Re: [logback-user] Setting log filename during runtime To: "logback users list" logback-user@qos.ch
Hello,
How can i set the log filename set during the init() of my StartupServlet. I tried using MDC but it's not working for me.
What happens is the file is not created. Instead a file with name "%exp{logFileName}" \ is created on my web server's bin directory.
In my StartupServlet, I have this init method that calls MDC.put(). public void init() throws ServletException { MDC.put("logFileName",
"c:\\testlogs\\test.log");
Logger log =
LoggerFactory.getLogger(StartupServlet.class);
log.info("This should be logged"); }
In my logback.xml, I have a MDC.get().
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append> <expression
name="logFileName">MDC.get("logFileName")</expression>
<file>%exp{logFileName}</file>
participants (4)
-
Ceki Gulcu
-
ekkehard
-
Ingo Siebert
-
Jenny