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

编程入门 行业动态 更新时间:2024-10-27 02:28:38
本文介绍了从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脚本写入自定义日志文件

本文发布于:2023-11-05 21:22:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   脚本   文件   日志   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!