cmd/osbuild-composer: journald support

This commit is contained in:
Lukas Zapletal 2024-04-04 09:00:23 +02:00 committed by Achilleas Koutsou
parent e68449404f
commit f3c0daebbf
7 changed files with 345 additions and 3 deletions

View file

@ -129,7 +129,7 @@ func GetDefaultConfig() *ComposerConfigFile {
"rhel-10": "rhel-10.0",
},
LogLevel: "info",
LogFormat: "text",
LogFormat: "journal",
DNFJson: "/usr/libexec/osbuild-depsolve-dnf",
}
}

View file

@ -77,7 +77,7 @@ func TestDefaultConfig(t *testing.T) {
}
require.Equal(t, expectedDistroAliases, defaultConfig.DistroAliases)
require.Equal(t, "text", defaultConfig.LogFormat)
require.Equal(t, "journal", defaultConfig.LogFormat)
}
func TestConfig(t *testing.T) {

View file

@ -3,11 +3,15 @@ package main
import (
"context"
"flag"
"io"
"log"
"os"
"github.com/coreos/go-systemd/activation"
"github.com/coreos/go-systemd/journal"
"github.com/getsentry/sentry-go"
sentrylogrus "github.com/getsentry/sentry-go/logrus"
"github.com/osbuild/osbuild-composer/internal/common"
slogger "github.com/osbuild/osbuild-composer/pkg/splunk_logger"
"github.com/sirupsen/logrus"
)
@ -28,6 +32,11 @@ func main() {
flag.BoolVar(&verbose, "verbose", false, "Print access log")
flag.Parse()
// Redirect Go standard logger into logrus before it's used by other packages
log.SetOutput(common.Logger())
// Ensure the Go standard logger does not have any prefix or timestamp
log.SetFlags(0)
if !verbose {
logrus.Print("verbose flag is provided for backward compatibility only, current behavior is always printing the access log")
}
@ -49,6 +58,16 @@ func main() {
}
switch config.LogFormat {
case "journal":
// If we are running under systemd, use the journal. Otherwise,
// fallback to text formatter.
if journal.Enabled() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.AddHook(&common.JournalHook{})
logrus.SetOutput(io.Discard)
} else {
logrus.SetFormatter(&logrus.TextFormatter{})
}
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
case "json":