diff --git a/LICENSE b/LICENSE index 41ead3f..74fbf2c 100755 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2009-2011, Dean Rather and contributors. +Copyright (c) 2009-2021, Dean Rather, Adam Stradomski and contributors. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index 111a35b..18cf07d 100755 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ Fork of Bash Logger designed to incorperate [PSR-3](http://www.php-fig.org/psr/p ## Contributors -- Dean Rather - Fred Palmer +- Dean Rather +- Adam Stradomski ## Using Bash Logger @@ -15,7 +16,7 @@ Fork of Bash Logger designed to incorperate [PSR-3](http://www.php-fig.org/psr/p #!/bin/bash source /path/to/bash-logger.sh - + LOG_LEVEL="INFO" INFO "This is a test info log" ``` @@ -30,8 +31,7 @@ Fork of Bash Logger designed to incorperate [PSR-3](http://www.php-fig.org/psr/p ### Logging Levels -Bash Logger supports the logging levels described by [RFC 5424](http://tools.ietf.org/html/rfc5424). - +Bash Logger supports the logging levels described by [RFC 5424](http://tools.ietf.org/html/rfc5424). Logging levels in order of priority: - **DEBUG** Detailed debug information. - **INFO** Interesting events. Examples: User logs in, SQL logs. @@ -54,11 +54,16 @@ Bash Logger supports the logging levels described by [RFC 5424](http://tools.iet - **EMERGENCY** Emergency: system is unusable. +### Log level + +- Use the LOG_LEVEL variable to determine the logs to be logged. For example, LOG_LEVEL="INFO" will disable logging of debug level messages. + ## Handlers By default: - Logs are displayed in colour - Logs are written to `~/bash-logger.log` +- Only logs above level DEBUG will be logged by default. Use LOG_LEVEL="DEBUG" to log all log levels. - **error** level logs and above `exit` with an error code The colours, logfile, default behavior, and log-level behavior can all be overwritten, see [examples.sh](examples.sh) for examples. diff --git a/bash-logger.sh b/bash-logger.sh index acbb129..b09b8ec 100755 --- a/bash-logger.sh +++ b/bash-logger.sh @@ -1,7 +1,7 @@ #!/bin/bash #-------------------------------------------------------------------------------------------------- # Bash Logger -# Copyright (c) Dean Rather +# Copyright (c) Dean Rather, Adam Stradomski # Licensed under the MIT license # http://github.com/deanrather/bash-logger #-------------------------------------------------------------------------------------------------- @@ -20,7 +20,12 @@ export LOG_COLOR_ERROR="\033[1;31m" # Red export LOG_COLOR_CRITICAL="\033[44m" # Blue Background export LOG_COLOR_ALERT="\033[43m" # Yellow Background export LOG_COLOR_EMERGENCY="\033[41m" # Red Background +export LOG_LEVEL_DEFAULT="INFO" export RESET_COLOR="\033[0m" +declare -A levels=([DEBUG]=0 [INFO]=1 [NOTICE]=2 [WARNING]=3 [ERROR]=4 [CRITICAL]=5 [ALERT]=6 [EMERGENCY]=7 ) + +# If global LOG_LEVEL is empty, set default log level +[[ -z $LOG_LEVEL ]] && LOG_LEVEL=$LOG_LEVEL_DEFAULT #-------------------------------------------------------------------------------------------------- # Individual Log Functions @@ -74,6 +79,9 @@ LOG() { LOG_HANDLER_DEFAULT() { # $1 - level # $2 - message + [[ ${levels[$1]} ]] || return 1 ## unknown log level for log message + [[ ${levels[$LOG_LEVEL]} ]] || LOG_LEVEL=$LOG_LEVEL_DEFAULT ## unknown global log level + (( ${levels[$1]} < ${levels[$LOG_LEVEL]} )) && return 2 ## log message log level is lower than global log level local formatted_log="$(FORMAT_LOG "$@")" LOG_HANDLER_COLORTERM "$1" "$formatted_log" LOG_HANDLER_LOGFILE "$1" "$formatted_log" diff --git a/examples.sh b/examples.sh index 9bd3865..af5ea90 100755 --- a/examples.sh +++ b/examples.sh @@ -1,11 +1,14 @@ #!/bin/bash #-------------------------------------------------------------------------------------------------- # Bash Logger -# Copyright (c) Dean Rather +# Copyright (c) Dean Rather, Adam Stradomski # Licensed under the MIT license # http://github.com/deanrather/bash-logger #-------------------------------------------------------------------------------------------------- +# Log level can be set outside scripts +export LOG_LEVEL="DEBUG" + # Including the logger functions source bash-logger.sh @@ -53,6 +56,18 @@ CRITICAL "Example Critical log" ALERT "Example Alert log" EMERGENCY "Example Emergency log" +export LOG_LEVEL="NOTICE" +# Example of all log levels +echo # newline +DEBUG "Example Debug log will not be logged" +INFO "Example Info log will not be logged" +NOTICE "Example Notice log will be logged" +WARNING "Example Warning log will be logged" +ERROR "Example Error log will be logged" +CRITICAL "Example Critical log will be logged" +ALERT "Example Alert log will be logged" +EMERGENCY "Example Emergency log will be logged" + # Overwriting default log behavior (eg. adding another echo) echo # newline INFO "Adding additional default behavior" @@ -63,3 +78,4 @@ LOG_HANDLER_DEFAULT() { echo "logged to logfile" } NOTICE "test notice log" +