From 0ee4b516f4e39ccef4b893a678b01095acbe6ad0 Mon Sep 17 00:00:00 2001 From: zhongjiawei Date: Mon, 7 Nov 2022 18:56:11 +0800 Subject: [PATCH] runc:runc log forward to syslog --- runc-1.1.3/main.go | 27 ++++++++++++++++++- .../sirupsen/logrus/hooks/syslog/syslog.go | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/runc-1.1.3/main.go b/runc-1.1.3/main.go index e52a2ea..381681e 100644 --- a/runc-1.1.3/main.go +++ b/runc-1.1.3/main.go @@ -1,19 +1,23 @@ package main import ( + "encoding/json" "errors" "fmt" "io" + "log/syslog" "os" "path/filepath" "runtime" "strconv" "strings" + "time" "github.com/opencontainers/runc/libcontainer/seccomp" "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" "github.com/urfave/cli" ) @@ -215,6 +219,12 @@ func configLogrus(context *cli.Context) error { return err } logrus.SetOutput(f) + hook, serr := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO|syslog.LOG_USER, "docker-runc") + if serr != nil { + logToFile(f, "error", fmt.Sprintf("new syslog hook get %s", serr)) + } else { + logrus.AddHook(hook) + } } if logLevel := context.GlobalString("log-level"); logLevel != "" { lvl, err := logrus.ParseLevel(logLevel) @@ -230,4 +240,19 @@ func configLogrus(context *cli.Context) error { } return nil } - +func logToFile(f io.Writer, level string, msg string) { + var ( + log struct { + Level string + Msg string + Time time.Time + } + ) + log.Level = level + log.Msg = msg + log.Time = time.Now() + s, err := json.Marshal(log) + if err != nil { + fmt.Fprint(f, string(s)) + } +} diff --git a/runc-1.1.3/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go b/runc-1.1.3/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go index b6fa374..430f646 100644 --- a/runc-1.1.3/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go +++ b/runc-1.1.3/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go @@ -2,7 +2,7 @@ package logrus_syslog import ( "fmt" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "log/syslog" "os" ) -- 2.30.0