Skip to content
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 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ Additionally, the following environment variable(s) can be used to configure the
| `ECS_AGENT_APPARMOR_PROFILE` | `unconfined` | Specifies the name of the AppArmor profile to run the ecs-agent container under. This only applies to AppArmor-enabled systems, such as Ubuntu, Debian, and SUSE. If unset, defaults to the profile written out by ecs-init (ecs-agent-default). | `ecs-agent-default` |



### Persistence

When you run the Amazon ECS Container Agent in production, its `datadir` should be persisted between runs of the Docker
Expand Down
2 changes: 1 addition & 1 deletion ecs-init/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func LogDirectory() string {
return directoryPrefix + "/var/log/ecs"
}

func initLogFile() string {
func InitLogFile() string {
return LogDirectory() + "/ecs-init.log"
}

Expand Down
46 changes: 0 additions & 46 deletions ecs-init/config/logger.go

This file was deleted.

11 changes: 3 additions & 8 deletions ecs-init/ecs-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"
"os"

"github.com/aws/amazon-ecs-agent/ecs-init/config"
"github.com/aws/amazon-ecs-agent/ecs-init/engine"
"github.com/aws/amazon-ecs-agent/ecs-init/logger"
"github.com/aws/amazon-ecs-agent/ecs-init/version"

log "github.com/cihub/seelog"
Expand All @@ -37,7 +37,6 @@ const (
)

func main() {
defer log.Flush()
flag.Parse()
args := flag.Args()

Expand All @@ -46,12 +45,8 @@ func main() {
os.Exit(1)
}

logger, err := log.LoggerFromConfigAsString(config.Logger())
if err != nil {
die(err, engine.DefaultInitErrorExitCode)
}
log.ReplaceLogger(logger)

logger.Setup()
defer log.Flush()
if args[0] == VERSION {
err := version.PrintVersion()
if err != nil {
Expand Down
119 changes: 119 additions & 0 deletions ecs-init/logger/log.go
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"
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it thanks!

"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
}
96 changes: 96 additions & 0 deletions ecs-init/logger/log_test.go
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"
}
41 changes: 0 additions & 41 deletions scripts/bundle_log_config.sh

This file was deleted.

2 changes: 1 addition & 1 deletion seelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and limitations under the License.
<seelog type="asyncloop">
<outputs formatid="main">
<console formatid="console" />
<rollingfile filename="` + initLogFile() + `" type="date"
<rollingfile filename="` + InitLogFile() + `" type="date"
datepattern="2006-01-02-15" archivetype="zip" maxrolls="5" />
</outputs>
<formats>
Expand Down
Loading