sink.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2016-2022 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zap
  21. import (
  22. "errors"
  23. "fmt"
  24. "io"
  25. "net/url"
  26. "os"
  27. "path/filepath"
  28. "strings"
  29. "sync"
  30. "go.uber.org/zap/zapcore"
  31. )
  32. const schemeFile = "file"
  33. var _sinkRegistry = newSinkRegistry()
  34. // Sink defines the interface to write to and close logger destinations.
  35. type Sink interface {
  36. zapcore.WriteSyncer
  37. io.Closer
  38. }
  39. type errSinkNotFound struct {
  40. scheme string
  41. }
  42. func (e *errSinkNotFound) Error() string {
  43. return fmt.Sprintf("no sink found for scheme %q", e.scheme)
  44. }
  45. type nopCloserSink struct{ zapcore.WriteSyncer }
  46. func (nopCloserSink) Close() error { return nil }
  47. type sinkRegistry struct {
  48. mu sync.Mutex
  49. factories map[string]func(*url.URL) (Sink, error) // keyed by scheme
  50. openFile func(string, int, os.FileMode) (*os.File, error) // type matches os.OpenFile
  51. }
  52. func newSinkRegistry() *sinkRegistry {
  53. sr := &sinkRegistry{
  54. factories: make(map[string]func(*url.URL) (Sink, error)),
  55. openFile: os.OpenFile,
  56. }
  57. // Infallible operation: the registry is empty, so we can't have a conflict.
  58. _ = sr.RegisterSink(schemeFile, sr.newFileSinkFromURL)
  59. return sr
  60. }
  61. // RegisterScheme registers the given factory for the specific scheme.
  62. func (sr *sinkRegistry) RegisterSink(scheme string, factory func(*url.URL) (Sink, error)) error {
  63. sr.mu.Lock()
  64. defer sr.mu.Unlock()
  65. if scheme == "" {
  66. return errors.New("can't register a sink factory for empty string")
  67. }
  68. normalized, err := normalizeScheme(scheme)
  69. if err != nil {
  70. return fmt.Errorf("%q is not a valid scheme: %v", scheme, err)
  71. }
  72. if _, ok := sr.factories[normalized]; ok {
  73. return fmt.Errorf("sink factory already registered for scheme %q", normalized)
  74. }
  75. sr.factories[normalized] = factory
  76. return nil
  77. }
  78. func (sr *sinkRegistry) newSink(rawURL string) (Sink, error) {
  79. // URL parsing doesn't work well for Windows paths such as `c:\log.txt`, as scheme is set to
  80. // the drive, and path is unset unless `c:/log.txt` is used.
  81. // To avoid Windows-specific URL handling, we instead check IsAbs to open as a file.
  82. // filepath.IsAbs is OS-specific, so IsAbs('c:/log.txt') is false outside of Windows.
  83. if filepath.IsAbs(rawURL) {
  84. return sr.newFileSinkFromPath(rawURL)
  85. }
  86. u, err := url.Parse(rawURL)
  87. if err != nil {
  88. return nil, fmt.Errorf("can't parse %q as a URL: %v", rawURL, err)
  89. }
  90. if u.Scheme == "" {
  91. u.Scheme = schemeFile
  92. }
  93. sr.mu.Lock()
  94. factory, ok := sr.factories[u.Scheme]
  95. sr.mu.Unlock()
  96. if !ok {
  97. return nil, &errSinkNotFound{u.Scheme}
  98. }
  99. return factory(u)
  100. }
  101. // RegisterSink registers a user-supplied factory for all sinks with a
  102. // particular scheme.
  103. //
  104. // All schemes must be ASCII, valid under section 0.1 of RFC 3986
  105. // (https://tools.ietf.org/html/rfc3983#section-3.1), and must not already
  106. // have a factory registered. Zap automatically registers a factory for the
  107. // "file" scheme.
  108. func RegisterSink(scheme string, factory func(*url.URL) (Sink, error)) error {
  109. return _sinkRegistry.RegisterSink(scheme, factory)
  110. }
  111. func (sr *sinkRegistry) newFileSinkFromURL(u *url.URL) (Sink, error) {
  112. if u.User != nil {
  113. return nil, fmt.Errorf("user and password not allowed with file URLs: got %v", u)
  114. }
  115. if u.Fragment != "" {
  116. return nil, fmt.Errorf("fragments not allowed with file URLs: got %v", u)
  117. }
  118. if u.RawQuery != "" {
  119. return nil, fmt.Errorf("query parameters not allowed with file URLs: got %v", u)
  120. }
  121. // Error messages are better if we check hostname and port separately.
  122. if u.Port() != "" {
  123. return nil, fmt.Errorf("ports not allowed with file URLs: got %v", u)
  124. }
  125. if hn := u.Hostname(); hn != "" && hn != "localhost" {
  126. return nil, fmt.Errorf("file URLs must leave host empty or use localhost: got %v", u)
  127. }
  128. return sr.newFileSinkFromPath(u.Path)
  129. }
  130. func (sr *sinkRegistry) newFileSinkFromPath(path string) (Sink, error) {
  131. switch path {
  132. case "stdout":
  133. return nopCloserSink{os.Stdout}, nil
  134. case "stderr":
  135. return nopCloserSink{os.Stderr}, nil
  136. }
  137. return sr.openFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o666)
  138. }
  139. func normalizeScheme(s string) (string, error) {
  140. // https://tools.ietf.org/html/rfc3986#section-3.1
  141. s = strings.ToLower(s)
  142. if first := s[0]; 'a' > first || 'z' < first {
  143. return "", errors.New("must start with a letter")
  144. }
  145. for i := 1; i < len(s); i++ { // iterate over bytes, not runes
  146. c := s[i]
  147. switch {
  148. case 'a' <= c && c <= 'z':
  149. continue
  150. case '0' <= c && c <= '9':
  151. continue
  152. case c == '.' || c == '+' || c == '-':
  153. continue
  154. }
  155. return "", fmt.Errorf("may not contain %q", c)
  156. }
  157. return s, nil
  158. }