英文:
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 > 10) << "Got lots of cookies";
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) << "Got the " << google::COUNTER << "th cookie";
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 > 1024), 10) << "Got the " << google::COUNTER
<< "th big cookie";
Instead of outputting a message every nth time, you can also limit the output to the first n occurrences:
LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论