embed: set journald logging with zap
@@ -41,6 +41,7 @@ import (
"github.com/coreos/pkg/capnslog"
"github.com/ghodss/yaml"
"go.uber.org/zap"
+ "go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
@@ -468,19 +469,17 @@ func (cfg *Config) setupLogging() error {
ErrorOutputPaths: make([]string, 0),
}
outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{})
+ isJournald := false
for _, v := range cfg.LogOutput {
switch v {
case DefaultLogOutput:
if syscall.Getppid() == 1 {
// capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
// where "NewDefaultFormatter" returns "NewJournaldFormatter"
- // when syscall.Getppid() == 1, specify 'stdout' or 'stderr' to
- // skip journald logging even when running under systemd
- // TODO: capnlog.NewJournaldFormatter()
- fmt.Println("running under init, which may be systemd!")
- outputPaths["stderr"] = struct{}{}
- errOutputPaths["stderr"] = struct{}{}
- continue
+ // specify 'stdout' or 'stderr' to override this redirects
+ // when syscall.Getppid() == 1
+ isJournald = true
+ break
}
outputPaths["stderr"] = struct{}{}
@@ -499,6 +498,8 @@ func (cfg *Config) setupLogging() error {
errOutputPaths[v] = struct{}{}
}
}
+
+ if !isJournald {
for v := range outputPaths {
lcfg.OutputPaths = append(lcfg.OutputPaths, v)
}
@@ -532,6 +533,25 @@ func (cfg *Config) setupLogging() error {
if err != nil {
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) {
state := conn.ConnectionState()
