Skip to content

Commit

Permalink
use json log formatter if stdout isnt tty (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
terev authored Jul 2, 2020
1 parent 693e210 commit 9a62f30
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
25 changes: 19 additions & 6 deletions common/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package common
import (
"os"

"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
)

Expand All @@ -35,19 +36,31 @@ const (
LabelHTTPMethod = "http-method"
LabelVersion = "version"
LabelTime = "time"
TimestampFormat = "2006-01-02 15:04:05"
)

// NewArgoEventsLogger returns a new ArgoEventsLogger
func NewArgoEventsLogger() *logrus.Logger {
log := &logrus.Logger{
Out: os.Stdout,
Level: logrus.InfoLevel,
Formatter: &logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
var formatter logrus.Formatter

// If out is term use textformatter
if isatty.IsTerminal(os.Stdout.Fd()) {
formatter = &logrus.TextFormatter{
TimestampFormat: TimestampFormat,
FullTimestamp: true,
ForceColors: true,
QuoteEmptyFields: true,
},
}
} else {
formatter = &logrus.JSONFormatter{
TimestampFormat: TimestampFormat,
}
}

log := &logrus.Logger{
Out: os.Stdout,
Level: logrus.InfoLevel,
Formatter: formatter,
}

debugMode, ok := os.LookupEnv(EnvVarDebugLog)
Expand Down
11 changes: 5 additions & 6 deletions controllers/sensor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/argoproj/argo-events/common"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
Expand All @@ -32,7 +31,7 @@ import (

// watchControllerConfigMap watches updates to sensor controller configmap
func (controller *Controller) watchControllerConfigMap() cache.Controller {
log.Info("watching controller config map updates")
controller.logger.Info("watching controller config map updates")
source := controller.newControllerConfigMapWatch()
_, ctrl := cache.NewInformer(
source,
Expand All @@ -41,19 +40,19 @@ func (controller *Controller) watchControllerConfigMap() cache.Controller {
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if cm, ok := obj.(*corev1.ConfigMap); ok {
log.Info("detected configuration update. updating the controller configuration")
controller.logger.Info("detected configuration update. updating the controller configuration")
err := controller.updateConfig(cm)
if err != nil {
log.Errorf("update of controller configuration failed due to: %v", err)
controller.logger.Errorf("update of controller configuration failed due to: %v", err)
}
}
},
UpdateFunc: func(old, new interface{}) {
if newCm, ok := new.(*corev1.ConfigMap); ok {
log.Info("detected configuration update. updating the controller configuration")
controller.logger.Info("detected configuration update. updating the controller configuration")
err := controller.updateConfig(newCm)
if err != nil {
log.Errorf("update of controller configuration failed due to: %v", err)
controller.logger.Errorf("update of controller configuration failed due to: %v", err)
}
}
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mailru/easyjson v0.7.1 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/minio/minio-go v1.0.1-0.20190523192347-c6c2912aa552
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.3.0
Expand Down

0 comments on commit 9a62f30

Please sign in to comment.