diff --git a/cmd/sshpiperd/main.go b/cmd/sshpiperd/main.go index f3a64854..02633f72 100644 --- a/cmd/sshpiperd/main.go +++ b/cmd/sshpiperd/main.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "runtime/debug" + "slices" "time" "github.com/pires/go-proxyproto" @@ -71,6 +72,11 @@ func createCmdPlugin(args []string) (*plugin.CmdPlugin, error) { return p, nil } +func isValidLogFormat(logFormat string) bool { + validFormats := []string{"text", "json"} + return slices.Contains(validFormats, logFormat) +} + func main() { app := &cli.App{ @@ -124,6 +130,12 @@ func main() { Usage: "log level, one of: trace, debug, info, warn, error, fatal, panic", EnvVars: []string{"SSHPIPERD_LOG_LEVEL"}, }, + &cli.StringFlag{ + Name: "log-format", + Value: "text", + Usage: "log format, one of: text, json", + EnvVars: []string{"SSHPIPERD_LOG_FORMAT"}, + }, &cli.StringFlag{ Name: "typescript-log-dir", Value: "", @@ -157,6 +169,14 @@ func main() { log.SetLevel(level) + logFormat := ctx.String("log-format") + if !isValidLogFormat(logFormat) { + return fmt.Errorf("not a valid log-format: %v", logFormat) + } + if logFormat == "json" { + log.SetFormatter(&log.JSONFormatter{}) + } + log.Info("starting sshpiperd version: ", version()) d, err := newDaemon(ctx) diff --git a/libplugin/template.go b/libplugin/template.go index e5477837..4b344d15 100644 --- a/libplugin/template.go +++ b/libplugin/template.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -11,6 +12,7 @@ type PluginTemplate struct { Name string Usage string Flags []cli.Flag + LogFormatter logrus.Formatter CreateConfig func(c *cli.Context) (*SshPiperPluginConfig, error) } @@ -42,7 +44,7 @@ func CreateAndRunPluginTemplate(t *PluginTemplate) { return err } - ConfigStdioLogrus(p, nil) + ConfigStdioLogrus(p, t.LogFormatter, nil) return p.Serve() }, } diff --git a/libplugin/util.go b/libplugin/util.go index c66b5709..7b2d02aa 100644 --- a/libplugin/util.go +++ b/libplugin/util.go @@ -39,7 +39,7 @@ func AuthMethodFromName(n string) AuthMethod { return -1 } -func ConfigStdioLogrus(p SshPiperPlugin, logger *logrus.Logger) { +func ConfigStdioLogrus(p SshPiperPlugin, formatter logrus.Formatter, logger *logrus.Logger) { if logger == nil { logger = logrus.StandardLogger() } @@ -50,7 +50,11 @@ func ConfigStdioLogrus(p SshPiperPlugin, logger *logrus.Logger) { logger.SetLevel(lv) if tty { - logger.SetFormatter(&logrus.TextFormatter{ForceColors: true}) + if formatter != nil { + logger.SetFormatter(formatter) + } else { + logger.SetFormatter(&logrus.TextFormatter{ForceColors: true}) + } } }) }