-
Notifications
You must be signed in to change notification settings - Fork 282
/
setting.go
153 lines (140 loc) · 4.13 KB
/
setting.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Licensed to LinDB under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. LinDB licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package logger
import (
"os"
"path/filepath"
"sync/atomic"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
"github.com/lindb/lindb/config"
)
var (
// IsCli represents if lin-cli command-line.
IsCli = false
isTerminal = IsTerminal(os.Stdout)
// max length of all modules
maxModuleNameLen uint32
lindLogger atomic.Value
accessLogger atomic.Value
slowSQLLogger atomic.Value
// uninitialized logger for default usage
defaultLogger = newDefaultLogger()
// RunningAtomicLevel supports changing level on the fly
RunningAtomicLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
)
func init() {
// get log level from evn
level := os.Getenv("LOG_LEVEL")
initLogLevel(level)
}
func initLogLevel(level string) {
if level != "" {
var zapLevel zapcore.Level
if err := zapLevel.Set(level); err == nil {
RunningAtomicLevel.SetLevel(zapLevel)
}
}
}
const (
AccessLogFileName = "access.log"
SlowSQLLogFileName = "show_sql.log"
)
func IsDebug() bool {
return RunningAtomicLevel.Level() == zapcore.DebugLevel
}
// GetLogger return logger with module name
func GetLogger(module, role string) *Logger {
length := len(module)
for {
currentMaxModuleLen := atomic.LoadUint32(&maxModuleNameLen)
if uint32(length) <= currentMaxModuleLen {
break
}
if atomic.CompareAndSwapUint32(&maxModuleNameLen, currentMaxModuleLen, uint32(length)) {
break
}
}
return &Logger{
module: module,
role: role,
}
}
// newDefaultLogger creates a default logger for uninitialized usage
func newDefaultLogger() *zap.Logger {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = SimpleTimeEncoder
encoderConfig.EncodeLevel = SimpleLevelEncoder
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
os.Stdout,
RunningAtomicLevel)
return zap.New(core, zap.AddCaller(), zap.AddCallerSkip(2))
}
// InitLogger initializes a zap logger from user config
func InitLogger(cfg config.Logging, fileName string) error {
if err := initLogger(fileName, cfg); err != nil {
return err
}
return nil
}
// initLogger initializes a zap logger for different module
func initLogger(logFilename string, cfg config.Logging) error {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: filepath.Join(cfg.Dir, logFilename),
MaxSize: int(cfg.MaxSize / 1024 / 1024), // because in lumberjack will * megabyte
MaxBackups: int(cfg.MaxBackups),
MaxAge: int(cfg.MaxAge),
})
// check if it is terminal
if !IsCli && isTerminal {
w = os.Stdout
}
// parse logging level
if err := RunningAtomicLevel.UnmarshalText([]byte(cfg.Level)); err != nil {
return err
}
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = SimpleTimeEncoder
switch logFilename {
case AccessLogFileName:
encoderConfig.EncodeLevel = SimpleAccessLevelEncoder
default:
encoderConfig.EncodeLevel = SimpleLevelEncoder
}
// check format
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
w,
RunningAtomicLevel)
switch logFilename {
case SlowSQLLogFileName:
encoderConfig.LevelKey = ""
encoderConfig.TimeKey = ""
core = zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
w,
RunningAtomicLevel)
slowSQLLogger.Store(zap.New(core))
case AccessLogFileName:
accessLogger.Store(zap.New(core))
default:
lindLogger.Store(zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1)))
}
return nil
}