Write to custom log file from a Bash script

前端 未结 5 605
渐次进展
渐次进展 2021-02-03 18:47

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!\"
<         


        
相关标签:
5条回答
  • 2021-02-03 18:55

    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.html The post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit.

    Sample Log:

    0 讨论(0)
  • 2021-02-03 19:02

    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
    

    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:

    logger -s "foo bar" >> /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).

    0 讨论(0)
  • 2021-02-03 19:03

    @chepner make a good point that logger is dedicated to logging messages.

    I do need to mention that @Thomas Haratyk simply inquired why I didn't simply use echo.

    At the time, I didn't know about echo, as I'm learning shell-scripting, but he was right.

    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)
    0 讨论(0)
  • 2021-02-03 19:05

    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.

    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.

    0 讨论(0)
  • 2021-02-03 19:18

    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.

    Whenever I run the command logger -t SRI some message, I want "some message" to only show up in /var/log/sri.log.

    To do this I added the file /etc/rsyslog.d/00-sri.conf with the following content.

    # 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:

    systemctl restart rsyslog.service
    

    Here is a shell session showing the results:

    [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]#
    
    0 讨论(0)
提交回复
热议问题