Replies: 3 comments 1 reply
-
Great question! 👍
Once you add LogManager.shutdown(); to your code, your program should exit correctly. As for the log not arriving in ES, I can't help without your Log4j config file. Could you try this example with or just public class StopApplication {
public static void main(String... args) {
LogManager.getLogger("elasticsearch").error("Hello world!");
LogManager.shutdown();
}
} examples are a bit outdated - expecting ES 7.x, I'll fix it soon - but with little effort, you should be able to get them working with latest ES 8.x One more thing worth noting is |
Beta Was this translation helpful? Give feedback.
-
Thank you ! I went a bit further. package testlog.main;
import org.apache.logging.log4j.LogManager;
public class SimplestestGoWithSystemExit {
public static void main(final String[] args) {
LogManager.getLogger("myLogger").error("Hello world with System.exit(0)!");
System.exit(0);
}
} or package testlog.main;
import org.apache.logging.log4j.LogManager;
public class SimplestestGoWithLogManagerShutdown {
public static void main(final String[] args) {
LogManager.getLogger("myLogger").error("Hello world with LogManager.shutdown()!");
LogManager.shutdown();
}
} System.exit(0) and LogManager.shutdown() both give the same error, and nothing is pushed to my ElasticsSearch server:
If I remove System.exit(0) or LogManager.shutdown(), the program never stops but does push the message: package testlog.main;
import org.apache.logging.log4j.LogManager;
public class SimplestestGoWithNothing {
public static void main(final String[] args) {
LogManager.getLogger("myLogger").error("Hello world with nothing!");
}
}
Here is the relevant part of my log4j2.xml: <Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="[%c] %m\n" />
</Console>
<Elasticsearch name="elasticsearchAsyncBatch-hc">
<IndexName indexName="myIndex" />
<JacksonJsonLayout>
<PooledItemSourceFactory poolName="xxx" itemSizeInBytes="1024" initialPoolSize="3000" />
</JacksonJsonLayout>
<AsyncBatchDelivery batchSize="1000" deliveryInterval="1000">
<IndexTemplate name="myIndex" path="classpath:myIndex.json" />
<HCHttp serverUris="https://myserver">
<Security>
<BasicCredentials username="myUserName" password="myPassword" />
</Security>
<PooledItemSourceFactory poolName="batchPool" itemSizeInBytes="1024000" initialPoolSize="3" />
</HCHttp>
</AsyncBatchDelivery>
</Elasticsearch>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console" />
<AppenderRef ref="elasticsearchAsyncBatch-hc" />
</Root>
</Loggers> Hope this will help you helping me! |
Beta Was this translation helpful? Give feedback.
-
Well, I switched to ahc, out of curiosity, and it works… differently Programs with LogManager.shutdown() or System.exit(0) do stop, and the message is pushed to my ElasticSearch server. Yay! But the program with “nothing” enters a connection problem report loop (sorry for looong log):
|
Beta Was this translation helpful? Give feedback.
-
Hello
I write the simplestest logging program
`
import org.apache.logging.log4j.LogManager;
public class Main {
}
`
Using the -hc logger (same thing with the -jest one), message is correctly logged, but the program won’t stop.
If I insert a System.exit(0), the program does stop, but nothing is logged…
Any clue as how I could fix that?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions