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

I needed the ability to set arbitrary tags. #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,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
Expand All @@ -49,17 +50,23 @@ 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:

$ cat example2.sh
#!/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`
2 changes: 2 additions & 0 deletions example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
. includes/bunyan

__bunyanSetLevel "trace"
bunyanFields[user]+=`whoami`
info "hello world"
trace "hello world"

ls -l | while read x; do trace "$x"; done
63 changes: 37 additions & 26 deletions includes/bunyan
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,51 @@

version='0.1.0'

__bunyanName=`basename $0`
__bunyanHost=`hostname`
__bunyanLevel=30 # info
__bunyanLevel=20 # info

declare -A bunyanFields
bunyanFields[name]+=`basename $0`
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
shift
printf "{\"name\":\"%s\",\"hostname\":\"%s\",\"pid\":%s,\"level\":%d,\"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \
"$__bunyanName" "$__bunyanHost" "$$" "$level" "$@" "$now"
levelname=$2

shift 2

extra=""
for i in "${!bunyanFields[@]}"; do
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"

}

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
Expand All @@ -44,26 +55,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 "$@"
}