Nick,

If you want to combine the formatted message string and the stack trace into a 
single string in your layout then yes, that is one way to do that. 

For reference, take a look at the MessagePatternConverter and 
ThrowablePatternConverter (used by Log4j's PatternLayout): 
https://github.com/apache/logging-log4j2/tree/master/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern

Conceptually the Message and the Throwable are separate parts of the LogEvent. 
I think what may have caused some confusion is that initially 
ParameterizedMessage captures message parameters and the Throwable in a single 
vararg array, but please consider that an implementation detail. 

Sent from my iPhone

> On 6 Nov 2016, at 10:33, Nicholas Duane <[email protected]> wrote:
> 
> Thanks.  I have no problem doing that.  Just wondering what needs to be done 
> and how?  I guess I was hoping that the logging framework, eg. log4j, handled 
> combining the message and the exception such that no matter what 
> appender/layout you used you'd get consistent results.  Sounds like that's 
> not the case.  So are you saying I need to combine the message and the 
> exception myself within our appender/layout?
> 
> 
> From the code I included below you can see we're doing:
> 
> 
> String message = logEvent.getMessage().getFormattedMessage();
> 
> 
> to get the event's message.  Are you saying we should be doing something like 
> this:
> 
> 
> StringWriter sw = new StringWriter();
> logEvent.getThrown().printStackTrace(new PrintWriter(sw));
> String exceptionAsString = sw.toString();
> String message = logEvent.getMessage().getFormattedMessage() + "\n" + 
> exceptionAsString;
> 
> 
> Thanks,
> 
> Nick
> 
> ________________________________
> From: Remko Popma <[email protected]>
> Sent: Saturday, November 5, 2016 9:16 PM
> To: Log4J Users List
> Subject: Re: looking for source
> 
> The point is that in a custom layout/appender you should get the throwable 
> from the LogEvent, not from the Message. That is the design that all Layouts 
> and Appenders follow and should follow.
> 
> The Message object passed to the Layout may no longer have the Throwable.
> 
> Sent from my iPhone
> 
>> On 6 Nov 2016, at 10:01, Nicholas Duane <[email protected]> wrote:
>> 
>> Thanks for the info.  Unfortunately I'm not following.  If someone can point 
>> me to the source in question I will certainly look it over.  My sample 
>> outputs the message (%msg) and when the method logger.error(String message, 
>> Throwable t) is called it seems some code is combining the supplied string 
>> message with the exception message and stack trace.  Just wondering what 
>> code is doing that as I'm told someone using our appender/layout is not 
>> seeing the exception.
>> 
>> 
>> Thanks,
>> 
>> Nick
>> 
>> ________________________________
>> From: Remko Popma <[email protected]>
>> Sent: Friday, November 4, 2016 9:58 PM
>> To: Log4J Users List
>> Subject: Re: looking for source
>> 
>> The Throwable is initially captured in the Message (usually 
>> ParameterizedMessage or its garbage-free equivalent), but is later 
>> transferred to the LogEvent.
>> 
>> By the time the LogEvent reaches the Layout, the Message instance may be a 
>> different Object than the one that originally held the Throwable.
>> 
>> The Layout should get the Throwable from the LogEvent, not from the Message.
>> 
>> Remko
>> 
>> Sent from my iPhone
>> 
>>> On 5 Nov 2016, at 7:50,Nicholas Duane <[email protected]> wrote:
>>> 
>>> Thanks for the replies.  Let me ask a more pointed question now as 
>>> rummaging through the source will probably take me hours.  I'm curious 
>>> about the overloads with the throwable, for instance:
>>> 
>>> 
>>> logger.error("some message", <some throwable>);
>>> 
>>> 
>>> What's the expected behavior?  Meaning, what is the logging framework going 
>>> to do with the message and the exception?  I know the message is usually 
>>> output via the message property in the pattern layout.  But what happens 
>>> with the throwable?  Is there some code responsible for constructing a 
>>> message which combines the supplied message with the throwable exception 
>>> message and callstack?
>>> 
>>> 
>>> I wrote the following simple java sample:
>>> 
>>> 
>>> import org.apache.logging.log4j.Logger;
>>> import org.apache.logging.log4j.LogManager;
>>> 
>>> public class Test
>>> {
>>>  private static final Logger logger = LogManager.getLogger(Test.class);
>>> 
>>>  public static void main(String[] args)
>>>  {
>>>       logger.info("entered main");
>>>  try
>>>          {
>>>          throw(new IllegalArgumentException("bad argument"));
>>>          }
>>>  catch(Throwable t)
>>>          {
>>>          logger.error("caught exception", t);
>>>          }
>>>  logger.info("exiting main");
>>>  }
>>> }
>>> 
>>> 
>>> Here is the log4j2.xml file:
>>> 
>>> 
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <Configuration status="WARN">
>>>  <Appenders>
>>>      <Console name="Console" >
>>>          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} 
>>> \
>>> - %msg%n"/>
>>>      </Console>
>>>  </Appenders>
>>>  <Loggers>
>>>  <Root level="info">
>>>          <AppenderRef ref="Console"/>
>>>  </Root>
>>>  </Loggers>
>>> </Configuration>
>>> 
>>> 
>>> Here is the output to the console when I run the sample:
>>> 
>>> 
>>> [nick@thinkpad log4j]$ java Test
>>> 16:37:57.681 [main] INFO  Test - entered main
>>> 16:37:57.683 [main] ERROR Test - caught exception
>>> java.lang.IllegalArgumentException: bad argument
>>>  at Test.main(Test.java:13) [log4j/:?]
>>> 16:37:57.689 [main] INFO  Test - exiting main
>>> 
>>> 
>>> So there does seem to be some code which is combining the message with the 
>>> exception.  Just wondering where that happens.  Now onto my specific 
>>> problem.  We've got someone saying that when they use the error() method 
>>> which takes a string message and an throwable the exception information is 
>>> not making it to our central repository.  We have written our own appender 
>>> and our own layout.  Here is a snippet from our appender:
>>> 
>>> 
>>> String serializedEvent = (String) getLayout().toSerializable(logEvent);
>>> 
>>> Here is a snippet from our layout:
>>> 
>>> 
>>> String message = logEvent.getMessage().getFormattedMessage();
>>> 
>>> 
>>> It seems the message above does not include any exception info.  What are 
>>> we doing wrong?
>>> 
>>> 
>>> Thanks,
>>> 
>>> Nick
>>> 
>>> ________________________________
>>> From: Remko Popma <[email protected]>
>>> Sent: Friday, November 4, 2016 10:42 AM
>>> To: Log4J Users List
>>> Subject: Re: looking for source
>>> 
>>> Sure. Start in AbstractLogger in the API module: 
>>> https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
> [https://avatars1.githubusercontent.com/u/47359?v=3&s=400]<https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java>
> 
> apache/logging-log4j2<https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java>
> github.com
> logging-log4j2 - Mirror of Apache Logging Log4J2
> 
> 
>> [https://avatars1.githubusercontent.com/u/47359?v=3&s=400]<https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java>
>> 
>> apache/logging-log4j2<https://github.com/apache/logging-log4j2/blob/master/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java>
>> github.com
>> logging-log4j2 - Mirror of Apache Logging Log4J2
>> 
>> 
>>> 
>>> and at some point you probably also want to look at the concrete subclass 
>>> Logger in the core module: 
>>> https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java
>> [https://avatars1.githubusercontent.com/u/47359?v=3&s=400]<https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java>
>> 
>> apache/logging-log4j2<https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/main/java/org/apache/logging/log4j/core/Logger.java>
>> github.com
>> logging-log4j2 - Mirror of Apache Logging Log4J2
>> 
>> 
>>> 
>>> Hope this helps to get you started.
>>> 
>>> It may be easier to use an IDE and check out the whole project 
>>> (https://logging.apache.org/log4j/2.x/source-repository.html).
> Log4j – Source Repository - Apache Log4j 
> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> logging.apache.org
> Access from Behind a Firewall. Refer to the documentation of the SCM used for 
> more information about access behind a firewall.
> 
> 
>> Log4j - Source Repository - Apache Log4j 
>> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> Log4j – Source Repository - Apache Log4j 
> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> logging.apache.org
> Access from Behind a Firewall. Refer to the documentation of the SCM used for 
> more information about access behind a firewall.
> 
> 
>> logging.apache.org
>> Access from Behind a Firewall. Refer to the documentation of the SCM used 
>> for more information about access behind a firewall.
>> 
>> 
>>> Log4j - Source Repository - Apache Log4j 
>>> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> Log4j – Source Repository - Apache Log4j 
> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> logging.apache.org
> Access from Behind a Firewall. Refer to the documentation of the SCM used for 
> more information about access behind a firewall.
> 
> 
>> Log4j - Source Repository - Apache Log4j 
>> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> Log4j – Source Repository - Apache Log4j 
> 2<https://logging.apache.org/log4j/2.x/source-repository.html>
> logging.apache.org
> Access from Behind a Firewall. Refer to the documentation of the SCM used for 
> more information about access behind a firewall.
> 
> 
>> logging.apache.org
>> Access from Behind a Firewall. Refer to the documentation of the SCM used 
>> for more information about access behind a firewall.
>> 
>> 
>>> logging.apache.org
>>> Access from Behind a Firewall. Refer to the documentation of the SCM used 
>>> for more information about access behind a firewall.
>>> 
>>> 
>>> 
>>> Enjoy!
>>> Remko
>>> 
>>> Sent from my iPhone
>>> 
>>>> On 4 Nov 2016, at 23:29, Nicholas Duane <[email protected]> wrote:
>>>> 
>>>> I'm not that familiar with java and java conventions, package names, etc.  
>>>> I'm trying to find the source for log4j2's implementation of 
>>>> logger.error(String, Throwable);.  Can someone point me to that?  I was 
>>>> looking around here:
>>>> 
>>>> 
>>>> https://git-wip-us.apache.org/repos/asf?p=logging-log4j2.git;a=tree;hb=refs/heads/master
>>>> 
>>>> 
>>>> But couldn't seem to find what I was looking for.
>>>> 
>>>> 
>>>> Thanks,
>>>> 
>>>> Nick
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 

Reply via email to