Search code examples
logback

Logback: use Java main class name as log filename


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.


Solution

  • 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.