-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezlog.go
51 lines (42 loc) · 971 Bytes
/
ezlog.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
package ezlog
import (
"io"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/nishisuke/ezlog/echologger"
"github.com/rs/zerolog"
)
type (
Config struct {
io.Writer
log.Lvl
TimeFieldFormat string
}
)
var DefaultConfig = Config{
Writer: os.Stdout,
Lvl: log.WARN,
TimeFieldFormat: zerolog.TimeFormatUnix,
}
func Prepare(e *echo.Echo) {
conf := DefaultConfig
zerolog.TimeFieldFormat = conf.TimeFieldFormat
e.Logger = echologger.New(conf.Writer, conf.Lvl)
}
func PrepareWithConfig(e *echo.Echo, conf *Config) {
setDefaultIfZeroValue(conf)
zerolog.TimeFieldFormat = conf.TimeFieldFormat
e.Logger = echologger.New(conf.Writer, conf.Lvl)
}
func setDefaultIfZeroValue(conf *Config) {
if conf.Writer == nil {
conf.Writer = DefaultConfig.Writer
}
if conf.Lvl == 0 {
conf.Lvl = DefaultConfig.Lvl
}
if conf.TimeFieldFormat == "" {
conf.TimeFieldFormat = DefaultConfig.TimeFieldFormat
}
}