Log to stdout instead of stderr

Signed-off-by: Andy Goldstein <andy.goldstein@gmail.com>
pull/553/head
Andy Goldstein 2018-06-14 14:02:12 -04:00
parent 7be81fe60e
commit cc9140b3cc
4 changed files with 72 additions and 0 deletions

View File

@ -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())

View File

@ -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.

View File

@ -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() {

View File

@ -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])
}
}