使用glog(github.com/golang/glog)如何拆分日志文件?

huangapple go评论130阅读模式
英文:

How to split log file in using glog(github.com/golang/glog)

问题

因为我的服务器可能运行很长时间,日志文件会变得非常大。有没有办法根据大小或时间来切割日志文件?

英文:

Because my server may run for a long time, the log file will be too large.Is there any way to cut logs according to size or time?

答案1

得分: 0

由于您担心日志文件过大,可以尝试条件日志记录或偶发日志记录。您可以使用以下宏来执行条件日志记录:

LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";

只有当变量num_cookies超过10时,才会记录"Got lots of cookies"消息。如果一行代码被执行多次,只在特定间隔记录一条消息可能会很有用。这种日志记录对于信息性消息最有用。

LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";

上述代码在第1次、第11次、第21次等执行时输出日志消息。请注意,特殊的google::COUNTER值用于标识正在发生的重复次数。

您可以结合条件和偶发日志记录使用以下宏:

LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
                                       << "th big cookie";

与其每隔n次输出一条消息,您还可以将输出限制为前n次出现:

LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";

上述代码在前20次执行时输出日志消息。同样,google::COUNTER标识符指示正在发生的重复次数。

您可以在这里查看更多信息。

英文:

Since you worry about a large log file, try conditional logging or occassional logging
You can use the following macros to perform conditional logging:

LOG_IF(INFO, num_cookies &gt; 10) &lt;&lt; &quot;Got lots of cookies&quot;;

The "Got lots of cookies" message is logged only when the variable num_cookies exceeds 10. If a line of code is executed many times, it may be useful to only log a message at certain intervals. This kind of logging is most useful for informational messages.

LOG_EVERY_N(INFO, 10) &lt;&lt; &quot;Got the &quot; &lt;&lt; google::COUNTER &lt;&lt; &quot;th cookie&quot;;

The above line outputs a log messages on the 1st, 11th, 21st, ... times it is executed. Note that the special google::COUNTER value is used to identify which repetition is happening.

You can combine conditional and occasional logging with the following macro.

LOG_IF_EVERY_N(INFO, (size &gt; 1024), 10) &lt;&lt; &quot;Got the &quot; &lt;&lt; google::COUNTER
                                           &lt;&lt; &quot;th big cookie&quot;;

Instead of outputting a message every nth time, you can also limit the output to the first n occurrences:

LOG_FIRST_N(INFO, 20) &lt;&lt; &quot;Got the &quot; &lt;&lt; google::COUNTER &lt;&lt; &quot;th cookie&quot;;

Outputs log messages for the first 20 times it is executed. Again, the google::COUNTER identifier indicates which repetition is happening.

You can check here for more info

答案2

得分: 0

现在我找到了一种分割日志的方法。
使用第三方库。(例如:https://github.com/natefinch/lumberjack

英文:

Now I found a way to split logs.
Using third party libraries.(ex:https://github.com/natefinch/lumberjack)

huangapple
  • 本文由 发表于 2021年6月7日 18:22:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/67869883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定