在日志记录文件中获取PID

发布于 2025-01-26 05:27:39 字数 219 浏览 1 评论 0 原文

在Spring-Boot之外使用LogBack时,如何将ProcessID放入日志文件名中?

rollingfileappender 我想定义:

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

如何获得 $ {pid} 设置,还是有标准方法获得此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-02-02 05:27:39
  1. 了解如何从JVM内部获取自己的PID-
      processhandle.current()。pid()
     

  2. 创建一个自定义 软件包com.example; 导入ch.qos.logback.core.propertydefinerbase; 类PidPropertyDefiner扩展了PropertyDefinerBase { public String getPropertyValue(){ 返回long.tostring(processhandle.current()。pid()); } }
  3. 使用该属性定义器将属性注入到日志中:
     &lt; define name =“ self_pid” class =“ com.example.pidpropertydefiner” /&gt;
     

...或使用 logback.groovy 而不是 logback.xml ,您可以直接访问JVM的每一点。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文