yvasyliev opened a new issue, #1386:
URL: https://github.com/apache/logging-log4j2/issues/1386
## Description
It is not possible to use custom logger appender without explicitly
specifying appender's package in `log4j2.xml`.
## Configuration
**Version:** `2.20.0`
**Operating system:** `Windows 10`, `Ubuntu 20.04.5 LTS`
**JDK:** `openjdk 17.0.6 2023-01-17 LTS`
## Logs
```
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will
impact performance.
ERROR StatusConsoleListener Appenders contains an invalid element or
attribute "MyAppender"
ERROR StatusConsoleListener Unable to locate appender "myAppender" for
logger config "root"
```
## Reproduction
<table>
<tr><th>Project structure</th></tr>
<tr><td>
```
๐log4j2-demo
โ ๐pom.xml
โโโโ๐src
โ๐main
โ๐java
โ โ๐demo
โ โ๐log4j2
โ โ๐MyAppender.java
โ๐resources
โ๐log4j2.xml
```
</td></tr>
</table>
<table>
<tr><th>
๐`pom.xml`
</th></tr>
<tr><td>
```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo.log4j2</groupId>
<artifactId>log4j2-demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>demo.log4j2.MyAppender</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
</project>
```
</td></tr>
</table>
<table>
<tr><th>
๐`MyAppender.java`
</th></tr>
<tr><td>
```java
package demo.log4j2;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
@Plugin(name = "MyAppender", category = Core.CATEGORY_NAME, elementType =
Appender.ELEMENT_TYPE)
public class MyAppender extends AbstractAppender {
private static final Logger LOGGER =
LoggerFactory.getLogger(MyAppender.class);
protected MyAppender(String name, Filter filter, Layout<? extends
Serializable> layout, boolean ignoreExceptions, Property[] properties) {
super(name, filter, layout, ignoreExceptions, properties);
}
@PluginFactory
public static MyAppender createAppender(@PluginAttribute("name") String
name, @PluginElement("Filter") Filter filter) {
return new MyAppender(name, filter,
PatternLayout.createDefaultLayout(), true, Property.EMPTY_ARRAY);
}
public static void main(String[] args) {
LOGGER.info("Hello world!");
}
@Override
public void append(LogEvent event) {
System.out.println(event.getMessage().getFormattedMessage());
}
}
```
</td></tr>
</table>
<table>
<tr><th>
๐`log4j2.xml`
</th></tr>
<tr><td>
```xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<MyAppender name="myAppender"/>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="myAppender"/>
</Root>
</Loggers>
</Configuration>
```
</td></tr>
</table>
---
1. If I run this code in Intellij Idea, I will get the next output:
```shell
C:\Users\Yevhen\.jdks\corretto-17.0.6\bin\java.exe
"-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition
2020.2.2\lib\idea_rt.jar=50945:C:\Program Files\JetBrains\IntelliJ IDEA
Community Edition 2020.2.2\bin" -Dfile.encoding=UTF-8 -classpath
C:\Users\Yevhen\IdeaProjects\log4j2-demo\target\classes;C:\Users\Yevhen\.m2\repository\org\slf4j\slf4j-api\2.0.7\slf4j-api-2.0.7.jar;C:\Users\Yevhen\.m2\repository\org\apache\logging\log4j\log4j-slf4j2-impl\2.20.0\log4j-slf4j2-impl-2.20.0.jar;C:\Users\Yevhen\.m2\repository\org\apache\logging\log4j\log4j-api\2.20.0\log4j-api-2.20.0.jar;C:\Users\Yevhen\.m2\repository\org\apache\logging\log4j\log4j-core\2.20.0\log4j-core-2.20.0.jar
demo.log4j2.MyAppender
Hello world!
Process finished with exit code 0
```
2. If I build the project, run the jar:
```shell
mvn clean package
```
and run the jar, I will get the next output:
```shell
C:\Users\Yevhen\IdeaProjects\log4j2-demo\target>java -jar
log4j2-demo-1.0.0-jar-with-dependencies.jar
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This
will impact performance.
ERROR StatusConsoleListener Appenders contains an invalid element or
attribute "MyAppender"
ERROR StatusConsoleListener Unable to locate appender "myAppender" for
logger config "root"
```
3. If I open ๐`log4j2.xml` and replace:
```xml
<Configuration status="warn">
```
with:
```xml
<Configuration status="warn" packages="demo.log4j2">
```
re-build the jar and run it, I will get the next output:
```shell
C:\Users\Yevhen\IdeaProjects\log4j2-demo\target>java -jar
log4j2-demo-1.0.0-jar-with-dependencies.jar
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This
will impact performance.
WARN StatusConsoleListener The use of package scanning to locate plugins
is deprecated and will be removed in a future release
WARN StatusConsoleListener The use of package scanning to locate plugins
is deprecated and will be removed in a future release
WARN StatusConsoleListener The use of package scanning to locate plugins
is deprecated and will be removed in a future release
Hello world!
```
---
According to
[this](https://logging.apache.org/log4j/2.x/manual/plugins.html) article
`MyAppender` must be autoconfigured at build time:
> Serialized plugin listing files are generated by an annotation processor
contained in the log4j-core artifact which
> will automatically scan your code for Log4j 2 plugins and output a
metadata file in your processed classes. There is
> nothing extra that needs to be done to enable this; the Java compiler will
automatically pick up the annotation
> processor on the class path unless you explicitly disable it.
Even if I add this plugin to `pom.xml`, nothing will be changed:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>log4j-plugin-processor</id>
<goals>
<goal>compile</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<proc>only</proc>
<annotationProcessors>
<annotationProcessor>org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor
</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]