Bash Shell脚本 - 写入自定义日志文件

时间:2021-03-17 13:57:10

In Linux, I know how to write a simply message to the /var/log/messages file, in a simple shell script I created:

在Linux中,我知道如何在我创建的简单shell脚本中向/ var / log / messages文件写一条简单的消息:

#!/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?

有人看到我错过了吗?

4 个解决方案

#1


33  

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:

记录器记录到syslog工具。如果要将消息转到特定文件,则必须相应地修改syslog配置。你可以添加这样一行:

local7.*   -/var/log/mycustomlog

and restart syslog. Then you can log like this:

并重新启动syslog。然后你可以像这样记录:

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).

这将指示记录器将消息打印到STDERR(除了将其记录到syslog之外),因此您可以将STDERR重定向到文件。但是,这将是完全没有意义的,因为消息已经通过syslog记录(使用默认优先级user.notice)。

#2


27  

@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脚本,但他是对的。

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 /可能不是我的自定义日志文件的最佳位置。 (只是说)

#3


5  

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://cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html

通过shell的全局变量来记录shell脚本有很多细节。我们可以在shell脚本中模拟类似的日志记录:http://cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html

The post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit.

该帖子详细介绍了介绍日志级别,如INFO,DEBUG,ERROR。跟踪脚本输入,脚本退出,功能输入,功能退出等详细信息。

Sample Log: Bash Shell脚本 - 写入自定义日志文件

样本日志:

#4


3  

If you see the man page of logger:

如果您看到logger的手册页:

$ man logger

LOGGER(1) BSD General Commands Manual LOGGER(1)

LOGGER(1)BSD通用命令手册LOGGER(1)

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

NAME logger - syslog(3)系统日志模块的shell命令接口

SYNOPSIS logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...]

大纲记录器[-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在系统日志中输入条目。它为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.

它清楚地说它会记录到系统日志。如果要登录到文件,可以使用“>>”重定向到日志文件。

#1


33  

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:

记录器记录到syslog工具。如果要将消息转到特定文件,则必须相应地修改syslog配置。你可以添加这样一行:

local7.*   -/var/log/mycustomlog

and restart syslog. Then you can log like this:

并重新启动syslog。然后你可以像这样记录:

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).

这将指示记录器将消息打印到STDERR(除了将其记录到syslog之外),因此您可以将STDERR重定向到文件。但是,这将是完全没有意义的,因为消息已经通过syslog记录(使用默认优先级user.notice)。

#2


27  

@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脚本,但他是对的。

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 /可能不是我的自定义日志文件的最佳位置。 (只是说)

#3


5  

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://cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html

通过shell的全局变量来记录shell脚本有很多细节。我们可以在shell脚本中模拟类似的日志记录:http://cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html

The post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit.

该帖子详细介绍了介绍日志级别,如INFO,DEBUG,ERROR。跟踪脚本输入,脚本退出,功能输入,功能退出等详细信息。

Sample Log: Bash Shell脚本 - 写入自定义日志文件

样本日志:

#4


3  

If you see the man page of logger:

如果您看到logger的手册页:

$ man logger

LOGGER(1) BSD General Commands Manual LOGGER(1)

LOGGER(1)BSD通用命令手册LOGGER(1)

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

NAME logger - syslog(3)系统日志模块的shell命令接口

SYNOPSIS logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...]

大纲记录器[-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在系统日志中输入条目。它为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.

它清楚地说它会记录到系统日志。如果要登录到文件,可以使用“>>”重定向到日志文件。