zap_journal.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. //go:build !windows
  15. // +build !windows
  16. package logutil
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "io"
  22. "os"
  23. "path/filepath"
  24. "go.etcd.io/etcd/client/pkg/v3/systemd"
  25. "github.com/coreos/go-systemd/v22/journal"
  26. "go.uber.org/zap/zapcore"
  27. )
  28. // NewJournalWriter wraps "io.Writer" to redirect log output
  29. // to the local systemd journal. If journald send fails, it fails
  30. // back to writing to the original writer.
  31. // The decode overhead is only <30µs per write.
  32. // Reference: https://github.com/coreos/pkg/blob/master/capnslog/journald_formatter.go
  33. func NewJournalWriter(wr io.Writer) (io.Writer, error) {
  34. return &journalWriter{Writer: wr}, systemd.DialJournal()
  35. }
  36. type journalWriter struct {
  37. io.Writer
  38. }
  39. // WARN: assume that etcd uses default field names in zap encoder config
  40. // make sure to keep this up-to-date!
  41. type logLine struct {
  42. Level string `json:"level"`
  43. Caller string `json:"caller"`
  44. }
  45. func (w *journalWriter) Write(p []byte) (int, error) {
  46. line := &logLine{}
  47. if err := json.NewDecoder(bytes.NewReader(p)).Decode(line); err != nil {
  48. return 0, err
  49. }
  50. var pri journal.Priority
  51. switch line.Level {
  52. case zapcore.DebugLevel.String():
  53. pri = journal.PriDebug
  54. case zapcore.InfoLevel.String():
  55. pri = journal.PriInfo
  56. case zapcore.WarnLevel.String():
  57. pri = journal.PriWarning
  58. case zapcore.ErrorLevel.String():
  59. pri = journal.PriErr
  60. case zapcore.DPanicLevel.String():
  61. pri = journal.PriCrit
  62. case zapcore.PanicLevel.String():
  63. pri = journal.PriCrit
  64. case zapcore.FatalLevel.String():
  65. pri = journal.PriCrit
  66. default:
  67. panic(fmt.Errorf("unknown log level: %q", line.Level))
  68. }
  69. err := journal.Send(string(p), pri, map[string]string{
  70. "PACKAGE": filepath.Dir(line.Caller),
  71. "SYSLOG_IDENTIFIER": filepath.Base(os.Args[0]),
  72. })
  73. if err != nil {
  74. // "journal" also falls back to stderr
  75. // "fmt.Fprintln(os.Stderr, s)"
  76. return w.Writer.Write(p)
  77. }
  78. return 0, nil
  79. }