3

Is there something like log4j for bash script? I would like to be able to treat errors differently based on their severity. For example, log4j allows me to differentiate between errors, info, debug, etc. Is there a way I can do that for bash?

I know, I can just write a function , that write to log as the following example:

LOG ()  { 
           echo `date` $* >> /var/log/my_log.log 
        } 

But actually I want to know if something like log4j is relevant for bash

in order to get the ability for – error level and etc

  • What does log4j do? If you mean this, which part of its feature set do you want? – terdon Jul 7 '14 at 10:06
  • I want to control the log sevirity as , errors , info , debug , etc , log4j enable that , and what I asked if there are something for bash – maihabunash Jul 7 '14 at 10:08
4

Most systems have the logger utility, which knows how to talk to syslogd. It allows you to set log level (severity), facility name, specify the log file to write to, send to syslogd on a remote host, write messages to STDERR as well as to the system log.

The logging semantics are not quite the same as those provided by tools like log4j, but by combining the facility.level settings with message tags, you can achieve something very close.

Examples

NOTE: These examples use the FreeBSD version of logger. Your system may have different options, so read your local documentation!

logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"

This will send the message to be logged in /var/log/messages with a severity of info, in the local3 facility. It includes a tag (-t MY_LOG_TAG), which is included in each line. Tags are useful for extracting log entries with grep, awk, etc.

logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"

This one sends the message with severity crit in the mail facility to the remote machine loghost, to be logged in /var/log/mail. The -s causes the message to be printed on the the script's STDERR as well as sending it to be logged.

  • can you please provide example how to write to some log by logger command , – maihabunash Jul 7 '14 at 10:16
  • Does this have anything to do with bash errors? As far as I know, logger communicates with syslog which has nothing to do with bash errors. – terdon Jul 7 '14 at 11:39
  • @terdon - I interpreted the question as wanting a logging tool for use in bash scripts. logger is suitable for that, but if the question is specific to logging errors from bash itself, then no - logger won't work, and the question needs to be updated to make it more explicit. – D_Bye Jul 7 '14 at 12:10
1

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/log-tracing-mechnism-for-shell-scripts.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:

enter image description here

0

You may want to take a look at my logging implementation on GitHub:

https://github.com/codeforester/base/blob/master/lib/stdlib.sh

It supports five log levels (ERROR, WARN, INFO, DEBUG, and VERBOSE) as well as multiple loggers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.