Skip to content
Open
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
4 changes: 3 additions & 1 deletion cronexpr/cronexpr/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/aptible/supercronic/cronexpr/cronexpr

go 1.18
go 1.21.4

toolchain go1.22.2

replace github.com/aptible/supercronic => ../../

Expand Down
48 changes: 48 additions & 0 deletions log/formatter/custom_field_formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package formatter

import (
"github.com/sirupsen/logrus"
"regexp"
"strings"
"time"
)

type CustomFieldFormatter struct {
LogFormat string
}

func (f *CustomFieldFormatter) getFieldValue(entry *logrus.Entry, field string) (string, bool) {
switch strings.ToLower(field) {
case "level":
return entry.Level.String(), true
case "time":
return entry.Time.Format(time.RFC3339Nano), true
case "message":
return entry.Message, true
default:
val, ok := entry.Data[field]

if ok {
return val.(string), true
}

return "", false
}
}

func (f *CustomFieldFormatter) Format(entry *logrus.Entry) ([]byte, error) {
re := regexp.MustCompile(`%[\w.]+`)

replaced := re.ReplaceAllStringFunc(f.LogFormat, func(match string) string {
// Remove the $ prefix to get the key for valuesMap
key := strings.TrimPrefix(match, "%")
// If the key exists in the entry, replace with the value from the map
if value, ok := f.getFieldValue(entry, key); ok {
return value
}

return ""
})

return []byte(strings.TrimSpace(replaced) + "\n"), nil
}
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/aptible/supercronic/cron"
"github.com/aptible/supercronic/crontab"
"github.com/aptible/supercronic/log/formatter"
"github.com/aptible/supercronic/log/hook"
"github.com/aptible/supercronic/prometheus_metrics"
"github.com/evalphobia/logrus_sentry"
Expand All @@ -37,6 +38,7 @@ func main() {
prometheus_metrics.DefaultPort,
),
)
customLogFormat := flag.String("log-format", "", "custom log format. Available fields are %level %time %message %job.command %job.schedule %job.position")
splitLogs := flag.Bool("split-logs", false, "split log output into stdout/stderr")
passthroughLogs := flag.Bool("passthrough-logs", false, "passthrough logs from commands, do not wrap them in Supercronic logging")
sentry := flag.String("sentry-dsn", "", "enable Sentry error logging, using provided DSN")
Expand Down Expand Up @@ -80,11 +82,16 @@ func main() {
logrus.SetLevel(logrus.WarnLevel)
}

if *json {
if *customLogFormat != "" {
customFormatter := new(formatter.CustomFieldFormatter)
customFormatter.LogFormat = *customLogFormat
logrus.SetFormatter(customFormatter)
} else if *json {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}

if *splitLogs {
hook.RegisterSplitLogger(
logrus.StandardLogger(),
Expand Down