教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go语言之日志,按日期轮询file-rotatelogs

go语言之日志,按日期轮询file-rotatelogs

发布时间:2022-03-26   编辑:jiaochengji.com
教程集为您提供go语言之日志,按日期轮询file-rotatelogs等资源,欢迎您收藏本站,我们将为您提供最新的go语言之日志,按日期轮询file-rotatelogs资源
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"/></svg>

“–————人与人之间还是直接点好——————”

<pre><code>1.go语言本身的日志系统是很强大的,例如: "log" "github.com/sirupsen/logrus" 但,go语言本身并没有日志轮询机制,(就是将日志定期清理,保存,使之不会不停涨大) 2.此处使介绍了一个 file-rotatelogs 包 "github.com/lestrrat-go/file-rotatelogs" ****它可以和标准的日志结合使用**** 具体参数,程序描述的够清晰了! 利弊看程序解析! </code></pre> <pre><code class="lang-go hljs"><span class="token keyword">package</span> main <span class="token keyword">import</span> <span class="token punctuation">(</span> <span class="token string">"log"</span> <span class="token string">"time"</span> rotatelogs <span class="token string">"github.com/lestrrat-go/file-rotatelogs"</span> <span class="token string">"github.com/sirupsen/logrus"</span> <span class="token punctuation">)</span> <span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> content<span class="token punctuation">,</span> err <span class="token operator">:=</span> rotatelogs<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span> <span class="token string">"/var/log/cli.log"</span><span class="token operator"> </span><span class="token string">"-%Y%m%d%H%M"</span><span class="token punctuation">,</span> rotatelogs<span class="token punctuation">.</span><span class="token function">WithLinkName</span><span class="token punctuation">(</span><span class="token string">"/var/log/cli.log"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 生成软链,指向最新日志文件</span> <span class="token comment">//MaxAge and RotationCount cannot be both set 两者不能同时设置</span> rotatelogs<span class="token punctuation">.</span><span class="token function">WithMaxAge</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token operator">*</span>time<span class="token punctuation">.</span>Minute<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//clear 最小分钟为单位</span> <span class="token comment">//rotatelogs.WithRotationCount(5), //number 默认7份 大于7份 或到了清理时间 开始清理</span> rotatelogs<span class="token punctuation">.</span><span class="token function">WithRotationTime</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>Minute<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//rotate 最小为1分钟轮询。默认60s 低于1分钟就按1分钟来</span> <span class="token punctuation">)</span> <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span> log<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"failed to create rotatelogs: %s"</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">}</span> logrus<span class="token punctuation">.</span><span class="token function">SetOutput</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span> f <span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">100</span><span class="token punctuation">;</span> i<span class="token operator"> </span> <span class="token punctuation">{</span> logrus<span class="token punctuation">.</span><span class="token function">WithFields</span><span class="token punctuation">(</span>logrus<span class="token punctuation">.</span>Fields<span class="token punctuation">{</span> <span class="token string">"animal"</span><span class="token punctuation">:</span> <span class="token string">"walrus"</span><span class="token punctuation">,</span> <span class="token string">"number"</span><span class="token punctuation">:</span> i<span class="token punctuation">,</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">"A walrus appears"</span><span class="token punctuation">)</span> logrus<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"xxxxxxxxxxxxxxx"</span><span class="token punctuation">)</span> logrus<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"xxxxxxxxxxxxxxx2"</span><span class="token punctuation">)</span> logrus<span class="token punctuation">.</span><span class="token function">Error</span><span class="token punctuation">(</span><span class="token string">"xxxxxxxxxxxxxxx3"</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">1000000</span><span class="token punctuation">;</span> i<span class="token operator"> </span> <span class="token punctuation">{</span> <span class="token keyword">go</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span><span class="token number">121</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span> <span class="token punctuation">}</span></code></pre> 到此这篇关于“go语言之日志,按日期轮询file-rotatelogs”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
删除及设置linux日志笔记
shell脚本:MySQL慢查询日志和错误日志按天轮询
MYSQL启用日志,查看日志,利用Mysqlbinlog工具恢复MySQL数据库
mysql打开慢查询日志的方法
利用Mysqlbinlog工具恢复MySQL数据库的例子
如何开启mysql和php慢日志
为开发者准备的10款最好的jQuery日历插件
教你删除MYSQl的BIN-LOG日志
学习mysql binlog日志清理
PHP记录和读取JSON格式日志文件

[关闭]
~ ~