0

When using logback outside of Spring-Boot, how can I get the ProcessID into the log file name?

In the RollingFileAppender I'd like to define:

<file>my-log-${PID}.log</file>

How can I get the ${PID} set or is there a standard way of obtaining this?

user2591854
  • 182
  • 3
  • 15

1 Answers1

1
  1. Learn how to get the own PID from inside the JVM - How can a Java program get its own process ID?
    ProcessHandle.current().pid()
    
  2. Create a custom PropertyDefiner:
    package com.example;
    import ch.qos.logback.core.PropertyDefinerBase;
    
    class PidPropertyDefiner extends PropertyDefinerBase {
      public String getPropertyValue() {
        return Long.toString(ProcessHandle.current().pid());
      }
    }
    
  3. Use that property definer to inject a property into the logback:
     <define name="SELF_PID" class="com.example.PidPropertyDefiner" />
    

... or use the logback.groovy instead of logback.xml, there you can just access every bit of JVM directly.

bobah
  • 18,364
  • 2
  • 37
  • 70