writer.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "fmt"
  23. "io"
  24. "go.uber.org/zap/zapcore"
  25. "go.uber.org/multierr"
  26. )
  27. // Open is a high-level wrapper that takes a variadic number of URLs, opens or
  28. // creates each of the specified resources, and combines them into a locked
  29. // WriteSyncer. It also returns any error encountered and a function to close
  30. // any opened files.
  31. //
  32. // Passing no URLs returns a no-op WriteSyncer. Zap handles URLs without a
  33. // scheme and URLs with the "file" scheme. Third-party code may register
  34. // factories for other schemes using RegisterSink.
  35. //
  36. // URLs with the "file" scheme must use absolute paths on the local
  37. // filesystem. No user, password, port, fragments, or query parameters are
  38. // allowed, and the hostname must be empty or "localhost".
  39. //
  40. // Since it's common to write logs to the local filesystem, URLs without a
  41. // scheme (e.g., "/var/log/foo.log") are treated as local file paths. Without
  42. // a scheme, the special paths "stdout" and "stderr" are interpreted as
  43. // os.Stdout and os.Stderr. When specified without a scheme, relative file
  44. // paths also work.
  45. func Open(paths ...string) (zapcore.WriteSyncer, func(), error) {
  46. writers, closeAll, err := open(paths)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. writer := CombineWriteSyncers(writers...)
  51. return writer, closeAll, nil
  52. }
  53. func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
  54. writers := make([]zapcore.WriteSyncer, 0, len(paths))
  55. closers := make([]io.Closer, 0, len(paths))
  56. closeAll := func() {
  57. for _, c := range closers {
  58. _ = c.Close()
  59. }
  60. }
  61. var openErr error
  62. for _, path := range paths {
  63. sink, err := _sinkRegistry.newSink(path)
  64. if err != nil {
  65. openErr = multierr.Append(openErr, fmt.Errorf("open sink %q: %w", path, err))
  66. continue
  67. }
  68. writers = append(writers, sink)
  69. closers = append(closers, sink)
  70. }
  71. if openErr != nil {
  72. closeAll()
  73. return nil, nil, openErr
  74. }
  75. return writers, closeAll, nil
  76. }
  77. // CombineWriteSyncers is a utility that combines multiple WriteSyncers into a
  78. // single, locked WriteSyncer. If no inputs are supplied, it returns a no-op
  79. // WriteSyncer.
  80. //
  81. // It's provided purely as a convenience; the result is no different from
  82. // using zapcore.NewMultiWriteSyncer and zapcore.Lock individually.
  83. func CombineWriteSyncers(writers ...zapcore.WriteSyncer) zapcore.WriteSyncer {
  84. if len(writers) == 0 {
  85. return zapcore.AddSync(io.Discard)
  86. }
  87. return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...))
  88. }