1 // Copyright © 2016 Steve Francia <spf@spf13.com>.
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
6 package jwalterweatherman
16 func (t Threshold) String() string {
21 LevelTrace Threshold = iota
30 var prefixes map[Threshold]string = map[Threshold]string{
36 LevelCritical: "CRITICAL",
40 // Notepad is where you leave a note!
53 loggers [7]**log.Logger
56 logThreshold Threshold
57 stdoutThreshold Threshold
62 logCounters [7]*logCounter
65 // NewNotepad create a new notepad.
66 func NewNotepad(outThreshold Threshold, logThreshold Threshold, outHandle, logHandle io.Writer, prefix string, flags int) *Notepad {
69 n.loggers = [7]**log.Logger{&n.TRACE, &n.DEBUG, &n.INFO, &n.WARN, &n.ERROR, &n.CRITICAL, &n.FATAL}
70 n.outHandle = outHandle
71 n.logHandle = logHandle
72 n.stdoutThreshold = outThreshold
73 n.logThreshold = logThreshold
76 n.prefix = "[" + prefix + "] "
83 n.LOG = log.New(n.logHandle,
86 n.FEEDBACK = &Feedback{out: log.New(outHandle, "", 0), log: n.LOG}
92 // init creates the loggers for each level depending on the notepad thresholds.
93 func (n *Notepad) init() {
94 logAndOut := io.MultiWriter(n.outHandle, n.logHandle)
96 for t, logger := range n.loggers {
97 threshold := Threshold(t)
98 counter := &logCounter{}
99 n.logCounters[t] = counter
100 prefix := n.prefix + threshold.String() + " "
103 case threshold >= n.logThreshold && threshold >= n.stdoutThreshold:
104 *logger = log.New(io.MultiWriter(counter, logAndOut), prefix, n.flags)
106 case threshold >= n.logThreshold:
107 *logger = log.New(io.MultiWriter(counter, n.logHandle), prefix, n.flags)
109 case threshold >= n.stdoutThreshold:
110 *logger = log.New(io.MultiWriter(counter, n.outHandle), prefix, n.flags)
113 // counter doesn't care about prefix and flags, so don't use them
115 *logger = log.New(counter, "", 0)
120 // SetLogThreshold changes the threshold above which messages are written to the
122 func (n *Notepad) SetLogThreshold(threshold Threshold) {
123 n.logThreshold = threshold
127 // SetLogOutput changes the file where log messages are written.
128 func (n *Notepad) SetLogOutput(handle io.Writer) {
133 // GetStdoutThreshold returns the defined Treshold for the log logger.
134 func (n *Notepad) GetLogThreshold() Threshold {
135 return n.logThreshold
138 // SetStdoutThreshold changes the threshold above which messages are written to the
140 func (n *Notepad) SetStdoutThreshold(threshold Threshold) {
141 n.stdoutThreshold = threshold
145 // GetStdoutThreshold returns the Treshold for the stdout logger.
146 func (n *Notepad) GetStdoutThreshold() Threshold {
147 return n.stdoutThreshold
150 // SetPrefix changes the prefix used by the notepad. Prefixes are displayed between
151 // brackets at the beginning of the line. An empty prefix won't be displayed at all.
152 func (n *Notepad) SetPrefix(prefix string) {
153 if len(prefix) != 0 {
154 n.prefix = "[" + prefix + "] "
161 // SetFlags choose which flags the logger will display (after prefix and message
162 // level). See the package log for more informations on this.
163 func (n *Notepad) SetFlags(flags int) {
168 // Feedback writes plainly to the outHandle while
169 // logging with the standard extra information (date, file, etc).
170 type Feedback struct {
175 func (fb *Feedback) Println(v ...interface{}) {
176 fb.output(fmt.Sprintln(v...))
179 func (fb *Feedback) Printf(format string, v ...interface{}) {
180 fb.output(fmt.Sprintf(format, v...))
183 func (fb *Feedback) Print(v ...interface{}) {
184 fb.output(fmt.Sprint(v...))
187 func (fb *Feedback) output(s string) {