bash 如何:将 syslog 用于用户创建的 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6646362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:20:33  来源:igfitidea点击:

Howto: Using syslog for user created shell script

linuxbashshellubuntusyslog

提问by David Parks

Information abound about syslog, but I can't find anything very concise for my interest.

关于 syslog 的信息比比皆是,但我找不到任何符合我兴趣的简洁信息。

I have a user-created bash script that should log various debug, info, and error messages. I'd like to use syslog. This in Ubuntu Server distribution.

我有一个用户创建的 bash 脚本,它应该记录各种调试、信息和错误消息。我想使用系统日志。这在 Ubuntu Server 发行版中。

I'm looking for a quick overview only.

我只是在寻找快速概览。

  • I see many files in /etc/logrotate.dthat don't get discussed in any man pages that confuse me.
  • Should I be logging as user? local0-7?
  • Do I need to do something to configure this before I use these in a logger command?
  • How should I define what logs get created? Or is this already done?
  • 我看到很多文件/etc/logrotate.d都没有在任何让我感到困惑的手册页中讨论。
  • 我应该以用户身份登录吗?本地0-7?
  • 在记录器命令中使用这些之前,我需要做些什么来配置它吗?
  • 我应该如何定义创建的日志?或者这已经完成了?

With those questions answered I should be able to glean the details from the man pages.

回答完这些问题后,我应该能够从手册页中收集详细信息。

回答by sorpigal

You want the logger(1) utility, available in the bsdutils package.

您需要 logger(1) 实用程序,可在 bsdutils 包中使用。

From the man page:

从手册页:

     logger - a shell command interface to the syslog(3) system log module

There's nothing that's essential to configure, just pass the switches you want. E.g.

没有什么是必要的配置,只需传递你想要的开关。例如

logger -p local3.info -t myprogram "What's up, doc?"

You can now inspect wherever local3.info messages go and you will see something like this:

您现在可以检查 local3.info 消息的任何位置,您将看到如下内容:

Jul 11 12:46:35 hostname myprogram: What's up, doc?

You only need to worry about logrotate if you need something fancier than this.

如果您需要比这更高级的东西,您只需要担心 logrotate。

As for what log facility to use, I would use daemon for daemon messages and local for most other things. You should consult syslog(3) for the purposes of the different facilities.

至于使用什么日志工具,我会使用 daemon 来处理守护进程消息,而使用本地来处理大多数其他事情。为了不同的设施,您应该查阅 syslog(3)。

回答by Ignacio Vazquez-Abrams

  • Don't worry about logrotate. It doesn't affect you if you're logging to the system log.
  • You can use any facility you like. See the syslogd configuration for what ends up where.
  • See the syslogd configuration for what ends up where.
  • See the... yeah, you get it.
  • 不要担心 logrotate。如果您登录到系统日志,它不会影响您。
  • 您可以使用任何您喜欢的设施。请参阅 syslogd 配置以了解最终结果。
  • 请参阅 syslogd 配置以了解最终结果。
  • 看到...是的,你明白了。