Adding custom appender from different module #2470
Replies: 5 comments 13 replies
-
Please run with -Dlog4j2.debug=true so we can see the whole configuraton being processed. Can you show the class definition of ClickHouseAppender along with the annotations on it? I am assuming that the ClickHoustAppender project is on the classpath when running the application? |
Beta Was this translation helpful? Give feedback.
-
Next to what @rgoers says (and asks for), you need to make sure that you use the |
Beta Was this translation helpful? Give feedback.
-
So I've managed to use my ClickHouse Appender as separate project and implementate it in @Plugin(
name = "ConnectionSettings",
category = Core.CATEGORY_NAME,
elementType = "connectionSettings",
printObject = true)
public class ConnectionSettings {} In my xml configuration I'm using it like this: <ClickHouseAppender name="ClickHouseAppender" ignoreExceptions="false"
bufferSize=""
timeout=""
tableName="">
<ConnectionSettings HOST=""
PORT=""
USERNAME=""
PASSWORD=""
DATABASE=""
SSL=""
SOCKET_TIMEOUT=""
MAX_EXECUTION_TIME=""
/>
<JsonTemplateLayout eventTemplateUri="classpath:LogMsgLayout.json"/>
</ClickHouseAppender> My parameters are passing and it's okay, however, when I'm starting my program I get this message Is there any way to fix this except of adding ConnectionSettings parameter in |
Beta Was this translation helpful? Give feedback.
-
@vy, good evening, could you please help me with the question: is it possible to have 2 loggers using the same appender but with different settings? |
Beta Was this translation helpful? Give feedback.
-
@vy, I got one more question. Could you please help me with more information. For example, I want to create in test package some BufferAppender, so I could append my messages in some buffer and therefore I could test wirtten messages in the future. @Plugin(
name = "BufferAppender",
category = Core.CATEGORY_NAME,
elementType = Appender.ELEMENT_TYPE,
printObject = true)
public class BufferAppender extends AbstractAppender {
private final List<String> logBuffer = new ArrayList<>();
private BufferAppender(
String name, Filter filter, Layout<String> layout, boolean ignoreExceptions) {
super(name, filter, layout, ignoreExceptions, null);
}
@PluginFactory
public static BufferAppender createAppender(
@PluginAttribute("name") String name,
@PluginElement("Filters") Filter filter,
@PluginElement("layout") Layout<String> layout,
@PluginAttribute("ignoreExceptions") boolean ignoreExceptions) {
return new BufferAppender(name, filter, layout, ignoreExceptions);
}
@Override
public void append(LogEvent event) {
String serializedEvent = (String) getLayout().toSerializable(event);
logBuffer.add(serializedEvent);
}
public List<String> getLogBuffer() {
return logBuffer;
}
} However, maybe is there more optimal way to write Logs somewhere(not file), so I could easily test written messages? I am failing to use this custom appender because I have message that This appender class located near my othet test classes. <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="StdOut" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
<BufferAppender name="BufferAppender" ignoreExceptions="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</BufferAppender>
</Appenders>
<Loggers>
<Logger name="TestLogger" level="INFO" additivity="false">
<AppenderRef ref="StdOut"/>
<AppenderRef ref="BufferAppender"/>
</Logger>
<Root level="ERROR">
<AppenderRef ref="StdOut"/>
<AppenderRef ref="BufferAppender"/>
</Root>
</Loggers>
</Configuration> |
Beta Was this translation helpful? Give feedback.
-
My main goal is to use custom appender from different module in my project.
So, my project structure is:
Project:
Configuration file:
When I'm trying to run my MainService:
So, what is the way to add my customAppender to the project?
Build system is Gradle.
ClickHouseAppender using
implementation project(':MainService')
because it's has several dependencies from main project. So I can't just use the sameimplementation (...)
feature for my MainProject -> circular dependencies.Beta Was this translation helpful? Give feedback.
All reactions