From 294d466e51f03705bfbad43303bae6e30bec745e Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Fri, 18 Sep 2020 16:23:11 -0700 Subject: [PATCH] Explicitly initialize seelog The logger package's initialization logic is init() which will be executed by loading the package. However some of our programs are using the agent as a library and haven't configured seelog beforehand. Due to that, loading the package logs ``` 1600397429848419666 [Error] node must have children ``` which breaks the programs. I think it would be safer to do less on init() and explicitly initialize the logger from the agent's main(). Signed-off-by: Kazuyoshi Kato --- agent/agent.go | 2 ++ agent/logger/log.go | 2 ++ agent/logger/log_init_test.go | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 agent/logger/log_init_test.go diff --git a/agent/agent.go b/agent/agent.go index 04285fb2f3c..0444729fe1f 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -19,6 +19,7 @@ import ( "time" "github.com/aws/amazon-ecs-agent/agent/app" + "github.com/aws/amazon-ecs-agent/agent/logger" ) func init() { @@ -26,5 +27,6 @@ func init() { } func main() { + logger.InitSeelog() os.Exit(app.Run(os.Args[1:])) } diff --git a/agent/logger/log.go b/agent/logger/log.go index b1feaeb583b..8aa3f6ffa46 100644 --- a/agent/logger/log.go +++ b/agent/logger/log.go @@ -181,7 +181,9 @@ func init() { MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE, MaxRollCount: DEFAULT_MAX_ROLL_COUNT, } +} +func InitSeelog() { if err := seelog.RegisterCustomFormatter("EcsAgentLogfmt", logfmtFormatter); err != nil { seelog.Error(err) } diff --git a/agent/logger/log_init_test.go b/agent/logger/log_init_test.go new file mode 100644 index 00000000000..dc5e6187100 --- /dev/null +++ b/agent/logger/log_init_test.go @@ -0,0 +1,23 @@ +// +build unit integration + +// 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 + +func init() { + // We don't want to call InitSeelog automatically (e.g. calling it from init()) + // because some internal clients use ECS Agent as a library. + // However we want to call InitSeelog automatically from all tests. + InitSeelog() +}