forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
/
logger.go
72 lines (64 loc) · 2.13 KB
/
logger.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
package sls
import (
"os"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"gopkg.in/natefinch/lumberjack.v2"
)
var Logger = initDefaultSLSLogger()
func initDefaultSLSLogger() log.Logger {
logFileName := os.Getenv("SLS_GO_SDK_LOG_FILE_NAME")
isJsonType := os.Getenv("SLS_GO_SDK_IS_JSON_TYPE")
logMaxSize := os.Getenv("SLS_GO_SDK_LOG_MAX_SIZE")
logFileBackupCount := os.Getenv("SLS_GO_SDK_LOG_FILE_BACKUP_COUNT")
allowLogLevel := os.Getenv("SLS_GO_SDK_ALLOW_LOG_LEVEL")
return GenerateInnerLogger(logFileName, isJsonType, logMaxSize, logFileBackupCount, allowLogLevel)
}
func GenerateInnerLogger(logFileName, isJsonType, logMaxSize, logFileBackupCount, allowLogLevel string) log.Logger {
var logger log.Logger
if logFileName == "" {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
return logger
} else if logFileName == "stdout" {
if isJsonType == "true" {
logger = log.NewLogfmtLogger(initLogFlusher(logFileBackupCount, logMaxSize, logFileName))
} else {
logger = log.NewJSONLogger(initLogFlusher(logFileBackupCount, logMaxSize, logFileName))
}
} else if logFileName != "stdout" {
if isJsonType == "true" {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
} else {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
}
}
switch allowLogLevel {
case "debug":
logger = level.NewFilter(logger, level.AllowDebug())
case "info":
logger = level.NewFilter(logger, level.AllowInfo())
case "warn":
logger = level.NewFilter(logger, level.AllowWarn())
case "error":
logger = level.NewFilter(logger, level.AllowError())
default:
logger = level.NewFilter(logger, level.AllowInfo())
}
logger = log.With(logger, "time", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return logger
}
func initLogFlusher(logFileBackupCount, logMaxSize, logFileName string) *lumberjack.Logger {
var newLogMaxSize, newLogFileBackupCount int
if logMaxSize == "0" {
newLogMaxSize = 10
}
if logFileBackupCount == "0" {
newLogFileBackupCount = 10
}
return &lumberjack.Logger{
Filename: logFileName,
MaxSize: newLogMaxSize,
MaxBackups: newLogFileBackupCount,
Compress: true,
}
}