Make the log formatter configurable (#371)

* Make the log formatter configurable

The change lets us configure the deamon and the plugins to output logs
in the JSON format, which is easier to parse/filter by log processing
tools.

* Add validation for the log-format flag
This commit is contained in:
Matej Trop 2024-05-08 23:11:18 +01:00 committed by GitHub
parent 7c44c2fee1
commit 73169bbace
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 3 deletions

View file

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