Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback: use Java main class name as log filename

Tags:

logback

I have several executable classes and I would like to automatically set the log filename to the name of the Java class being executed.

I know I could do this programmatically, probably using Reflection and MDC, but isn't there an easier way to achieve this by configuring logback.xml?

This seems a rather basic need to me, so I am surprised I could not find documentation about it.

like image 836
Alphaaa Avatar asked Jul 14 '16 16:07

Alphaaa


1 Answers

If you're on a JVM that supports it you can use this property

<file>${sun.java.command}.log</file>

Otherwise you need to set a similar system property in all your main methods that logback can use.

like image 148
OrangeDog Avatar answered Nov 24 '22 00:11

OrangeDog