From 5b7ff2e0cb88a70fac3b5ed5a0cd22bf5477b2e2 Mon Sep 17 00:00:00 2001 From: Adam Stradomski Date: Thu, 11 Nov 2021 06:28:26 +0100 Subject: [PATCH 1/5] Added support for LOG_LEVEL LOG_LEVEL="ERROR" will suppress any logs below the set level. LOG_LEVEL="DEBUG" will show all logs. --- LICENSE | 2 +- README.md | 11 ++++++++--- bash-logger.sh | 7 ++++++- examples.sh | 18 +++++++++++++++++- 4 files changed, 32 insertions(+), 6 deletions(-) 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..09f33d7 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 @@ -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` +- Logs above level ERROR will be logged. 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..b59fff3 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,9 @@ 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="ERROR" export RESET_COLOR="\033[0m" +declare -A levels=([DEBUG]=0 [INFO]=1 [NOTICE]=2 [WARNING]=3 [ERROR]=4 [CRITICAL]=5 [ALERT]=6 [EMERGENCY]=7 ) #-------------------------------------------------------------------------------------------------- # Individual Log Functions @@ -74,6 +76,9 @@ LOG() { LOG_HANDLER_DEFAULT() { # $1 - level # $2 - message + [[ ${levels[$1]} ]] || return 1 + [[ ${levels[$LOG_LEVEL]} ]] || LOG_LEVEL=$LOG_LEVEL_DEFAULT + (( ${levels[$1]} < ${levels[$LOG_LEVEL]} )) && return 2 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" + From 0e97adce77ffad2a2b2b20dd475a8848185eaa7c Mon Sep 17 00:00:00 2001 From: Adam Stradomski Date: Thu, 11 Nov 2021 06:31:03 +0100 Subject: [PATCH 2/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 09f33d7..5cd99c4 100755 --- a/README.md +++ b/README.md @@ -16,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" ``` From 20c785e3c7ffb30d08b56d546552923caef52636 Mon Sep 17 00:00:00 2001 From: Adam Stradomski Date: Thu, 11 Nov 2021 18:47:03 +0100 Subject: [PATCH 3/5] set default log level, when LOG_LEVEL is empty --- bash-logger.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bash-logger.sh b/bash-logger.sh index b59fff3..e836555 100755 --- a/bash-logger.sh +++ b/bash-logger.sh @@ -24,6 +24,9 @@ export LOG_LEVEL_DEFAULT="ERROR" 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 # These can be overwritten to provide custom behavior for different log levels @@ -76,9 +79,9 @@ LOG() { LOG_HANDLER_DEFAULT() { # $1 - level # $2 - message - [[ ${levels[$1]} ]] || return 1 - [[ ${levels[$LOG_LEVEL]} ]] || LOG_LEVEL=$LOG_LEVEL_DEFAULT - (( ${levels[$1]} < ${levels[$LOG_LEVEL]} )) && return 2 + [[ ${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" From 27a1ae36714055aa05eb695fb04e44ce261f16b3 Mon Sep 17 00:00:00 2001 From: Adam Stradomski Date: Thu, 11 Nov 2021 18:51:00 +0100 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cd99c4..e3001c3 100755 --- a/README.md +++ b/README.md @@ -16,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" + LOG_LEVEL="INFO" INFO "This is a test info log" ``` From b203487f5bc34484d31af774430681cdfd8cfafc Mon Sep 17 00:00:00 2001 From: Adam Stradomski Date: Thu, 11 Nov 2021 18:53:00 +0100 Subject: [PATCH 5/5] change default to INFO --- README.md | 2 +- bash-logger.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e3001c3..18cf07d 100755 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Bash Logger supports the logging levels described by [RFC 5424](http://tools.iet By default: - Logs are displayed in colour - Logs are written to `~/bash-logger.log` -- Logs above level ERROR will be logged. Use LOG_LEVEL="DEBUG" to log all log levels. +- 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 e836555..b09b8ec 100755 --- a/bash-logger.sh +++ b/bash-logger.sh @@ -20,7 +20,7 @@ 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="ERROR" +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 )