Home

Awesome

shell-logger

Logger for shell script, working on Bash and Zsh.

This includes functions of debug, info(information), notice(notification), warn(warning) and err(error). Each output is formatted with date-time and colored by each color definition.

These color codes are removed when the output is passed to a pipe or written into files.

shelllogger

colors

traceback

Installation

You can use an install script on the web like:

$ curl -fsSL https://raw.github.com/rcmdnk/shell-logger/install/install.sh| sh

This will install scripts to /usr/etc and you may be asked root password.

If you want to install other directory, do like:

$ curl -fsSL https://raw.github.com/rcmdnk/shell-logger/install/install.sh|  prefix=~/usr/local/ sh

On Mac, you can install etc/shell-logger by Homebrew:

$ brew tap rcmdnk/rcmdnkpac/shell-logger

The file will be installed in $(brew --prefix)/etc (normally /usr/local/etc).

Otherwise download shell-logger and place it where you like.

Once shell-logger is installed, source it in your .bashrc or .zshrc like:

source /path/to/shell-logger

Usage

In your script, source shell-logger:

source /path/to/shell-logger

Then, you can use such info or err command in your script like:

#!/bin/bash

source /usr/local/etc/shell-logger

test_command
ret=$?
if [ ret = 0 ];then
  info Command succeeded.
else
  err Command failed!
fi

Each level has each functions:

LEVELFunctions
DEBUGdebug
INFOinfo, information
NOTICEnotice, notification
WARNINGwarn, warning
ERRORerr, error

Options

Variable NameDescriptionDefault
LOGGER_DATE_FORMATOutput date format.'%Y/%m/%d %H:%M:%S'
LOGGER_LEVEL0: DEBUG, 1: INFO, 2: NOTICE, 3: WARN, 4: ERROR1
LOGGER_STDERR_LEVELFor levels greater than equal this level, outputs will go stderr.4
LOGGER_DEBUG_COLORColor for DEBUG3 (Italicized. Some terminal shows it as color inversion)
LOGGER_INFO_COLORColor for INFO"" (Use default output color)
LOGGER_NOTICE_COLORColor for NOTICE36 (Front color cyan)
LOGGER_WARNING_COLORColor for WARNING33 (Front color yellow)
LOGGER_ERROR_COLORColor for ERROR31 (Front color red)
LOGGER_COLORColor mode: never->Always no color. auto->Put color only for terminal output. always->Always put color.auto
LOGGER_LEVELSNames printed for each level. Need 5 names.("DEBUG" "INFO" "NOTICE" "WARNING" "ERROR")
LOGGER_SHOW_TIMEShow time information1
LOGGER_SHOW_FILEShow file/line information1
LOGGER_SHOW_LEVELShow level1
LOGGER_ERROR_RETURN_CODEError reutrn code of err/error100
LOGGER_ERROR_TRACEIf 1, error trace back is shown by err/error1
LOGGER_FILE_OUTPUTIf set, output is written to the file."" (Not output file is defined)
LOGGER_FILE_ONLYIf 1 and LOGGER_FILE_OUTPUT is set, not output will be given to stdout/stderr.0
LOGGER_FILE_LEVELOutput level for the file.Same as LOGGER_LEVEL
LOGGER_FILE_APPENDIf 1, output is appended to the file. Otherwise, the file is overwritten.0

About colors, you can find the standard definitions in Standard ECMA-48 (p61, p62).

Normal are:

NumberColor definition
30black display
31red display
32green display
33yellow display
34blue display
35magenta display
36cyan display
37white display
40black background
41red background
42green background
43yellow background
44blue background
45magenta background
46cyan background
47white background

You can set display (letter's color) and background in the same time. For example, if you want to use red background and white front color for error output, set:

_LOGGER_ERROR_COLOR="37;41"

You can easily check colors by escseqcheck.