forked from facebookarchive/flashback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
73 lines (62 loc) · 1.54 KB
/
logger.go
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
69
70
71
72
73
package flashback
import (
"log"
"os"
)
// Logger provides a way to send different types of log messages to stderr/stdout
type Logger struct {
stderr *log.Logger
stdout *log.Logger
toClose []closeable
}
type closeable interface {
Close() error
}
// NewLogger creates a new logger
func NewLogger(stdout string, stderr string) (logger *Logger, err error) {
var (
stderrWriter = os.Stderr
stdoutWriter = os.Stdout
toClose []closeable
)
if stderr != "" {
if stderrWriter, err = os.OpenFile(stderr, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
return
}
toClose = append(toClose, stderrWriter)
}
if stdout != "" {
if stdoutWriter, err = os.OpenFile(stdout, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
return
}
toClose = append(toClose, stderrWriter)
}
logger = &Logger{
stderr: log.New(stderrWriter, "ERROR ", log.LstdFlags|log.Lshortfile),
stdout: log.New(stdoutWriter, "INFO ", log.LstdFlags|log.Lshortfile),
toClose: toClose,
}
return
}
// Info prints message to stdout
func (l *Logger) Info(v ...interface{}) {
l.stdout.Print(v...)
}
// Infof prints message to stdout
func (l *Logger) Infof(format string, v ...interface{}) {
l.stdout.Printf(format, v...)
}
// Error prints message to stderr
func (l *Logger) Error(v ...interface{}) {
l.stderr.Print(v...)
}
// Errorf prints message to stderr
func (l *Logger) Errorf(format string, v ...interface{}) {
l.stderr.Printf(format, v...)
}
// Close the underlying files
func (l *Logger) Close() {
for _, c := range l.toClose {
c.Close()
}
}