动态创建& 销毁日志附加程序

发布于 2024-08-01 16:58:41 字数 279 浏览 2 评论 0原文

我有一个旧版 PSVM 应用程序,我想将其日志输出重定向到每次执行的唯一文件。 因此,如果我在 10:00 调用它,则让它将其输出重定向到 {thread-id}-10:00.log; 另一个执行线程可能在 10:01 开始执行,其输出将转到 {thread-id}-10:01.log。 我知道这并不优雅。

我的问题是:

  • 这可能吗?
  • 有人知道如何处理吗?
  • 当不再需要时是否可以释放/销毁附加程序?

谢谢!

I have a legacy PSVM application which I'd like to redirect its logging output to unique files per execution. So, if I invoke it at 10:00, then have it redirect it's output to {thread-id}-10:00.log; and another thread of execution may begin an execution at 10:01, and its output would go to {thread-id}-10:01.log. I understand that this is not elegant.

My questions are:

  • is this possible?
  • does someone have an idea of how to approach?
  • is it possible to release/destroy an appender when it's no longer needed?

Thanks!

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

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

发布评论

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

评论(2

陪我终i 2024-08-08 16:58:41

这是不可能的,至少在 log4j 中不容易做到。 但是,如果您查看 SiftingAppender 随 logback(log4j 的后继者)一起发布,您会发现它旨在根据运行时标准处理附加程序的创建以及不再需要时的删除。

如果您的应用程序需要在每次应用程序启动时创建一个日志文件,您可以简单地根据时间戳命名日志文件。 如果您需要进一步的帮助,请在 logback-user 邮件列表中留言。

It is not possible, at least not easy to do in log4j. However, if you look at SiftingAppender shipping with logback (log4j's successor), it is designed to handle the creation of appenders on runtime criteria as well as their removal when no longer needed.

If you application needs to create just one log file per application launch, you could simply name your log file based on a timestamp. Shout on the logback-user mailing list if you need further assistance.

水染的天色ゝ 2024-08-08 16:58:41

我将从 FileAppender 开始并派生自创建您自己的。 只需修改您的版本即可获取当前线程 ID,并在创建之前将合适的线程 ID/时间戳附加到文件中。 您将维护(比如说)一个(缓冲的)FileWriters 键入线程 ID。

编写附加程序非常简单 - 这里有一个 Javaworld 指南 如何去做。

在上面的内容中,您的程序是否有可能在一分钟内启动两次? 您想要附加进程 ID 或类似内容以保持唯一性吗?

I would start with FileAppender and derive from that to create your own. Simply modify your version to get the current thread id and append a suitable thread-id/timestamp to the file prior to creation. You would maintain (say) a map of (buffered) FileWriters keyed on thread id.

Writing appenders is quite trivial - here's a Javaworld guide on how to do it.

In the above, is it at all likely that your program will start up twice in one minute ? Would you want to append a process id or similar to maintain uniqueness ?

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