cmd/osbuild-composer: journald support
This commit is contained in:
parent
e68449404f
commit
f3c0daebbf
7 changed files with 345 additions and 3 deletions
70
internal/common/journal_hook.go
Normal file
70
internal/common/journal_hook.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// Inspired by github.com/wercker/journalhook (MIT license)
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/go-systemd/journal"
|
||||
logrus "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type JournalHook struct{}
|
||||
|
||||
var (
|
||||
severityMap = map[logrus.Level]journal.Priority{
|
||||
logrus.DebugLevel: journal.PriDebug,
|
||||
logrus.InfoLevel: journal.PriInfo,
|
||||
logrus.WarnLevel: journal.PriWarning,
|
||||
logrus.ErrorLevel: journal.PriErr,
|
||||
logrus.FatalLevel: journal.PriCrit,
|
||||
logrus.PanicLevel: journal.PriEmerg,
|
||||
}
|
||||
)
|
||||
|
||||
func stringifyOp(r rune) rune {
|
||||
switch {
|
||||
case r >= 'A' && r <= 'Z':
|
||||
return r
|
||||
case r >= '0' && r <= '9':
|
||||
return r
|
||||
case r == '_':
|
||||
return r
|
||||
case r >= 'a' && r <= 'z':
|
||||
return r - 32
|
||||
default:
|
||||
return rune('_')
|
||||
}
|
||||
}
|
||||
|
||||
func stringifyKey(key string) string {
|
||||
key = strings.Map(stringifyOp, key)
|
||||
key = strings.TrimPrefix(key, "_")
|
||||
return key
|
||||
}
|
||||
|
||||
// Journal wants strings but logrus takes anything.
|
||||
func stringifyEntries(data map[string]interface{}) map[string]string {
|
||||
entries := make(map[string]string)
|
||||
for k, v := range data {
|
||||
|
||||
key := stringifyKey(k)
|
||||
entries[key] = fmt.Sprint(v)
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
func (hook *JournalHook) Fire(entry *logrus.Entry) error {
|
||||
return journal.Send(entry.Message, severityMap[entry.Level], stringifyEntries(entry.Data))
|
||||
}
|
||||
|
||||
func (hook *JournalHook) Levels() []logrus.Level {
|
||||
return []logrus.Level{
|
||||
logrus.PanicLevel,
|
||||
logrus.FatalLevel,
|
||||
logrus.ErrorLevel,
|
||||
logrus.WarnLevel,
|
||||
logrus.InfoLevel,
|
||||
logrus.DebugLevel,
|
||||
}
|
||||
}
|
||||
27
internal/common/journal_hook_test.go
Normal file
27
internal/common/journal_hook_test.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package common
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestStringifyEntries(t *testing.T) {
|
||||
input := map[string]interface{}{
|
||||
"foo": "bar",
|
||||
"baz": 123,
|
||||
"foo-foo": "x",
|
||||
"-bar": "1",
|
||||
}
|
||||
|
||||
output := stringifyEntries(input)
|
||||
if output["FOO"] != "bar" {
|
||||
t.Fatalf("%v", output)
|
||||
t.Fatalf("expected value 'bar'. Got %q", output["FOO"])
|
||||
}
|
||||
if output["BAZ"] != "123" {
|
||||
t.Fatalf("expected value '123'. Got %q", output["BAZ"])
|
||||
}
|
||||
if output["FOO_FOO"] != "x" {
|
||||
t.Fatalf("expected value 'x'. Got %q", output["FOO_FOO"])
|
||||
}
|
||||
if output["BAR"] != "1" {
|
||||
t.Fatalf("expected value 'x'. Got %q", output["BAR"])
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue