diff --git a/pkg/cmd/server/server.go b/pkg/cmd/server/server.go index 872a3a77f..4caa57008 100644 --- a/pkg/cmd/server/server.go +++ b/pkg/cmd/server/server.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "net/http" "os" "reflect" @@ -85,9 +86,16 @@ func NewCommand() *cobra.Command { Short: "Run the ark server", Long: "Run the ark server", Run: func(c *cobra.Command, args []string) { + // go-plugin uses log.Println to log when it's waiting for all plugin processes to complete so we need to + // set its output to stdout. + log.SetOutput(os.Stdout) + logLevel := logLevelFlag.Parse() + // Make sure we log to stdout so cloud log dashboards don't show this as an error. + logrus.SetOutput(os.Stdout) logrus.Infof("setting log-level to %s", strings.ToUpper(logLevel.String())) + // Ark's DefaultLogger logs to stdout, so all is good there. logger := logging.DefaultLogger(logLevel) logger.Infof("Starting Ark server %s", buildinfo.FormattedGitSHA()) diff --git a/pkg/plugin/logger.go b/pkg/plugin/logger.go index 575a0e223..26b26a6ff 100644 --- a/pkg/plugin/logger.go +++ b/pkg/plugin/logger.go @@ -26,6 +26,13 @@ import ( // Ark plugin. func NewLogger() logrus.FieldLogger { logger := logrus.New() + /* + !!!DO NOT SET THE OUTPUT TO STDOUT!!! + + go-plugin uses stdout for a communications protocol between client and server. + + stderr is used for log messages from server to client. The ark server makes sure they are logged to stdout. + */ // we use the JSON formatter because go-plugin will parse incoming // JSON on stderr and use it to create structured log entries. diff --git a/pkg/util/logging/default_logger.go b/pkg/util/logging/default_logger.go index 3acf24bc4..3574ce1e2 100644 --- a/pkg/util/logging/default_logger.go +++ b/pkg/util/logging/default_logger.go @@ -1,6 +1,24 @@ +/* +Copyright 2018 the Heptio Ark contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License 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 logging import ( + "os" + "github.com/sirupsen/logrus" ) @@ -17,6 +35,10 @@ func DefaultHooks() []logrus.Hook { // and hooks. func DefaultLogger(level logrus.Level) *logrus.Logger { logger := logrus.New() + + // Make sure the output is set to stdout so log messages don't show up as errors in cloud log dashboards. + logger.Out = os.Stdout + logger.Level = level for _, hook := range DefaultHooks() { diff --git a/pkg/util/logging/default_logger_test.go b/pkg/util/logging/default_logger_test.go new file mode 100644 index 000000000..46a366e1c --- /dev/null +++ b/pkg/util/logging/default_logger_test.go @@ -0,0 +1,35 @@ +/* +Copyright 2018 the Heptio Ark contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License 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 logging + +import ( + "os" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestDefaultLogger(t *testing.T) { + logger := DefaultLogger(logrus.InfoLevel) + assert.Equal(t, logrus.InfoLevel, logger.Level) + assert.Equal(t, os.Stdout, logger.Out) + + for _, level := range logrus.AllLevels { + assert.Equal(t, DefaultHooks(), logger.Hooks[level]) + } +}