├── example.sh ├── LICENSE ├── includes └── bunyan └── README.md /example.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | . includes/bunyan 4 | 5 | __bunyanSetLevel "trace" 6 | info "hello world" 7 | trace "hello world" 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Trevor Orsztynowicz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /includes/bunyan: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | version='0.1.0' 4 | 5 | __bunyanName=`basename $0` 6 | __bunyanHost=`hostname` 7 | __bunyanLevel=30 # info 8 | 9 | function __bunyan() { 10 | # arg 1 is LEVEL 11 | # arg 2 is MSG 12 | now=$(date -u +"%Y-%m-%dT%H:%M:%SZ") 13 | level=$1 14 | shift 15 | printf "{\"name\":\"%s\",\"hostname\":\"%s\",\"pid\":%s,\"level\":%d,\"msg\":\"%s\",\"time\":\"%s\",\"v\":0}\n" \ 16 | "$__bunyanName" "$__bunyanHost" "$$" "$level" "$@" "$now" 17 | } 18 | 19 | function __bunyanSetLevel() { 20 | newlevel=$1 21 | case "$newlevel" in 22 | trace) 23 | __bunyanLevel=10 24 | ;; 25 | debug) 26 | __bunyanLevel=20 27 | ;; 28 | info) 29 | __bunyanLevel=30 30 | ;; 31 | warn) 32 | __bunyanLevel=40 33 | ;; 34 | error) 35 | __bunyanLevel=50 36 | ;; 37 | fatal) 38 | __bunyanLevel=60 39 | ;; 40 | *) 41 | printf "unknown log level '$1'\n" >&2 42 | ;; 43 | esac 44 | } 45 | 46 | 47 | function trace() { 48 | [[ $__bunyanLevel -le 10 ]] && __bunyan 10 "$@" 49 | } 50 | 51 | function debug() { 52 | [[ $__bunyanLevel -le 20 ]] && __bunyan 20 "$@" 53 | } 54 | 55 | function info() { 56 | [[ $__bunyanLevel -le 30 ]] && __bunyan 30 "$@" 57 | } 58 | 59 | function warn() { 60 | [[ $__bunyanLevel -le 40 ]] && __bunyan 40 "$@" 61 | } 62 | 63 | function error() { 64 | [[ $__bunyanLevel -le 50 ]] && __bunyan 50 "$@" 65 | } 66 | 67 | function fatal() { 68 | [[ $__bunyanLevel -le 60 ]] && __bunyan 60 "$@" 69 | } 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bash-Bunyan 2 | 3 | This tool is based off [trentms](http://github.com/trentm) excellent [node-bunyan](https://github.com/trentm/node-bunyan) utility. It's used for 4 | creating structured logs using the JSON format. The output is JSON which can be 5 | piped to either the 'bunyan' tool itself, or to another JSON parser or to a 6 | file. 7 | 8 | ## Usage 9 | 10 | To use bash-bunyan in your bash script, source the 'bunyan' script then use one 11 | of the level names to log directly. 12 | ex: 13 | 14 | $ cat example.sh 15 | #!/usr/bin/bash 16 | 17 | . includes/bunyan 18 | 19 | info "this is an info message" 20 | $ 21 | ~ 22 | $ sh example.sh | json 23 | { 24 | "name": "example.sh", 25 | "hostname": "mac.local", 26 | "pid": 49121, 27 | "level": 30, 28 | "msg": "this is an info message", 29 | "time": "2012-03-24T02:48:21Z", 30 | "v": 0 31 | } 32 | 33 | ## Levels 34 | 35 | When you include bunyan you will automatically inherit functions which 36 | correspond to the log levels. These functions are 37 | 38 | * trace (60): logging from external libraries 39 | * debug (50): verbose debug information 40 | * info (40): detail on regular information 41 | * warn (30): something an operation should pay attention to 42 | * error (20): fatal for a request / action 43 | * fatal (10): the application exited because of some error 44 | 45 | To change the loglevel set the '\_\_bunyanLevel to the appropriate level you 46 | care about. Anything under that level will not be logged. By default, the level 47 | is set to 'info'. 48 | 49 | ## Settings 50 | 51 | bash-bunyan doesn't have nearly the granularity of node-bunyan, but you can set 52 | the name of the process reported in bunyan by setting the '\_\_bunyanName' 53 | variable. This variable will be set automatically in your script when you source 54 | the bunyan include file. 55 | ex: 56 | 57 | $ cat example2.sh 58 | #!/usr/bin/bash 59 | 60 | . includes/bunyan 61 | __bunyanName='super' 62 | info 'hello world' 63 | ~ 64 | $ sh example2.sh | bunyan 65 | [2012-03-24T02:47:15Z] INFO: super/49105 on mac.local: hello world 66 | --------------------------------------------------------------------------------