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

feat: Use json log formatter if stdout isnt tty #699

Merged
merged 1 commit into from
Jul 2, 2020
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
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 @@ -59,6 +59,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