forked from alecthomas/log4go
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtermlog.go
More file actions
68 lines (57 loc) · 1.66 KB
/
termlog.go
File metadata and controls
68 lines (57 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2010, Kyle Lemons <kyle@kylelemons.net>. All rights reserved.
package log4go
import (
"fmt"
"io"
"os"
)
var stdout io.Writer = os.Stdout
var isColorful = (os.Getenv("TERM") != "" && os.Getenv("TERM") != "dumb") ||
os.Getenv("ConEmuANSI") == "ON"
// 0, Black; 1, Red; 2, Green; 3, Yellow; 4, Blue; 5, Purple; 6, Cyan; 7, White
var ColorBytes = [...][]byte{
[]byte("\x1b[0;34m"), // FINEST, Blue
[]byte("\x1b[0;36m"), // FINE, Cyan
[]byte("\x1b[0;32m"), // DEBUG, Green
[]byte("\x1b[0;35m"), // TRACE, Purple
nil, // INFO, Default
[]byte("\x1b[1;33m"), // WARNING, Yellow
[]byte("\x1b[0;31m"), // ERROR, Red
[]byte("\x1b[0;31m;47m"), // CRITICAL, Red - White
}
var ColorReset = []byte("\x1b[0m")
// This is the standard writer that prints to standard output.
type ConsoleLogWriter struct {
out io.Writer
color bool
format string
}
// This creates a new ConsoleLogWriter
func NewConsoleLogWriter() *ConsoleLogWriter {
c := &ConsoleLogWriter{
out: stdout,
color: false,
format: "[%T %D %Z] [%L] (%S) %M",
}
return c
}
// Must be called before the first log message is written.
func (c *ConsoleLogWriter) SetColor(color bool) *ConsoleLogWriter {
c.color = color
return c
}
// Set the logging format (chainable). Must be called before the first log
// message is written.
func (c *ConsoleLogWriter) SetFormat(format string) *ConsoleLogWriter {
c.format = format
return c
}
func (c *ConsoleLogWriter) Close() {
}
func (c *ConsoleLogWriter) LogWrite(rec *LogRecord) {
if c.color {
c.out.Write(ColorBytes[rec.Level])
defer c.out.Write(ColorReset)
}
fmt.Fprint(c.out, FormatLogRecord(c.format, rec))
}