sink.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //go:build darwin || freebsd || linux
  2. // +build darwin freebsd linux
  3. package logrotate
  4. import (
  5. "fmt"
  6. "net/url"
  7. "os"
  8. "os/signal"
  9. "sync/atomic"
  10. "unsafe"
  11. "github.com/ydb-platform/ydb/library/go/core/xerrors"
  12. "go.uber.org/zap"
  13. )
  14. const defaultSchemeName = "logrotate"
  15. // Register logrotate sink in zap sink registry.
  16. // This sink internally is like file sink, but listens to provided logrotate signal
  17. // and reopens file when that signal is delivered
  18. // This can be called only once. Any future calls will result in an error
  19. func RegisterLogrotateSink(sig ...os.Signal) error {
  20. return RegisterNamedLogrotateSink(defaultSchemeName, sig...)
  21. }
  22. // Same as RegisterLogrotateSink, but use provided schemeName instead of default `logrotate`
  23. // Can be useful in special cases for registering different types of sinks for different signal
  24. func RegisterNamedLogrotateSink(schemeName string, sig ...os.Signal) error {
  25. factory := func(url *url.URL) (sink zap.Sink, e error) {
  26. return NewLogrotateSink(url, sig...)
  27. }
  28. return zap.RegisterSink(schemeName, factory)
  29. }
  30. // sink itself, use RegisterLogrotateSink to register it in zap machinery
  31. type sink struct {
  32. path string
  33. notifier chan os.Signal
  34. file unsafe.Pointer
  35. }
  36. // Factory for logrotate sink, which accepts os.Signals to listen to for reloading
  37. // Generally if you don't build your own core it is used by zap machinery.
  38. // See RegisterLogrotateSink.
  39. func NewLogrotateSink(u *url.URL, sig ...os.Signal) (zap.Sink, error) {
  40. notifier := make(chan os.Signal, 1)
  41. signal.Notify(notifier, sig...)
  42. if u.User != nil {
  43. return nil, fmt.Errorf("user and password not allowed with logrotate file URLs: got %v", u)
  44. }
  45. if u.Fragment != "" {
  46. return nil, fmt.Errorf("fragments not allowed with logrotate file URLs: got %v", u)
  47. }
  48. // Error messages are better if we check hostname and port separately.
  49. if u.Port() != "" {
  50. return nil, fmt.Errorf("ports not allowed with logrotate file URLs: got %v", u)
  51. }
  52. if hn := u.Hostname(); hn != "" && hn != "localhost" {
  53. return nil, fmt.Errorf("logrotate file URLs must leave host empty or use localhost: got %v", u)
  54. }
  55. sink := &sink{
  56. path: u.Path,
  57. notifier: notifier,
  58. }
  59. if err := sink.reopen(); err != nil {
  60. return nil, err
  61. }
  62. go sink.listenToSignal()
  63. return sink, nil
  64. }
  65. // wait for signal delivery or chanel close
  66. func (m *sink) listenToSignal() {
  67. for {
  68. _, ok := <-m.notifier
  69. if !ok {
  70. return
  71. }
  72. if err := m.reopen(); err != nil {
  73. // Last chance to signalize about an error
  74. _, _ = fmt.Fprintf(os.Stderr, "%s", err)
  75. }
  76. }
  77. }
  78. func (m *sink) reopen() error {
  79. file, err := os.OpenFile(m.path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
  80. if err != nil {
  81. return xerrors.Errorf("failed to open log file on %s: %w", m.path, err)
  82. }
  83. old := (*os.File)(m.file)
  84. atomic.StorePointer(&m.file, unsafe.Pointer(file))
  85. if old != nil {
  86. if err := old.Close(); err != nil {
  87. return xerrors.Errorf("failed to close old file: %w", err)
  88. }
  89. }
  90. return nil
  91. }
  92. func (m *sink) getFile() *os.File {
  93. return (*os.File)(atomic.LoadPointer(&m.file))
  94. }
  95. func (m *sink) Close() error {
  96. signal.Stop(m.notifier)
  97. close(m.notifier)
  98. return m.getFile().Close()
  99. }
  100. func (m *sink) Write(p []byte) (n int, err error) {
  101. return m.getFile().Write(p)
  102. }
  103. func (m *sink) Sync() error {
  104. return m.getFile().Sync()
  105. }