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 Clément Guillaume <cg...@hotpads.com> on 2020/10/07 23:46:15 UTC

logging Exception toString with slf4j

Hi,

I'm using slf4j (1.7.25) and log4j (2.13.3).
My config is simple, just a console appender.
I'm trying to log the toString of an Exception with a parameterized message.
It works fine when there is more than one parameter, but when it's a single
one, the whole exception stacktrace is printed.

org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("loggerName");
> logger.warn("{} {} ", "an error occured", new Exception("my message")); //
> work as expected: print only the toString
> logger.warn("{} ", new Exception("my message")); // print the curly
> brackets and the whole stacktrace


an error occured java.lang.Exception: my message
> {}
> java.lang.Exception: my message
> at com.example.Main.main(Main.java:206) [test-classes/:?]
>

I wanted to confirm this is a bug and wanted to know if it's more likely
caused by slf4j or log4j?

Thank you.

Re: logging Exception toString with slf4j

Posted by Ralph Goers <ra...@dslextreme.com>.
Yes, ultimately that ends up doing

protected void logMessage(final String fqcn, final Level level, final Marker marker, final String message,
        final Throwable t) {
    logMessageSafely(fqcn, level, marker, messageFactory.newMessage(message), t);
}
In AbstractLogger.  As you can see the throwable is not passed as a parameter when the Message is created. 

Ralph

> On Oct 8, 2020, at 9:06 PM, Bobby Bissett <ro...@enterprisedb.com> wrote:
> 
> On Thu, Oct 8, 2020 at 10:37 PM Clément Guillaume <cg...@hotpads.com>
> wrote:
> 
>> 
>> I guess I can do the following (not very elegant)
>>  logger.warn("{} ", (Object)new Exception("my message"));
>> 
> 
> Or just log: new Exception("my message").toString();
> 
> Cheers,
> Bobby


Re: logging Exception toString with slf4j

Posted by Ralph Goers <ra...@dslextreme.com>.
One other option is to use the Log4j API and do

logger.atWarn().log(“{}”, throwable);

Since the LogBuilder adds exceptions using a separate method the log method knows it is a parameter to the format.

FWIW, there is no disadvantage in switching to the Log4j API. It can be bridged to other logging implementations just like SLF4J can. Plus Log4j is actively maintained whereas SLF4J hasn’t had any activity in over a year.

Ralph

> On Oct 8, 2020, at 9:06 PM, Bobby Bissett <ro...@enterprisedb.com> wrote:
> 
> On Thu, Oct 8, 2020 at 10:37 PM Clément Guillaume <cg...@hotpads.com>
> wrote:
> 
>> 
>> I guess I can do the following (not very elegant)
>>  logger.warn("{} ", (Object)new Exception("my message"));
>> 
> 
> Or just log: new Exception("my message").toString();
> 
> Cheers,
> Bobby



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


Re: logging Exception toString with slf4j

Posted by Bobby Bissett <ro...@enterprisedb.com>.
On Thu, Oct 8, 2020 at 10:37 PM Clément Guillaume <cg...@hotpads.com>
wrote:

>
> I guess I can do the following (not very elegant)
>   logger.warn("{} ", (Object)new Exception("my message"));
>

Or just log: new Exception("my message").toString();

Cheers,
Bobby

Re: logging Exception toString with slf4j

Posted by Clément Guillaume <cg...@hotpads.com>.
good call, while the other is calling
public void warn(String format, Object arg1, Object arg2)

I guess I can do the following (not very elegant)
  logger.warn("{} ", (Object)new Exception("my message"));


On Thu, Oct 8, 2020 at 1:31 AM Anders <in...@gmail.com> wrote:

> Hi,
>
> I think logger.warn("{} ", new Exception("my message")); will call
> warn(java.lang.String,java.lang.Throwable)
> <
> http://www.slf4j.org/apidocs/org/slf4j/Logger.html#warn(java.lang.String,java.lang.Throwable
> >
> ).
>
>
> On Thu, Oct 8, 2020 at 7:46 AM Clément Guillaume <cg...@hotpads.com>
> wrote:
>
> > Hi,
> >
> > I'm using slf4j (1.7.25) and log4j (2.13.3).
> > My config is simple, just a console appender.
> > I'm trying to log the toString of an Exception with a parameterized
> > message.
> > It works fine when there is more than one parameter, but when it's a
> single
> > one, the whole exception stacktrace is printed.
> >
> > org.slf4j.Logger logger =
> org.slf4j.LoggerFactory.getLogger("loggerName");
> > > logger.warn("{} {} ", "an error occured", new Exception("my message"));
> > //
> > > work as expected: print only the toString
> > > logger.warn("{} ", new Exception("my message")); // print the curly
> > > brackets and the whole stacktrace
> >
> >
> > an error occured java.lang.Exception: my message
> > > {}
> > > java.lang.Exception: my message
> > > at com.example.Main.main(Main.java:206) [test-classes/:?]
> > >
> >
> > I wanted to confirm this is a bug and wanted to know if it's more likely
> > caused by slf4j or log4j?
> >
> > Thank you.
> >
>

Re: logging Exception toString with slf4j

Posted by Anders <in...@gmail.com>.
Hi,

I think logger.warn("{} ", new Exception("my message")); will call
warn(java.lang.String,java.lang.Throwable)
<http://www.slf4j.org/apidocs/org/slf4j/Logger.html#warn(java.lang.String,java.lang.Throwable>
).


On Thu, Oct 8, 2020 at 7:46 AM Clément Guillaume <cg...@hotpads.com>
wrote:

> Hi,
>
> I'm using slf4j (1.7.25) and log4j (2.13.3).
> My config is simple, just a console appender.
> I'm trying to log the toString of an Exception with a parameterized
> message.
> It works fine when there is more than one parameter, but when it's a single
> one, the whole exception stacktrace is printed.
>
> org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("loggerName");
> > logger.warn("{} {} ", "an error occured", new Exception("my message"));
> //
> > work as expected: print only the toString
> > logger.warn("{} ", new Exception("my message")); // print the curly
> > brackets and the whole stacktrace
>
>
> an error occured java.lang.Exception: my message
> > {}
> > java.lang.Exception: my message
> > at com.example.Main.main(Main.java:206) [test-classes/:?]
> >
>
> I wanted to confirm this is a bug and wanted to know if it's more likely
> caused by slf4j or log4j?
>
> Thank you.
>