从Bash脚本写入自定义日志文件 [英] Write to custom log file from a Bash script

查看:139
本文介绍了从Bash脚本写入自定义日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

#!/bin/bash
logger "have fun!"

我想停止将消息扔到默认的/var/log/messages 文件中,然后创建自己的消息.

I want to stop throwing messages into the default /var/log/messages file, and create my own.

我尝试过:

#!/bin/bash
logger "have more fun" > /var/log/mycustomlog

它仍然记录到/var/log/messages .它确实创建了/var/log/mycustomlog ,但它为空.

It still logs to /var/log/messages. It did create the /var/log/mycustomlog, but it's empty.

有人知道我想念什么吗?

Anyone see what I'm missing?

推荐答案

logger 记录到syslog工具.如果要将消息发送到特定文件,则必须相应地修改syslog配置.您可以添加这样的行:

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:

local7.*   -/var/log/mycustomlog

并重新启动syslog.然后您可以像这样登录:

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.

无需更改系统日志配置,您可以像这样使用 logger :

Without making changes to the syslog configuration you could use logger like this:

logger -s "foo bar" >> /var/log/mycustomlog

这将指示 logger 将消息也打印到STDERR(除了将其记录到syslog中),因此您可以将STDERR重定向到文件.但是,这将毫无意义,因为无论如何该消息已经通过syslog记录了(默认优先级为 user.notice ).

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

这篇关于从Bash脚本写入自定义日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆