From a09334910ec6cd3b6c606f600892319ca46abf95 Mon Sep 17 00:00:00 2001 From: JessFlan Date: Fri, 15 Feb 2019 14:24:04 +1100 Subject: [PATCH 01/10] Note bash version --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8288804..1b233fa 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Bash-Bunyan +This version uses associative arrays and requires bash version 4 This tool is based off [trentms](http://github.com/trentm) excellent [node-bunyan](https://github.com/trentm/node-bunyan) utility. It's used for creating structured logs using the JSON format. The output is JSON which can be From 0428457f131f1b3a8fad492e4bffea30c115a4bd Mon Sep 17 00:00:00 2001 From: JessFlan Date: Fri, 15 Feb 2019 14:24:57 +1100 Subject: [PATCH 02/10] Update bunyan --- includes/bunyan | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/includes/bunyan b/includes/bunyan index 7cc0b5f..6570cad 100755 --- a/includes/bunyan +++ b/includes/bunyan @@ -6,14 +6,24 @@ __bunyanName=`basename $0` __bunyanHost=`hostname` __bunyanLevel=30 # info +declare -A bunyanFields +bunyanFields[name]+=`basename $0` +bunyanFields[hostname]+=`hostname` + function __bunyan() { # arg 1 is LEVEL # arg 2 is MSG now=$(date -u +"%Y-%m-%dT%H:%M:%SZ") level=$1 - shift - printf "{\"name\":\"%s\",\"hostname\":\"%s\",\"pid\":%s,\"level\":%d,\"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \ - "$__bunyanName" "$__bunyanHost" "$$" "$level" "$@" "$now" + shift + + extra="" + for i in "${!bunyanFields[@]}"; do + extra+=$(printf \"%s\":\"%s\", $i ${bunyanFields[$i]}) + done ; + printf "{$extra \"pid\":%s,\"level\":%d,\"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \ + "$$" "$level" "$@" "$now" + } function __bunyanSetLevel() { From 88e74fbb6c2f5c57274fcad2aa9d1999ac45c241 Mon Sep 17 00:00:00 2001 From: JessFlan Date: Fri, 15 Feb 2019 14:25:46 +1100 Subject: [PATCH 03/10] show adding additional fields --- example.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/example.sh b/example.sh index 9ddc0f6..c75f429 100644 --- a/example.sh +++ b/example.sh @@ -1,8 +1,10 @@ #!/usr/bin/env bash -. includes/bunyan +. includes/bunyan2 __bunyanSetLevel "trace" +bunyanFields[user]+=`whoami` info "hello world" trace "hello world" +ls -l | while read x; do trace "$x"; done From fa894df50bc9a94a8e425f1b438fad34b9df0b34 Mon Sep 17 00:00:00 2001 From: JessFlan Date: Fri, 15 Feb 2019 14:27:18 +1100 Subject: [PATCH 04/10] Update bunyan --- includes/bunyan | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/bunyan b/includes/bunyan index 6570cad..238ccf4 100755 --- a/includes/bunyan +++ b/includes/bunyan @@ -2,8 +2,6 @@ version='0.1.0' -__bunyanName=`basename $0` -__bunyanHost=`hostname` __bunyanLevel=30 # info declare -A bunyanFields From e07ac0ae899d9d53a1405c979eee7c56a6a0e7bf Mon Sep 17 00:00:00 2001 From: JessFlan Date: Fri, 15 Feb 2019 14:29:34 +1100 Subject: [PATCH 05/10] Swap documentation to show associative array. --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1b233fa..569a582 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,10 @@ is set to 'info'. ## Settings bash-bunyan doesn't have nearly the granularity of node-bunyan, but you can set -the name of the process reported in bunyan by setting the '\_\_bunyanName' -variable. This variable will be set automatically in your script when you source +the name of the process reported in bunyan by setting: +bunyanFields[name] + +This variable will be set automatically in your script when you source the bunyan include file. ex: @@ -59,8 +61,12 @@ ex: #!/usr/bin/bash . includes/bunyan - __bunyanName='super' + bunyanFields[name]+=super info 'hello world' ~ $ sh example2.sh | bunyan [2012-03-24T02:47:15Z] INFO: super/49105 on mac.local: hello world + +Additional flags can also be set as seen in the example: + +bunyanFields[user]+=`whoami` From 7717b4a527754451b51efbe751ec1fedf91549c6 Mon Sep 17 00:00:00 2001 From: JessFlan Date: Fri, 15 Feb 2019 14:30:43 +1100 Subject: [PATCH 06/10] remove debug code --- example.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.sh b/example.sh index c75f429..14ac97e 100644 --- a/example.sh +++ b/example.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -. includes/bunyan2 +. includes/bunyan __bunyanSetLevel "trace" bunyanFields[user]+=`whoami` From 417fd20dff54e2aa8115e9e608a8f23e2e3c6d70 Mon Sep 17 00:00:00 2001 From: Jess Flanagan Date: Wed, 19 Jun 2019 13:11:49 +1000 Subject: [PATCH 07/10] Change to add LevelName and match python log levels --- includes/bunyan | 51 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/includes/bunyan b/includes/bunyan index 238ccf4..a512797 100755 --- a/includes/bunyan +++ b/includes/bunyan @@ -2,7 +2,7 @@ version='0.1.0' -__bunyanLevel=30 # info +__bunyanLevel=20 # info declare -A bunyanFields bunyanFields[name]+=`basename $0` @@ -10,40 +10,45 @@ bunyanFields[hostname]+=`hostname` function __bunyan() { # arg 1 is LEVEL + # arg 2 is LEVELNAME # arg 2 is MSG now=$(date -u +"%Y-%m-%dT%H:%M:%SZ") level=$1 + levelname=$2 + + echo $2 + shift shift extra="" for i in "${!bunyanFields[@]}"; do extra+=$(printf \"%s\":\"%s\", $i ${bunyanFields[$i]}) done ; - printf "{$extra \"pid\":%s,\"level\":%d,\"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \ - "$$" "$level" "$@" "$now" + printf "{$extra \"pid\":%s,\"level\":%d,\"levelname\":\"%s\", \"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \ + "$$" "$level" "$levelname" "$@" "$now" } function __bunyanSetLevel() { newlevel=$1 case "$newlevel" in - trace) - __bunyanLevel=10 + noset) + __bunyanLevel=0 ;; debug) - __bunyanLevel=20 + __bunyanLevel=10 ;; info) - __bunyanLevel=30 + __bunyanLevel=20 ;; warn) - __bunyanLevel=40 + __bunyanLevel=30 ;; error) - __bunyanLevel=50 + __bunyanLevel=40 ;; - fatal) - __bunyanLevel=60 + critical) + __bunyanLevel=50 ;; *) printf "unknown log level '$1'\n" >&2 @@ -52,26 +57,26 @@ function __bunyanSetLevel() { } -function trace() { - [[ $__bunyanLevel -le 10 ]] && __bunyan 10 "$@" +function noset() { + [[ $__bunyanLevel -le 10 ]] && __bunyan 0 NOTSET "$@" } -function debug() { - [[ $__bunyanLevel -le 20 ]] && __bunyan 20 "$@" +function debug() { + [[ $__bunyanLevel -le 20 ]] && __bunyan 10 DEBUG "$@" } -function info() { - [[ $__bunyanLevel -le 30 ]] && __bunyan 30 "$@" +function info() { + [[ $__bunyanLevel -le 30 ]] && __bunyan 20 INFO "$@" } -function warn() { - [[ $__bunyanLevel -le 40 ]] && __bunyan 40 "$@" +function warn() { + [[ $__bunyanLevel -le 40 ]] && __bunyan 30 WARNING "$@" } -function error() { - [[ $__bunyanLevel -le 50 ]] && __bunyan 50 "$@" +function error() { + [[ $__bunyanLevel -le 50 ]] && __bunyan 40 ERROR "$@" } -function fatal() { - [[ $__bunyanLevel -le 60 ]] && __bunyan 60 "$@" +function critical() { + [[ $__bunyanLevel -le 60 ]] && __bunyan 50 CRITICAL "$@" } From 1d5507d6947c443df8475dc3ee51e2703fb32883 Mon Sep 17 00:00:00 2001 From: Jess Flanagan Date: Wed, 19 Jun 2019 13:26:14 +1000 Subject: [PATCH 08/10] removed accidental debug line --- includes/bunyan | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/includes/bunyan b/includes/bunyan index a512797..153d83a 100755 --- a/includes/bunyan +++ b/includes/bunyan @@ -16,9 +16,7 @@ function __bunyan() { level=$1 levelname=$2 - echo $2 - shift - shift + shift 2 extra="" for i in "${!bunyanFields[@]}"; do From 86038b9bf203d7e77b2ef369fe6332922fe1f992 Mon Sep 17 00:00:00 2001 From: JessFlan Date: Wed, 19 Jun 2019 13:28:33 +1000 Subject: [PATCH 09/10] update log levels --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 569a582..fa1acf0 100644 --- a/README.md +++ b/README.md @@ -36,12 +36,12 @@ ex: When you include bunyan you will automatically inherit functions which correspond to the log levels. These functions are - * trace (60): logging from external libraries - * debug (50): verbose debug information - * info (40): detail on regular information + * noset (0): logging from external libraries + * debug (10): verbose debug information + * info (20): detail on regular information * warn (30): something an operation should pay attention to - * error (20): fatal for a request / action - * fatal (10): the application exited because of some error + * error (40): fatal for a request / action + * critical (50): the application exited because of some error To change the loglevel set the '\_\_bunyanLevel to the appropriate level you care about. Anything under that level will not be logged. By default, the level From 8c29d2cca800dd7a803b33c738a3f75d43c8e903 Mon Sep 17 00:00:00 2001 From: Craig Brown Date: Mon, 29 Jul 2019 08:56:29 +0000 Subject: [PATCH 10/10] Fix for fields with whitespace in them --- includes/bunyan | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/bunyan b/includes/bunyan index 153d83a..b3391f4 100755 --- a/includes/bunyan +++ b/includes/bunyan @@ -20,7 +20,7 @@ function __bunyan() { extra="" for i in "${!bunyanFields[@]}"; do - extra+=$(printf \"%s\":\"%s\", $i ${bunyanFields[$i]}) + extra+=$(printf \"%s\":\"%s\", "$i" "${bunyanFields[$i]}") done ; printf "{$extra \"pid\":%s,\"level\":%d,\"levelname\":\"%s\", \"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \ "$$" "$level" "$levelname" "$@" "$now"