zap_journal.go 2.4 KB

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