-
Notifications
You must be signed in to change notification settings - Fork 31
/
fileWriter.go
44 lines (37 loc) · 981 Bytes
/
fileWriter.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
// Copyright (c) 2015, huangjunwei <[email protected]>. All rights reserved.
package blog4go
import (
"fmt"
"path"
"strings"
"sync"
)
// NewFileWriter initialize a file writer
// baseDir must be base directory of log files
// rotate determine if it will logrotate
func NewFileWriter(baseDir string, rotate bool) (err error) {
singltonLock.Lock()
defer singltonLock.Unlock()
if nil != blog {
return ErrAlreadyInit
}
fileWriter := new(MultiWriter)
fileWriter.lock = new(sync.RWMutex)
fileWriter.level = DEBUG
fileWriter.closed = false
fileWriter.writers = make(map[LevelType]Writer)
for _, level := range Levels {
fileName := fmt.Sprintf("%s.log", strings.ToLower(level.String()))
writer, err := newBaseFileWriter(path.Join(baseDir, fileName), rotate)
if nil != err {
return err
}
fileWriter.writers[level] = writer
}
// log hook
fileWriter.hook = nil
fileWriter.hookLevel = DEBUG
fileWriter.hookAsync = true
blog = fileWriter
return
}