vendor/github.com/spf13/jwalterweatherman/notepad.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
     6 package jwalterweatherman
     6 package jwalterweatherman
     7 
     7 
     8 import (
     8 import (
     9 	"fmt"
     9 	"fmt"
    10 	"io"
    10 	"io"
       
    11 	"io/ioutil"
    11 	"log"
    12 	"log"
    12 )
    13 )
    13 
    14 
    14 type Threshold int
    15 type Threshold int
    15 
    16 
    56 	logThreshold    Threshold
    57 	logThreshold    Threshold
    57 	stdoutThreshold Threshold
    58 	stdoutThreshold Threshold
    58 	prefix          string
    59 	prefix          string
    59 	flags           int
    60 	flags           int
    60 
    61 
    61 	// One per Threshold
    62 	logListeners []LogListener
    62 	logCounters [7]*logCounter
    63 }
    63 }
    64 
    64 
    65 // A LogListener can ble supplied to a Notepad to listen on log writes for a given
    65 // NewNotepad create a new notepad.
    66 // threshold. This can be used to capture log events in unit tests and similar.
    66 func NewNotepad(outThreshold Threshold, logThreshold Threshold, outHandle, logHandle io.Writer, prefix string, flags int) *Notepad {
    67 // Note that this function will be invoked once for each log threshold. If
    67 	n := &Notepad{}
    68 // the given threshold is not of interest to you, return nil.
       
    69 // Note that these listeners will receive log events for a given threshold, even
       
    70 // if the current configuration says not to log it. That way you can count ERRORs even
       
    71 // if you don't print them to the console.
       
    72 type LogListener func(t Threshold) io.Writer
       
    73 
       
    74 // NewNotepad creates a new Notepad.
       
    75 func NewNotepad(
       
    76 	outThreshold Threshold,
       
    77 	logThreshold Threshold,
       
    78 	outHandle, logHandle io.Writer,
       
    79 	prefix string, flags int,
       
    80 	logListeners ...LogListener,
       
    81 ) *Notepad {
       
    82 
       
    83 	n := &Notepad{logListeners: logListeners}
    68 
    84 
    69 	n.loggers = [7]**log.Logger{&n.TRACE, &n.DEBUG, &n.INFO, &n.WARN, &n.ERROR, &n.CRITICAL, &n.FATAL}
    85 	n.loggers = [7]**log.Logger{&n.TRACE, &n.DEBUG, &n.INFO, &n.WARN, &n.ERROR, &n.CRITICAL, &n.FATAL}
    70 	n.outHandle = outHandle
    86 	n.outHandle = outHandle
    71 	n.logHandle = logHandle
    87 	n.logHandle = logHandle
    72 	n.stdoutThreshold = outThreshold
    88 	n.stdoutThreshold = outThreshold
    93 func (n *Notepad) init() {
   109 func (n *Notepad) init() {
    94 	logAndOut := io.MultiWriter(n.outHandle, n.logHandle)
   110 	logAndOut := io.MultiWriter(n.outHandle, n.logHandle)
    95 
   111 
    96 	for t, logger := range n.loggers {
   112 	for t, logger := range n.loggers {
    97 		threshold := Threshold(t)
   113 		threshold := Threshold(t)
    98 		counter := &logCounter{}
       
    99 		n.logCounters[t] = counter
       
   100 		prefix := n.prefix + threshold.String() + " "
   114 		prefix := n.prefix + threshold.String() + " "
   101 
   115 
   102 		switch {
   116 		switch {
   103 		case threshold >= n.logThreshold && threshold >= n.stdoutThreshold:
   117 		case threshold >= n.logThreshold && threshold >= n.stdoutThreshold:
   104 			*logger = log.New(io.MultiWriter(counter, logAndOut), prefix, n.flags)
   118 			*logger = log.New(n.createLogWriters(threshold, logAndOut), prefix, n.flags)
   105 
   119 
   106 		case threshold >= n.logThreshold:
   120 		case threshold >= n.logThreshold:
   107 			*logger = log.New(io.MultiWriter(counter, n.logHandle), prefix, n.flags)
   121 			*logger = log.New(n.createLogWriters(threshold, n.logHandle), prefix, n.flags)
   108 
   122 
   109 		case threshold >= n.stdoutThreshold:
   123 		case threshold >= n.stdoutThreshold:
   110 			*logger = log.New(io.MultiWriter(counter, n.outHandle), prefix, n.flags)
   124 			*logger = log.New(n.createLogWriters(threshold, n.outHandle), prefix, n.flags)
   111 
   125 
   112 		default:
   126 		default:
   113 			// counter doesn't care about prefix and flags, so don't use them
   127 			*logger = log.New(n.createLogWriters(threshold, ioutil.Discard), prefix, n.flags)
   114 			// for performance.
       
   115 			*logger = log.New(counter, "", 0)
       
   116 		}
   128 		}
   117 	}
   129 	}
       
   130 }
       
   131 
       
   132 func (n *Notepad) createLogWriters(t Threshold, handle io.Writer) io.Writer {
       
   133 	if len(n.logListeners) == 0 {
       
   134 		return handle
       
   135 	}
       
   136 	writers := []io.Writer{handle}
       
   137 	for _, l := range n.logListeners {
       
   138 		w := l(t)
       
   139 		if w != nil {
       
   140 			writers = append(writers, w)
       
   141 		}
       
   142 	}
       
   143 
       
   144 	if len(writers) == 1 {
       
   145 		return handle
       
   146 	}
       
   147 
       
   148 	return io.MultiWriter(writers...)
   118 }
   149 }
   119 
   150 
   120 // SetLogThreshold changes the threshold above which messages are written to the
   151 // SetLogThreshold changes the threshold above which messages are written to the
   121 // log file.
   152 // log file.
   122 func (n *Notepad) SetLogThreshold(threshold Threshold) {
   153 func (n *Notepad) SetLogThreshold(threshold Threshold) {