1

The log4j2 documentation shows the preferred method to produce custom components (such as appenders, filters and lookups). However, it is unclear on how to ensure that these custom components are picked up by the logging framework and useable by a logging configuration.

After placing a custom appender with the correct annotations in the classpath, what else do I need to do to be able to start including it in my log4j2.xml file and have it picked up by the logging framework?

To provide an example of this, if I annotate a custon appender with:

@Plugin(name = "Stub", category = "Core", elementType = "appender", printObject = true)

how do I get it to be picked up and used like this in the log4j2.xml configuration:

    ...
    <Appenders>
      <Stub name="stub">
      ...
      </Stub>
      ...
    </Appenders>
    ...
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
jr.
  • 1,699
  • 14
  • 31

2 Answers2

1

You need to put the package name (or names in a comma-separated list if multiple) in the packages attribute of the Configuration element of your log4j2.xml.

E.g.

<Configuration status="trace" packages="com.mycomp.myproject.appenders">
...

See the Log4j2 Configuration Documentation for more information.

jr.
  • 1,699
  • 14
  • 31
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Thanks for your answer. I haven't had time to try this out, but I will get to it ASAP so I can hopefully accept and upvote your answer. – jr. May 30 '14 at 00:19
1

I can't post a comment yet, so I'm writing this as an answer.

packages attribute in configuration element is no longer supported (since version 2.0-rc2), see this answer: https://stackoverflow.com/a/24931395/3145863.

It should be included in the 2.0.1 release of log4j (https://issues.apache.org/jira/browse/LOG4J2-741)

For now, I recommend you to use Maven to build your project.

Community
  • 1
  • 1
jjurm
  • 501
  • 6
  • 11