Logger level from Layout class

Hi, Is there any way to get from MyLayout the logger level? I don't want to do this: package com.render;import org.slf4j.LoggerFactory;import ch.qos.logback.classic.Logger;import ch.qos.logback.classic.spi.ILoggingEvent;import ch.qos.logback.core.LayoutBase; import com.render.domain.Employee; public class MyLayout extends LayoutBase<ILoggingEvent>{ String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString();...... This works well but i don't like to instantiate an object just to get a logger level. This is my xml config file: <configuration><logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root></configuration> Thanks,Gastón.

Gaston, Normally if you need to do something based on the logging level, you'd do. if (log.isDebugEnabled()) { … } Is there some special reason why you'd need the exact level? David On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:
Hi, Is there any way to get from MyLayout the logger level? I don't want to do this:
package com.render; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.LayoutBase;
import com.render.domain.Employee;
public class MyLayout extends LayoutBase<ILoggingEvent>{
String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString(); ... ...
This works well but i don't like to instantiate an object just to get a logger level.
This is my xml config file:
<configuration> <logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender>
<root level="off"> <appender-ref ref="CONSOLE" /> </root> </configuration>
Thanks, Gastón.
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

I don't understand the question anyway. If there is no need for a logger (that is, if it shall not become instantiated), what does it mean to know the log-level. Which routine shall read the config, if the logger isn't instantiated? Otherwise do a "String level=log.getLevel.toString()" Ulrich Am 30.07.2013 um 21:18 schrieb David Roussel <nabble@diroussel.xsmail.com>:
Gaston,
Normally if you need to do something based on the logging level, you'd do.
if (log.isDebugEnabled()) { … }
Is there some special reason why you'd need the exact level?
David
On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:
Hi, Is there any way to get from MyLayout the logger level? I don't want to do this:
package com.render; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.LayoutBase;
import com.render.domain.Employee;
public class MyLayout extends LayoutBase<ILoggingEvent>{
String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString(); ... ...
This works well but i don't like to instantiate an object just to get a logger level.
This is my xml config file:
<configuration> <logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender>
<root level="off"> <appender-ref ref="CONSOLE" /> </root> </configuration>
Thanks, Gastón.
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Of course logger must be instantiated, we are good with that. I'm doing the instantiation of my logger in other class. I can´t do String level=log.getLevel.toString(), because from MyLayout class i don't have access to my logger. Unless of course, i do this: log = (Logger)LoggerFactory.getLogger("employee"); then i can follow your advice: level=log.getLevel.toString(); I could be wrong but, isn´t it a dirty way to get just a level ?I was thinking maybe on get the logger level from MyLayout event (param from doLayout), but i can´t find any logger level, just event.getLevel(), and that is a differente one. I hope to be clear on what i want. Thanks guysFrom: Forums@gombers.de Date: Tue, 30 Jul 2013 21:37:58 +0200 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class I don't understand the question anyway. If there is no need for a logger (that is, if it shall not become instantiated), what does it mean to know the log-level. Which routine shall read the config, if the logger isn't instantiated?Otherwise do a "String level=log.getLevel.toString()" Ulrich Am 30.07.2013 um 21:18 schrieb David Roussel <nabble@diroussel.xsmail.com>: Gaston, Normally if you need to do something based on the logging level, you'd do. if (log.isDebugEnabled()) { …} Is there some special reason why you'd need the exact level? David On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:Hi, Is there any way to get from MyLayout the logger level? I don't want to do this: package com.render;import org.slf4j.LoggerFactory;import ch.qos.logback.classic.Logger;import ch.qos.logback.classic.spi.ILoggingEvent;import ch.qos.logback.core.LayoutBase; import com.render.domain.Employee; public class MyLayout extends LayoutBase<ILoggingEvent>{ String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString();...... This works well but i don't like to instantiate an object just to get a logger level. This is my xml config file: <configuration><logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root></configuration> Thanks,Gastón. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

If you are inside the layout, then get the level from the event. That is the one and only place to get the level from. David On 30 Jul 2013, at 20:58, gaston sponer <gaston.sponer@hotmail.com> wrote:
Of course logger must be instantiated, we are good with that. I'm doing the instantiation of my logger in other class.
I can´t do String level=log.getLevel.toString(), because from MyLayout class i don't have access to my logger. Unless of course, i do this:
log = (Logger)LoggerFactory.getLogger("employee");
then i can follow your advice:
level=log.getLevel.toString();
I could be wrong but, isn´t it a dirty way to get just a level ? I was thinking maybe on get the logger level from MyLayout event (param from doLayout), but i can´t find any logger level, just event.getLevel(), and that is a differente one.
I hope to be clear on what i want.
Thanks guys From: Forums@gombers.de Date: Tue, 30 Jul 2013 21:37:58 +0200 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class
I don't understand the question anyway. If there is no need for a logger (that is, if it shall not become instantiated), what does it mean to know the log-level. Which routine shall read the config, if the logger isn't instantiated? Otherwise do a "String level=log.getLevel.toString()"
Ulrich
Am 30.07.2013 um 21:18 schrieb David Roussel <nabble@diroussel.xsmail.com>:
Gaston,
Normally if you need to do something based on the logging level, you'd do.
if (log.isDebugEnabled()) { … }
Is there some special reason why you'd need the exact level?
David
On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:
Hi, Is there any way to get from MyLayout the logger level? I don't want to do this:
package com.render; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.LayoutBase;
import com.render.domain.Employee;
public class MyLayout extends LayoutBase<ILoggingEvent>{
String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString(); ... ...
This works well but i don't like to instantiate an object just to get a logger level.
This is my xml config file:
<configuration> <logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender>
<root level="off"> <appender-ref ref="CONSOLE" /> </root> </configuration>
Thanks, Gastón.
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Thanks David. From: nabble@diroussel.xsmail.com Date: Tue, 30 Jul 2013 21:06:04 +0100 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class If you are inside the layout, then get the level from the event. That is the one and only place to get the level from. David On 30 Jul 2013, at 20:58, gaston sponer <gaston.sponer@hotmail.com> wrote: Of course logger must be instantiated, we are good with that. I'm doing the instantiation of my logger in other class. I can´t do String level=log.getLevel.toString(), because from MyLayout class i don't have access to my logger. Unless of course, i do this: log = (Logger)LoggerFactory.getLogger("employee"); then i can follow your advice: level=log.getLevel.toString(); I could be wrong but, isn´t it a dirty way to get just a level ?I was thinking maybe on get the logger level from MyLayout event (param from doLayout), but i can´t find any logger level, just event.getLevel(), and that is a differente one. I hope to be clear on what i want. Thanks guysFrom: Forums@gombers.de Date: Tue, 30 Jul 2013 21:37:58 +0200 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class I don't understand the question anyway. If there is no need for a logger (that is, if it shall not become instantiated), what does it mean to know the log-level. Which routine shall read the config, if the logger isn't instantiated?Otherwise do a "String level=log.getLevel.toString()" Ulrich Am 30.07.2013 um 21:18 schrieb David Roussel <nabble@diroussel.xsmail.com>: Gaston, Normally if you need to do something based on the logging level, you'd do. if (log.isDebugEnabled()) { …} Is there some special reason why you'd need the exact level? David On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:Hi, Is there any way to get from MyLayout the logger level? I don't want to do this: package com.render;import org.slf4j.LoggerFactory;import ch.qos.logback.classic.Logger;import ch.qos.logback.classic.spi.ILoggingEvent;import ch.qos.logback.core.LayoutBase; import com.render.domain.Employee; public class MyLayout extends LayoutBase<ILoggingEvent>{ String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString();...... This works well but i don't like to instantiate an object just to get a logger level. This is my xml config file: <configuration><logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root></configuration> Thanks,Gastón. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Hi guys, I have some doubts about implementing methods from DefaultThrowableRenderer. Can you please give me some basic example to figure out how render() or printFirstLine() method it works? I can't find any example. What advantages I have using render(StringBuilder sbuf, ILoggingEvent event) ? Can i use this render method on a Layout different from HTMLayout ? Thanks

I need to show logger level on my log. Ex: Log:event level: ERROR logger level: debug [main] Company_Module - Personal: name=John, age=36, id=3124362] I understand isDebugEnabled, but this is not what i'm looking. My logger levele (set on xml config file) could be any level (info, trace, error, debug...) is not about debug level, is about print the level from logger that i can set on xml. If i had this in my xml :<logger name="employee" level="info"/> my log would print : event level: ERROR logger level: info[main] Company_Module - Personal: name=John, age=36, id=3124362] My concern is about what other way exist to get logger level different what i'm using now: String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString(); public String doLayout(ILoggingEvent event) { StringBuffer sbuf = new StringBuffer(128); sbuf.append("event level: "); sbuf.append(event.getLevel()); sbuf.append(" logger level: "); sbuf.append(getLevel()); sbuf.append(" ["); sbuf.append(event.getThreadName()); sbuf.append("] "); sbuf.append(event.getLoggerName()); sbuf.append(" - "); logMsg.append(employee.getPerson()); return sbuf.toString();} public String getLevel() { return level; } public void setLevel(String level) { this.level = level;} Thanks, GastonFrom: nabble@diroussel.xsmail.com Date: Tue, 30 Jul 2013 20:18:37 +0100 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class Gaston, Normally if you need to do something based on the logging level, you'd do. if (log.isDebugEnabled()) { …} Is there some special reason why you'd need the exact level? David On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:Hi, Is there any way to get from MyLayout the logger level? I don't want to do this: package com.render;import org.slf4j.LoggerFactory;import ch.qos.logback.classic.Logger;import ch.qos.logback.classic.spi.ILoggingEvent;import ch.qos.logback.core.LayoutBase; import com.render.domain.Employee; public class MyLayout extends LayoutBase<ILoggingEvent>{ String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString();...... This works well but i don't like to instantiate an object just to get a logger level. This is my xml config file: <configuration><logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root></configuration> Thanks,Gastón. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

That's the job of the log encoder pattern, you shouldn't be doing that in your code, use the %p placeholder in your pattern string in your configuration file. See http://logback.qos.ch/manual/configuration.htmlfor more information (*Chris*) On Tue, Jul 30, 2013 at 12:40 PM, gaston sponer <gaston.sponer@hotmail.com>wrote:
I need to show logger level on my log. Ex:
Log: event level: ERROR logger level: *debug *[main] Company_Module - Personal: name=John, age=36, id=3124362]
I understand isDebugEnabled, but this is not what i'm looking. My logger levele (set on xml config file) could be any level (info, trace, error, debug...) is not about debug level, is about print the level from logger that i can set on xml.
If i had this in my xml : <logger name="employee" level="info"/>
my log would print :
event level: ERROR logger level: *info*[main] Company_Module - Personal: name=John, age=36, id=3124362]
My concern is about what other way exist to get logger level different what i'm using now:
*String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString();* * * public String doLayout(ILoggingEvent event) { StringBuffer sbuf = new StringBuffer(128); sbuf.append("event level: "); sbuf.append(event.getLevel()); sbuf.append(" logger level: "); sbuf.append(getLevel()); sbuf.append(" ["); sbuf.append(event.getThreadName()); sbuf.append("] "); sbuf.append(event.getLoggerName()); sbuf.append(" - "); logMsg.append(employee.getPerson()); return sbuf.toString(); }
public String getLevel() { return level; }
public void setLevel(String level) { this.level = level; }
Thanks,
Gaston ------------------------------ From: nabble@diroussel.xsmail.com Date: Tue, 30 Jul 2013 20:18:37 +0100 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class
Gaston,
Normally if you need to do something based on the logging level, you'd do.
if (log.isDebugEnabled()) { … }
Is there some special reason why you'd need the exact level?
David
On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote:
Hi, Is there any way to get from MyLayout the logger level? I don't want to do this:
package com.render; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.LayoutBase;
import com.render.domain.Employee;
public class MyLayout extends LayoutBase<ILoggingEvent>{ *String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString();* ... ...
This works well but i don't like to instantiate an object just to get a logger level.
This is my xml config file:
<configuration> <logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root> </configuration>
Thanks, Gastón.
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Oh, thanks Chris.. I will go for encoder pattern way. From: thechrispratt@gmail.com Date: Tue, 30 Jul 2013 12:44:42 -0700 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class That's the job of the log encoder pattern, you shouldn't be doing that in your code, use the %p placeholder in your pattern string in your configuration file. See http://logback.qos.ch/manual/configuration.html for more information (*Chris*) On Tue, Jul 30, 2013 at 12:40 PM, gaston sponer <gaston.sponer@hotmail.com> wrote: I need to show logger level on my log. Ex: Log:event level: ERROR logger level: debug [main] Company_Module - Personal: name=John, age=36, id=3124362] I understand isDebugEnabled, but this is not what i'm looking. My logger levele (set on xml config file) could be any level (info, trace, error, debug...) is not about debug level, is about print the level from logger that i can set on xml. If i had this in my xml :<logger name="employee" level="info"/> my log would print : event level: ERROR logger level: info[main] Company_Module - Personal: name=John, age=36, id=3124362] My concern is about what other way exist to get logger level different what i'm using now: String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString(); public String doLayout(ILoggingEvent event) { StringBuffer sbuf = new StringBuffer(128); sbuf.append("event level: "); sbuf.append(event.getLevel()); sbuf.append(" logger level: "); sbuf.append(getLevel()); sbuf.append(" ["); sbuf.append(event.getThreadName()); sbuf.append("] "); sbuf.append(event.getLoggerName()); sbuf.append(" - "); logMsg.append(employee.getPerson()); return sbuf.toString();} public String getLevel() { return level; } public void setLevel(String level) { this.level = level;} Thanks, GastonFrom: nabble@diroussel.xsmail.com Date: Tue, 30 Jul 2013 20:18:37 +0100 To: logback-user@qos.ch Subject: Re: [logback-user] Logger level from Layout class Gaston, Normally if you need to do something based on the logging level, you'd do. if (log.isDebugEnabled()) { …} Is there some special reason why you'd need the exact level? David On 30 Jul 2013, at 19:33, gaston sponer <gaston.sponer@hotmail.com> wrote: Hi, Is there any way to get from MyLayout the logger level? I don't want to do this: package com.render;import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger;import ch.qos.logback.classic.spi.ILoggingEvent;import ch.qos.logback.core.LayoutBase; import com.render.domain.Employee; public class MyLayout extends LayoutBase<ILoggingEvent>{ String level= ((Logger)LoggerFactory.getLogger("employee")).getLevel().toString(); ...... This works well but i don't like to instantiate an object just to get a logger level. This is my xml config file: <configuration><logger name="employee" level="debug"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.render.MyLayout" > </layout> </encoder> </appender> <root level="off"> <appender-ref ref="CONSOLE" /> </root></configuration> Thanks,Gastón. _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
participants (4)
-
Chris Pratt
-
David Roussel
-
gaston sponer
-
Ulrich