-
Notifications
You must be signed in to change notification settings - Fork 25
/
handler.go
117 lines (98 loc) · 2.66 KB
/
handler.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
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
113
114
115
116
117
// Package handler provide useful common log handlers.
//
// eg: file, console, multi_file, rotate_file, stream, syslog, email
package handler
import (
"io"
"os"
"sync"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/slog"
)
// DefaultBufferSize sizes the buffer associated with each log file. It's large
// so that log records can accumulate without the logging thread blocking
// on disk I/O. The flushDaemon will block instead.
var DefaultBufferSize = 8 * 1024
var (
// DefaultFilePerm perm and flags for create log file
DefaultFilePerm os.FileMode = 0664
// DefaultFileFlags for create/open file
DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND
)
// FlushWriter is the interface satisfied by logging destinations.
type FlushWriter interface {
Flush() error
// Writer the output writer
io.Writer
}
// FlushCloseWriter is the interface satisfied by logging destinations.
type FlushCloseWriter interface {
Flush() error
// WriteCloser the output writer
io.WriteCloser
}
// SyncCloseWriter is the interface satisfied by logging destinations.
// such as os.File
type SyncCloseWriter interface {
Sync() error
// WriteCloser the output writer
io.WriteCloser
}
/********************************************************************************
* Common parts for handler
********************************************************************************/
// LevelWithFormatter struct definition
//
// - support set log formatter
// - only support set one log level
//
// Deprecated: please use slog.LevelWithFormatter instead.
type LevelWithFormatter = slog.LevelWithFormatter
// LevelsWithFormatter struct definition
//
// - support set log formatter
// - support setting multi log levels
//
// Deprecated: please use slog.LevelsWithFormatter instead.
type LevelsWithFormatter = slog.LevelsWithFormatter
// NopFlushClose no operation.
//
// provide empty Flush(), Close() methods, useful for tests.
type NopFlushClose struct{}
// Flush logs to disk
func (h *NopFlushClose) Flush() error {
return nil
}
// Close handler
func (h *NopFlushClose) Close() error {
return nil
}
// LockWrapper struct
type LockWrapper struct {
sync.Mutex
disable bool
}
// Lock it
func (lw *LockWrapper) Lock() {
if !lw.disable {
lw.Mutex.Lock()
}
}
// Unlock it
func (lw *LockWrapper) Unlock() {
if !lw.disable {
lw.Mutex.Unlock()
}
}
// EnableLock enable lock
func (lw *LockWrapper) EnableLock(enable bool) {
lw.disable = !enable
}
// LockEnabled status
func (lw *LockWrapper) LockEnabled() bool {
return !lw.disable
}
// QuickOpenFile like os.OpenFile
func QuickOpenFile(filepath string) (*os.File, error) {
return fsutil.OpenFile(filepath, DefaultFileFlags, DefaultFilePerm)
}