As a go newb, how do I log properly when running as a service (systemd/upstart)?

agolangf · · 455 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m running a golang program as a service using <code>upstart</code> on Ubuntu 14.04. </p> <p>I&#39;m also very new to golang and loving it. When I just do <code>go run program.go</code> it obviously logs to stderr and I can see it super easily. Obviously this isn&#39;t the case when running as a system service, and I see no logs of any sort that my program may print out.</p> <p>As a complete newb, how do I handle this? Should I capture stderr of the service (somehow) and redirect it into a file? Or should I log specific errors to specific files (this sounds more logical)? What happens when the file gets to be a few megabytes, is there a way to purge it (or should I not be worrying about that)?</p> <hr/>**评论:**<br/><br/>journalctl: <pre><p>Read the documentation for Upstart. I believe by default stdout and stderr are logged to /var/log/upstart/service-name. </p></pre>HadronHubbub: <pre><p>I don&#39;t know about upstart, but systemd by default captures stdout &amp; stderr and stores them in journald. So you don&#39;t need to do anything special in your program.</p></pre>nesigma: <pre><p>This is a piece of cake to do with Upstart. Just add <code>console log</code> in your upstart .conf script then anywhere in your Go app just log something with the standard log package like: <code>log.Println(&#34;Something important happened!&#34;)</code>. </p> <p>If you are using Ubuntu (but probably on other distributions as well), this will log the message under <code>/var/log/upstart</code> in a file <code>&lt;your binary name&gt;.log</code>. </p> <p>The operating system will zip that log file after a while and keep logging on a new one, so there&#39;s no need to worry about it&#39;s size.</p> <p>In my opinion you should keep things simple:</p> <ul> <li>Only log what&#39;s truly important.</li> <li>Start with the standard library log package; it can get you pretty far.</li> <li>Just let upstart handle the logging to one file.</li> </ul> <p>If you come to the conclusion that you <em>truly</em> need something more elaborate, then you can consider other choices, for example logging in a database or structured logging with a 3rd party package etc.</p></pre>hugocat: <pre><p>Why does it show my text as [removed] if I log out?</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

455 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传