-
Notifications
You must be signed in to change notification settings - Fork 602
/
log.go
176 lines (157 loc) · 4.85 KB
/
log.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 (
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/cihub/seelog"
)
const (
LOGLEVEL_ENV_VAR = "ECS_LOGLEVEL"
LOGFILE_ENV_VAR = "ECS_LOGFILE"
LOG_ROLLOVER_TYPE_ENV_VAR = "ECS_LOG_ROLLOVER_TYPE"
LOG_OUTPUT_FORMAT_ENV_VAR = "ECS_LOG_OUTPUT_FORMAT"
LOG_MAX_FILE_SIZE_ENV_VAR = "ECS_LOG_MAX_FILE_SIZE_MB"
LOG_MAX_ROLL_COUNT_ENV_VAR = "ECS_LOG_MAX_ROLL_COUNT"
DEFAULT_LOGLEVEL = "info"
DEFAULT_ROLLOVER_TYPE = "date"
DEFAULT_OUTPUT_FORMAT = "logfmt"
DEFAULT_MAX_FILE_SIZE float64 = 10
DEFAULT_MAX_ROLL_COUNT int = 24
)
type logConfig struct {
RolloverType string
MaxRollCount int
MaxFileSizeMB float64
logfile string
level string
outputFormat string
lock sync.Mutex
}
var Config *logConfig
func logfmtFormatter(params string) seelog.FormatterFunc {
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} {
return fmt.Sprintf(`level=%s time=%s msg=%q module=%s
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName())
}
}
func jsonFormatter(params string) seelog.FormatterFunc {
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} {
return fmt.Sprintf(`{"level": %q, "time": %q, "msg": %q, "module": %q}
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName())
}
}
func reloadConfig() {
logger, err := seelog.LoggerFromConfigAsString(seelogConfig())
if err == nil {
seelog.ReplaceLogger(logger)
} else {
seelog.Error(err)
}
}
func seelogConfig() string {
c := `
<seelog type="asyncloop" minlevel="` + Config.level + `">
<outputs formatid="` + Config.outputFormat + `">
<console />`
c += platformLogConfig()
if Config.logfile != "" {
if Config.RolloverType == "size" {
c += `
<rollingfile filename="` + Config.logfile + `" type="size"
maxsize="` + strconv.Itoa(int(Config.MaxFileSizeMB*1000000)) + `" archivetype="none" maxrolls="` + strconv.Itoa(Config.MaxRollCount) + `" />`
} else {
c += `
<rollingfile filename="` + Config.logfile + `" type="date"
datepattern="2006-01-02-15" archivetype="none" maxrolls="` + strconv.Itoa(Config.MaxRollCount) + `" />`
}
}
c += `
</outputs>
<formats>
<format id="logfmt" format="%EcsAgentLogfmt" />
<format id="json" format="%EcsAgentJson" />
<format id="windows" format="%Msg" />
</formats>
</seelog>`
return c
}
// SetLevel sets the log level for logging
func SetLevel(logLevel string) {
levels := map[string]string{
"debug": "debug",
"info": "info",
"warn": "warn",
"error": "error",
"crit": "critical",
"none": "off",
}
parsedLevel, ok := levels[strings.ToLower(logLevel)]
if ok {
Config.lock.Lock()
defer Config.lock.Unlock()
Config.level = parsedLevel
reloadConfig()
}
}
// GetLevel gets the log level
func GetLevel() string {
Config.lock.Lock()
defer Config.lock.Unlock()
return Config.level
}
func init() {
Config = &logConfig{
logfile: os.Getenv(LOGFILE_ENV_VAR),
level: DEFAULT_LOGLEVEL,
RolloverType: DEFAULT_ROLLOVER_TYPE,
outputFormat: DEFAULT_OUTPUT_FORMAT,
MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE,
MaxRollCount: DEFAULT_MAX_ROLL_COUNT,
}
SetLevel(os.Getenv(LOGLEVEL_ENV_VAR))
if RolloverType := os.Getenv(LOG_ROLLOVER_TYPE_ENV_VAR); RolloverType != "" {
Config.RolloverType = RolloverType
}
if outputFormat := os.Getenv(LOG_OUTPUT_FORMAT_ENV_VAR); outputFormat != "" {
Config.outputFormat = outputFormat
}
if MaxRollCount := os.Getenv(LOG_MAX_ROLL_COUNT_ENV_VAR); MaxRollCount != "" {
i, err := strconv.Atoi(MaxRollCount)
if err == nil {
Config.MaxRollCount = i
} else {
seelog.Error("Invalid value for "+LOG_MAX_ROLL_COUNT_ENV_VAR, err)
}
}
if MaxFileSizeMB := os.Getenv(LOG_MAX_FILE_SIZE_ENV_VAR); MaxFileSizeMB != "" {
f, err := strconv.ParseFloat(MaxFileSizeMB, 64)
if err == nil {
Config.MaxFileSizeMB = f
} else {
seelog.Error("Invalid value for "+LOG_MAX_FILE_SIZE_ENV_VAR, err)
}
}
if err := seelog.RegisterCustomFormatter("EcsAgentLogfmt", logfmtFormatter); err != nil {
seelog.Error(err)
}
if err := seelog.RegisterCustomFormatter("EcsAgentJson", jsonFormatter); err != nil {
seelog.Error(err)
}
registerPlatformLogger()
reloadConfig()
}