Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding log level info

  • Loading branch information...
commit e80455b94d316bdb6dc38035847f12315ac38481 1 parent d3d136a
authored March 23, 2012
19  README.md
Source Rendered
@@ -7,7 +7,8 @@ file.
7 7
 
8 8
 ## Usage
9 9
 
10  
-To use bash-bunyan in your bash script, source the 'bunyan' script
  10
+To use bash-bunyan in your bash script, source the 'bunyan' script then use one
  11
+of the level names to log directly.
11 12
 ex:
12 13
 
13 14
     $ cat example.sh
@@ -29,6 +30,22 @@ ex:
29 30
       "v": 0
30 31
     }
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
+
32 49
 ## Settings
33 50
 
34 51
 bash-bunyan doesn't have nearly the granularity of node-bunyan, but you can set
3  example.sh
... ...
@@ -1,5 +1,8 @@
1 1
 #!/usr/bin/env bash
2 2
 
3 3
 . includes/bunyan
  4
+
  5
+__bunyanSetLevel "trace"
4 6
 info "hello world"
  7
+trace "hello world"
5 8
 
40  includes/bunyan
@@ -4,6 +4,7 @@ version='0.1.0'
4 4
 
5 5
 __bunyanName=`basename $0`
6 6
 __bunyanHost=`hostname`
  7
+__bunyanLevel=30 # info
7 8
 
8 9
 function __bunyan() {
9 10
   # arg 1 is LEVEL
@@ -16,27 +17,54 @@ function __bunyan() {
16 17
   	"$__bunyanName" "$__bunyanHost" "$$" "$level" "$@" "$now"
17 18
 }
18 19
 
  20
+function __bunyanSetLevel() {
  21
+  newlevel=$1
  22
+  case "$newlevel" in
  23
+		trace)
  24
+			__bunyanLevel=10
  25
+			;;
  26
+		debug)
  27
+			__bunyanLevel=20
  28
+			;;
  29
+		info)
  30
+			__bunyanLevel=30
  31
+			;;
  32
+		warn)
  33
+			__bunyanLevel=40
  34
+			;;
  35
+		error)
  36
+			__bunyanLevel=50
  37
+			;;
  38
+		fatal)
  39
+			__bunyanLevel=60
  40
+			;;
  41
+		*)
  42
+			printf "unknown log level '$1'\n" >&2
  43
+			;;
  44
+	esac
  45
+}
  46
+
19 47
 
20 48
 function trace() { 
21  
-  __bunyan 10 "$@" 
  49
+  [[ __bunyanLevel -ge 10 ]] && __bunyan 10 "$@" 
22 50
 }
23 51
 
24 52
 function debug() { 
25  
-  __bunyan 20 "$@" 
  53
+  [[ __bunyanLevel -ge 20 ]] && __bunyan 20 "$@" 
26 54
 }
27 55
 
28 56
 function info()  { 
29  
-  __bunyan 30 "$@" 
  57
+  [[ __bunyanLevel -ge 30 ]] && __bunyan 30 "$@" 
30 58
 }
31 59
 
32 60
 function warn()  { 
33  
-  __bunyan 40 "$@" 
  61
+  [[ __bunyanLevel -ge 40 ]] && __bunyan 40 "$@" 
34 62
 }
35 63
 
36 64
 function error() { 
37  
-  __bunyan 50 "$@" 
  65
+  [[ __bunyanLevel -ge 50 ]] && __bunyan 50 "$@" 
38 66
 }
39 67
 
40 68
 function fatal() { 
41  
-  __bunyan 60 "$@" 
  69
+  [[ __bunyanLevel -ge 60 ]] && __bunyan 60 "$@" 
42 70
 }

0 notes on commit e80455b

Please sign in to comment.
Something went wrong with that request. Please try again.