embed: set journald logging with zap

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
Gyuho Lee 2018-04-24 15:25:20 -07:00
parent 1fa80bf520
commit 48d5542a76

View File

@ -41,6 +41,7 @@ import (
"github.com/coreos/pkg/capnslog" "github.com/coreos/pkg/capnslog"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
) )
@ -468,19 +469,17 @@ func (cfg *Config) setupLogging() error {
ErrorOutputPaths: make([]string, 0), ErrorOutputPaths: make([]string, 0),
} }
outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{}) outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{})
isJournald := false
for _, v := range cfg.LogOutput { for _, v := range cfg.LogOutput {
switch v { switch v {
case DefaultLogOutput: case DefaultLogOutput:
if syscall.Getppid() == 1 { if syscall.Getppid() == 1 {
// capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr)) // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
// where "NewDefaultFormatter" returns "NewJournaldFormatter" // where "NewDefaultFormatter" returns "NewJournaldFormatter"
// when syscall.Getppid() == 1, specify 'stdout' or 'stderr' to // specify 'stdout' or 'stderr' to override this redirects
// skip journald logging even when running under systemd // when syscall.Getppid() == 1
// TODO: capnlog.NewJournaldFormatter() isJournald = true
fmt.Println("running under init, which may be systemd!") break
outputPaths["stderr"] = struct{}{}
errOutputPaths["stderr"] = struct{}{}
continue
} }
outputPaths["stderr"] = struct{}{} outputPaths["stderr"] = struct{}{}
@ -499,6 +498,8 @@ func (cfg *Config) setupLogging() error {
errOutputPaths[v] = struct{}{} errOutputPaths[v] = struct{}{}
} }
} }
if !isJournald {
for v := range outputPaths { for v := range outputPaths {
lcfg.OutputPaths = append(lcfg.OutputPaths, v) lcfg.OutputPaths = append(lcfg.OutputPaths, v)
} }
@ -532,6 +533,25 @@ func (cfg *Config) setupLogging() error {
if err != nil { if err != nil {
return err return err
} }
} else {
// use stderr as fallback
syncer := zapcore.AddSync(logutil.NewJournaldWriter(os.Stderr))
lvl := zap.NewAtomicLevelAt(zap.InfoLevel)
if cfg.Debug {
lvl = zap.NewAtomicLevelAt(zap.DebugLevel)
grpc.EnableTracing = true
}
cr := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
syncer,
lvl,
)
cfg.logger = zap.New(cr, zap.AddCaller(), zap.ErrorOutput(syncer))
grpcLogOnce.Do(func() {
grpclog.SetLoggerV2(logutil.NewGRPCLoggerV2FromZapCore(cr, syncer))
})
}
logTLSHandshakeFailure := func(conn *tls.Conn, err error) { logTLSHandshakeFailure := func(conn *tls.Conn, err error) {
state := conn.ConnectionState() state := conn.ConnectionState()