-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
making the ECS init log level configurable #4097
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 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" | ||
|
||
conf "github.com/aws/amazon-ecs-agent/ecs-init/config" | ||
"github.com/cihub/seelog" | ||
) | ||
|
||
const ( | ||
LOGLEVEL_ENV_VAR = "ECS_LOGLEVEL" | ||
defaultLogLevel = "info" | ||
outputFmt = "logfmt" | ||
rollCount = 24 | ||
) | ||
|
||
// logLevels is the mapping from ECS_INIT_LOGLEVEL to Seelog provided levels. | ||
var logLevels = map[string]string{ | ||
"debug": "debug", | ||
"info": "info", | ||
"warn": "warn", | ||
"error": "error", | ||
"crit": "critical", | ||
"none": "off", | ||
} | ||
|
||
type logConfig struct { | ||
level string | ||
outputFormat string | ||
maxRollCount int | ||
lock sync.Mutex | ||
} | ||
|
||
// config contains config for seelog logger | ||
var config *logConfig | ||
|
||
func init() { | ||
config = &logConfig{ | ||
level: defaultLogLevel, | ||
outputFormat: outputFmt, | ||
maxRollCount: rollCount, | ||
} | ||
} | ||
|
||
// Setup sets the custom logging config | ||
func Setup() { | ||
if logLevel := os.Getenv(LOGLEVEL_ENV_VAR); logLevel != "" { | ||
SetLogLevel(logLevel) | ||
} | ||
if err := seelog.RegisterCustomFormatter("InitLogfmt", logfmtFormatter); err != nil { | ||
seelog.Error(err) | ||
} | ||
reloadConfig() | ||
} | ||
|
||
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 | ||
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message) | ||
} | ||
} | ||
|
||
func SetLogLevel(logLevel string) { | ||
parsedLevel, ok := logLevels[strings.ToLower(logLevel)] | ||
if ok { | ||
config.lock.Lock() | ||
defer config.lock.Unlock() | ||
config.level = parsedLevel | ||
reloadConfig() | ||
} else { | ||
seelog.Error("log level mapping not found") | ||
} | ||
} | ||
|
||
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 />` | ||
if conf.InitLogFile() != "" { | ||
c += ` | ||
<rollingfile filename="` + conf.InitLogFile() + `" type="date" | ||
datepattern="2006-01-02-15" archivetype="none" maxrolls="` + strconv.Itoa(config.maxRollCount) + `" />` | ||
} | ||
c += ` | ||
</outputs> | ||
<formats> | ||
<format id="logfmt" format="%InitLogfmt" /> | ||
</formats> | ||
</seelog>` | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 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 ( | ||
"testing" | ||
"time" | ||
|
||
conf "github.com/aws/amazon-ecs-agent/ecs-init/config" | ||
"github.com/cihub/seelog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogfmtFormat(t *testing.T) { | ||
logfmt := logfmtFormatter("") | ||
out := logfmt("This is my log message", seelog.InfoLvl, &LogContextMock{}) | ||
s, ok := out.(string) | ||
assert.True(t, ok) | ||
assert.Equal(t, `level=info time=2018-10-01T01:02:03Z msg="This is my log message" | ||
`, s) | ||
} | ||
|
||
func TestSeelogConfig(t *testing.T) { | ||
config = &logConfig{ | ||
level: defaultLogLevel, | ||
outputFormat: outputFmt, | ||
maxRollCount: 24, | ||
} | ||
c := seelogConfig() | ||
assert.Equal(t, ` | ||
<seelog type="asyncloop" minlevel="info"> | ||
<outputs formatid="logfmt"> | ||
<console /> | ||
<rollingfile filename="`+conf.InitLogFile()+`" type="date" | ||
datepattern="2006-01-02-15" archivetype="none" maxrolls="24" /> | ||
</outputs> | ||
<formats> | ||
<format id="logfmt" format="%InitLogfmt" /> | ||
</formats> | ||
</seelog>`, c) | ||
} | ||
|
||
type LogContextMock struct{} | ||
|
||
// Caller's function name. | ||
func (l *LogContextMock) Func() string { | ||
return "" | ||
} | ||
|
||
// Caller's line number. | ||
func (l *LogContextMock) Line() int { | ||
return 0 | ||
} | ||
|
||
// Caller's file short path (in slashed form). | ||
func (l *LogContextMock) ShortPath() string { | ||
return "" | ||
} | ||
|
||
// Caller's file full path (in slashed form). | ||
func (l *LogContextMock) FullPath() string { | ||
return "" | ||
} | ||
|
||
// True if the context is correct and may be used. | ||
// If false, then an error in context evaluation occurred and | ||
// all its other data may be corrupted. | ||
func (l *LogContextMock) IsValid() bool { | ||
return true | ||
} | ||
|
||
// Time when log function was called. | ||
func (l *LogContextMock) CallTime() time.Time { | ||
return time.Date(2018, time.October, 1, 1, 2, 3, 0, time.UTC) | ||
} | ||
|
||
// Custom context that can be set by calling logger.SetContext | ||
func (l *LogContextMock) CustomContext() interface{} { | ||
return map[string]string{} | ||
} | ||
|
||
// Caller's file name (without path). | ||
func (l *LogContextMock) FileName() string { | ||
return "mytestmodule.go" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe standard library imports can be imported together as a block first followed by other imports, following go styleguide
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it thanks!