aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/lunny/log/filewriter.go
blob: f0bb4d1df1b208559ca2a9e68a35a8b367b4cc23 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package log

import (
	"io"
	"os"
	"path/filepath"
	"sync"
	"time"
)

var _ io.Writer = &Files{}

type ByType int

const (
	ByDay ByType = iota
	ByHour
	ByMonth
)

var (
	formats = map[ByType]string{
		ByDay:   "2006-01-02",
		ByHour:  "2006-01-02-15",
		ByMonth: "2006-01",
	}
)

func SetFileFormat(t ByType, format string) {
	formats[t] = format
}

func (b ByType) Format() string {
	return formats[b]
}

type Files struct {
	FileOptions
	f          *os.File
	lastFormat string
	lock       sync.Mutex
}

type FileOptions struct {
	Dir    string
	ByType ByType
	Loc    *time.Location
}

func prepareFileOption(opts []FileOptions) FileOptions {
	var opt FileOptions
	if len(opts) > 0 {
		opt = opts[0]
	}
	if opt.Dir == "" {
		opt.Dir = "./"
	}
	err := os.MkdirAll(opt.Dir, os.ModePerm)
	if err != nil {
		panic(err.Error())
	}

	if opt.Loc == nil {
		opt.Loc = time.Local
	}
	return opt
}

func NewFileWriter(opts ...FileOptions) *Files {
	opt := prepareFileOption(opts)
	return &Files{
		FileOptions: opt,
	}
}

func (f *Files) getFile() (*os.File, error) {
	var err error
	t := time.Now().In(f.Loc)
	if f.f == nil {
		f.lastFormat = t.Format(f.ByType.Format())
		f.f, err = os.OpenFile(filepath.Join(f.Dir, f.lastFormat+".log"),
			os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
		return f.f, err
	}
	if f.lastFormat != t.Format(f.ByType.Format()) {
		f.f.Close()
		f.lastFormat = t.Format(f.ByType.Format())
		f.f, err = os.OpenFile(filepath.Join(f.Dir, f.lastFormat+".log"),
			os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
		return f.f, err
	}
	return f.f, nil
}

func (f *Files) Write(bs []byte) (int, error) {
	f.lock.Lock()
	defer f.lock.Unlock()

	w, err := f.getFile()
	if err != nil {
		return 0, err
	}
	return w.Write(bs)
}

func (f *Files) Close() {
	if f.f != nil {
		f.f.Close()
		f.f = nil
	}
	f.lastFormat = ""
}