logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicholas Duane <nic...@msn.com>
Subject Re: looking for source
Date Fri, 04 Nov 2016 22:50:46 GMT
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");
            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">
        <Console name="Console" >
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} \
- %msg%n"/>
    <Root level="info">
            <AppenderRef ref="Console"/>

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?



From: Remko Popma <remko.popma@gmail.com>
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

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

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>
Access from Behind a Firewall. Refer to the documentation of the SCM used for more information
about access behind a firewall.


Sent from my iPhone

> On 4 Nov 2016, at 23:29, Nicholas Duane <nickdu@msn.com> 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

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message