从 Bash 脚本写入自定义日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15045946/
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
Write to custom log file from a Bash script
提问by coffeemonitor
In Linux, I know how to write a simply message to the /var/log/messages
file, in a simple shell script I created:
在 Linux 中,我知道如何/var/log/messages
在我创建的一个简单的 shell 脚本中向文件写入一条简单的消息:
#!/bin/bash
logger "have fun!"
I want to stop throwing messages into the default /var/log/messages
file, and create my own.
我想停止将消息扔到默认/var/log/messages
文件中,并创建我自己的。
I tried this:
我试过这个:
#!/bin/bash
logger "have more fun" > /var/log/mycustomlog
It still logs to /var/log/messages
. It did create the /var/log/mycustomlog
, but it's empty.
它仍然登录到/var/log/messages
. 它确实创建了/var/log/mycustomlog
,但它是空的。
Anyone see what I'm missing?
有人看到我缺少什么吗?
采纳答案by Ansgar Wiechers
logger
logs to syslog facilities. If you want the message to go to a particular file you have to modify the syslog configuration accordingly. You could add a line like this:
logger
日志到系统日志设施。如果您希望消息转到特定文件,则必须相应地修改 syslog 配置。你可以添加这样的一行:
local7.* -/var/log/mycustomlog
and restart syslog. Then you can log like this:
并重新启动系统日志。然后你可以像这样登录:
logger -p local7.info "information message"
logger -p local7.err "error message"
and the messages will appear in the desired logfile with the correct log level.
并且消息将以正确的日志级别出现在所需的日志文件中。
Without making changes to the syslog configuration you could use logger
like this:
在不更改 syslog 配置的情况下,您可以logger
像这样使用:
logger -s "foo bar" 2>> /var/log/mycustomlog
That would instruct logger
to print the message to STDERR as well (in addition to logging it to syslog), so you could redirect STDERR to a file. However, it would be utterly pointless, because the message is already logged via syslog anyway (with the default priority user.notice
).
这也会指示logger
将消息打印到 STDERR(除了将其记录到 syslog 之外),因此您可以将 STDERR 重定向到一个文件。但是,这完全没有意义,因为无论如何该消息已经通过系统日志记录了(具有默认优先级user.notice
)。
回答by coffeemonitor
@chepner make a good point that logger
is dedicated to logging messages.
@chepner 提出了一个logger
专门用于记录消息的好点。
I do need to mention that @Thomas Haratyk simply inquired why I didn't simply use echo
.
我确实需要提到@Thomas Haratyk 只是询问我为什么不简单地使用echo
.
At the time, I didn't know about echo, as I'm learning shell-scripting
, but he was right.
当时,我不知道回声,因为我正在学习shell-scripting
,但他是对的。
My simple solution is now this:
我的简单解决方案现在是这样的:
#!/bin/bash
echo "This logs to where I want, but using echo" > /var/log/mycustomlog
The example above will overwrite the file after the >
上面的例子将覆盖 > 之后的文件
So, I can append to that file with this:
所以,我可以用这个附加到该文件:
#!/bin/bash
echo "I will just append to my custom log file" >> /var/log/customlog
Thanks guys!
谢谢你们!
- on a side note, it's simply my personal preference to keep my personal logs in
/var/log/
, but I'm sure there are other good ideas out there. And since I didn't create a daemon,/var/log/
probably isn't the best place for my custom log file. (just saying)
- 附带说明一下,保留个人日志只是我个人的偏好
/var/log/
,但我相信还有其他好主意。而且由于我没有创建守护程序,因此/var/log/
可能不是我的自定义日志文件的最佳位置。(只是说)
回答by Piyush Chordia
There's good amount of detail on logging for shell scripts via global varaibles of shell. We can emulate the similar kind of logging in shell script: http://www.cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.htmlThe post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit.
关于通过 shell 的全局变量记录 shell 脚本的大量细节。我们可以在 shell 脚本中模拟类似的日志记录:http://www.cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html 这篇文章详细介绍了 INFO、DEBUG、错误。跟踪详细信息,如脚本入口、脚本出口、函数入口、函数出口。
回答by pallav_rus
If you see the man page of logger:
如果您看到记录器的手册页:
$ man logger
LOGGER(1) BSD General Commands Manual LOGGER(1)
NAME logger — a shell command interface to the syslog(3) system log module
SYNOPSIS logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...]
DESCRIPTION Logger makes entries in the system log. It provides a shell command interface to the syslog(3) system log module.
LOGGER(1) BSD 通用命令手册 LOGGER(1)
NAME logger — syslog(3) 系统日志模块的 shell 命令接口
概要记录器 [-isd] [-f 文件] [-p pri] [-t 标记] [-u 套接字] [消息 ...]
描述 Logger 在系统日志中创建条目。它为 syslog(3) 系统日志模块提供了一个 shell 命令接口。
It Clearly says that it will log to system log. If you want to log to file, you can use ">>" to redirect to log file.
它清楚地表明它将登录到系统日志。如果要登录到文件,可以使用“>>”重定向到日志文件。
回答by shrewmouse
I did it by using a filter. Most linux systems use rsyslog these days. The config files are located at /etc/rsyslog.conf
and /etc/rsyslog.d
.
我是通过使用过滤器做到的。现在大多数 linux 系统都使用 rsyslog。配置文件位于/etc/rsyslog.conf
和/etc/rsyslog.d
。
Whenever I run the command logger -t SRI some message
, I want "some message" to only show up in /var/log/sri.log
.
每当我运行命令时logger -t SRI some message
,我都希望“一些消息”只出现在/var/log/sri.log
.
To do this I added the file /etc/rsyslog.d/00-sri.conf
with the following content.
为此,我添加了/etc/rsyslog.d/00-sri.conf
具有以下内容的文件。
# Filter all messages whose tag starts with SRI
# Note that 'isequal, "SRI:"' or 'isequal "SRI"' will not work.
#
:syslogtag, startswith, "SRI" /var/log/sri.log
# The stop command prevents this message from getting processed any further.
# Thus the message will not show up in /var/log/messages.
#
& stop
Then restart the rsyslogd service:
然后重启rsyslogd服务:
systemctl restart rsyslog.service
Here is a shell session showing the results:
这是一个显示结果的 shell 会话:
[root@rpm-server html]# logger -t SRI Hello World!
[root@rpm-server html]# cat /var/log/sri.log
Jun 5 10:33:01 rpm-server SRI[11785]: Hello World!
[root@rpm-server html]#
[root@rpm-server html]# # see that nothing shows up in /var/log/messages
[root@rpm-server html]# tail -10 /var/log/messages | grep SRI
[root@rpm-server html]#