使用适用于 Mac 的 Google 工具箱记录到文件时的日志文件翻转
我正在使用适用于 Mac 的 GTMLogger 的 Google 工具箱在我正在开发的应用程序中记录到文件。
我正在尝试决定当文件变得足够大时如何进行日志文件翻转。
理想情况下,当日志文件达到 1 mb 且最多 11 个日志文件时,我希望像 log4net 那样立即滚动,但我没有看到任何内置方法可以执行此操作,我想知道是否尝试添加它麻烦多于其价值。
我能想到的更简单的选择是在应用程序启动时进行此检查,并在日志超过一定大小时滚动日志。当然,这样做的缺点是,如果有人让应用程序运行一两周(并且由于应用程序的一部分是 launchd 守护进程,对于那些很少重新启动的人来说,这绝对是可能的),可能会出现一个非- 在此期间建立的微不足道的大小(取决于启用的日志记录级别)。
我最好的选择是什么?
I'm using Google toolbox for Mac's GTMLogger to do logging to file in the app I'm working on.
I'm trying to decide how to do log file rollover when the file gets large enough.
Ideally I would like something like log4net's immediate rollover when the log file hits 1 mb with max 11 log files at any one time, but I don't see any built-in way to do this and I'm wondering if trying to add it is more trouble than it's worth.
The somewhat simpler option I can think of is just doing this check on app start-up and rolling over the log it it's over a certain size. The downside to this is of course if somebody leaves the app running for a week or two (and since a portion of the app is a launchd daemon this is a definite possibility for those who rarely restart), there could be a log file of non-trivial size built up during this period (depending on what logging level is enabled).
What's going to be my best option here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您担心仅在启动时检查可能不够频繁。因此,在启动时在运行循环中添加一个计时器,以立即触发日志检查,此后每隔一两天触发一次日志检查。如果您的目标是一次最多使用 1 MiB,即使您确实在一段时间内超出了限制,也可能不会那么重要。
您还可以看看 log4net 如何实现此功能:它就像挂钩每个 Append() 来检查是否满足大小/日期约束并在必要时调整记录的文件一样简单。请参阅
RollingFileAppender
源代码了解更多详细信息;从AdjustFileBeforeAppend()
开始,然后查看RollOverSize()
。 这是 Apache:使用源代码!You're worried that checking only at startup might not be often enough. So, throw a timer in your runloop at startup to trigger a log check immediately and once every day or two thereafter. If you're targeting using at most 1 MiB at a time, even if you do go over limit for a while, it likely won't matter all that much.
You could also just look at how log4net implements this feature: it's as simple as hooking each
Append()
to check that the size/date constraints are being met and adjusting the file logged to if necessary. See theRollingFileAppender
source code for further details; start withAdjustFileBeforeAppend()
and then check outRollOverSize()
. It's Apache: Use the source!