diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 5328cc78..e2fc7bd5 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -4,12 +4,19 @@ This page summarizes a couple of rules to keep in mind when developing features ## Debugging and Logging +### General Logging + While developing feature or making changes in general, you can log error/warning/debug using `_log_error` `_log_warning` and `_log_debug`. This will help you solve problems quicker and also propagate important notes to other users of Bash-it. You can see the logs by using `bash-it doctor` command to reload and see the logs. Alternatively, you can set `BASH_IT_LOG_LEVEL` to `BASH_IT_LOG_LEVEL_ERROR`, `BASH_IT_LOG_LEVEL_WARNING` or `BASH_IT_LOG_LEVEL_ALL`. +### Log Prefix/Context + +You can define `BASH_IT_LOG_PREFIX` in your files in order to a have a constant prefix before your logs. +Note that we prefer to uses "tags" based logging, i.e `plugins: git: DEBUG: Loading git plugin`. + ## Load Order diff --git a/lib/log.bash b/lib/log.bash index 8f954359..afc6b1ac 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -6,12 +6,13 @@ export BASH_IT_LOG_LEVEL_ALL=3 function _log_general() { - _about 'Internal function used for logging' + _about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix' _param '1: color of the message' _param '2: message to log' _group 'log' - _has_colors && echo -e "$1$2${echo_normal}" || echo -e "$2" + message=${BASH_IT_LOG_PREFIX}$2 + _has_colors && echo -e "$1${message}${echo_normal}" || echo -e "${message}" } function _log_debug() diff --git a/test/lib/log.bats b/test/lib/log.bats index da31a9b5..ad1cdf07 100644 --- a/test/lib/log.bats +++ b/test/lib/log.bats @@ -77,3 +77,10 @@ load ../../lib/log run _log_error "test test test" refute_output } + +@test "lib log: logging with prefix" { + BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL + BASH_IT_LOG_PREFIX="nice: prefix: " + run _log_debug "test test test" + assert_output "nice: prefix: DEBUG: test test test" +}