动态创建和销毁日志附加程序 [英] dynamically creating & destroying logging appenders

查看:45
本文介绍了动态创建和销毁日志附加程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧版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.

我的问题是:

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

谢谢!

推荐答案

我将从

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.

编写附加程序非常简单-这是一个Javaworld指南.

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

在上面,您的程序在一分钟之内是否有可能两次启动?您是否要添加一个进程ID或类似的ID来保持唯一性?

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 ?

这篇关于动态创建和销毁日志附加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆