...

Source file src/go.etcd.io/etcd/client/pkg/v3/logutil/zap_journal.go

Documentation: go.etcd.io/etcd/client/pkg/v3/logutil

     1  // Copyright 2018 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build !windows
    16  // +build !windows
    17  
    18  package logutil
    19  
    20  import (
    21  	"bytes"
    22  	"encoding/json"
    23  	"fmt"
    24  	"io"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	"go.etcd.io/etcd/client/pkg/v3/systemd"
    29  
    30  	"github.com/coreos/go-systemd/v22/journal"
    31  	"go.uber.org/zap/zapcore"
    32  )
    33  
    34  // NewJournalWriter wraps "io.Writer" to redirect log output
    35  // to the local systemd journal. If journald send fails, it fails
    36  // back to writing to the original writer.
    37  // The decode overhead is only <30µs per write.
    38  // Reference: https://github.com/coreos/pkg/blob/master/capnslog/journald_formatter.go
    39  func NewJournalWriter(wr io.Writer) (io.Writer, error) {
    40  	return &journalWriter{Writer: wr}, systemd.DialJournal()
    41  }
    42  
    43  type journalWriter struct {
    44  	io.Writer
    45  }
    46  
    47  // WARN: assume that etcd uses default field names in zap encoder config
    48  // make sure to keep this up-to-date!
    49  type logLine struct {
    50  	Level  string `json:"level"`
    51  	Caller string `json:"caller"`
    52  }
    53  
    54  func (w *journalWriter) Write(p []byte) (int, error) {
    55  	line := &logLine{}
    56  	if err := json.NewDecoder(bytes.NewReader(p)).Decode(line); err != nil {
    57  		return 0, err
    58  	}
    59  
    60  	var pri journal.Priority
    61  	switch line.Level {
    62  	case zapcore.DebugLevel.String():
    63  		pri = journal.PriDebug
    64  	case zapcore.InfoLevel.String():
    65  		pri = journal.PriInfo
    66  
    67  	case zapcore.WarnLevel.String():
    68  		pri = journal.PriWarning
    69  	case zapcore.ErrorLevel.String():
    70  		pri = journal.PriErr
    71  
    72  	case zapcore.DPanicLevel.String():
    73  		pri = journal.PriCrit
    74  	case zapcore.PanicLevel.String():
    75  		pri = journal.PriCrit
    76  	case zapcore.FatalLevel.String():
    77  		pri = journal.PriCrit
    78  
    79  	default:
    80  		panic(fmt.Errorf("unknown log level: %q", line.Level))
    81  	}
    82  
    83  	err := journal.Send(string(p), pri, map[string]string{
    84  		"PACKAGE":           filepath.Dir(line.Caller),
    85  		"SYSLOG_IDENTIFIER": filepath.Base(os.Args[0]),
    86  	})
    87  	if err != nil {
    88  		// "journal" also falls back to stderr
    89  		// "fmt.Fprintln(os.Stderr, s)"
    90  		return w.Writer.Write(p)
    91  	}
    92  	return 0, nil
    93  }
    94  

View as plain text