Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for LOG_LEVEL #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"

```
Expand All @@ -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.
Expand All @@ -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.
10 changes: 9 additions & 1 deletion bash-logger.sh
Original file line number Diff line number Diff line change
@@ -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
#--------------------------------------------------------------------------------------------------
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion examples.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -63,3 +78,4 @@ LOG_HANDLER_DEFAULT() {
echo "logged to logfile"
}
NOTICE "test notice log"