blob: 06768c679198d13db00faf1cf7caed62a3127dd6 [file] [log] [blame]
Abhay Kumara61c5222025-11-10 07:32:50 +00001// Copyright (c) 2016-2022 Uber Technologies, Inc.
William Kurkianea869482019-04-09 15:16:11 -04002//
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
21package zap
22
23import (
24 "fmt"
25 "io"
William Kurkianea869482019-04-09 15:16:11 -040026
27 "go.uber.org/zap/zapcore"
28
29 "go.uber.org/multierr"
30)
31
32// Open is a high-level wrapper that takes a variadic number of URLs, opens or
33// creates each of the specified resources, and combines them into a locked
34// WriteSyncer. It also returns any error encountered and a function to close
35// any opened files.
36//
37// Passing no URLs returns a no-op WriteSyncer. Zap handles URLs without a
38// scheme and URLs with the "file" scheme. Third-party code may register
39// factories for other schemes using RegisterSink.
40//
41// URLs with the "file" scheme must use absolute paths on the local
42// filesystem. No user, password, port, fragments, or query parameters are
43// allowed, and the hostname must be empty or "localhost".
44//
45// Since it's common to write logs to the local filesystem, URLs without a
46// scheme (e.g., "/var/log/foo.log") are treated as local file paths. Without
47// a scheme, the special paths "stdout" and "stderr" are interpreted as
48// os.Stdout and os.Stderr. When specified without a scheme, relative file
49// paths also work.
50func Open(paths ...string) (zapcore.WriteSyncer, func(), error) {
Abhay Kumara61c5222025-11-10 07:32:50 +000051 writers, closeAll, err := open(paths)
William Kurkianea869482019-04-09 15:16:11 -040052 if err != nil {
53 return nil, nil, err
54 }
55
56 writer := CombineWriteSyncers(writers...)
Abhay Kumara61c5222025-11-10 07:32:50 +000057 return writer, closeAll, nil
William Kurkianea869482019-04-09 15:16:11 -040058}
59
60func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
61 writers := make([]zapcore.WriteSyncer, 0, len(paths))
62 closers := make([]io.Closer, 0, len(paths))
Abhay Kumara61c5222025-11-10 07:32:50 +000063 closeAll := func() {
William Kurkianea869482019-04-09 15:16:11 -040064 for _, c := range closers {
Abhay Kumara61c5222025-11-10 07:32:50 +000065 _ = c.Close()
William Kurkianea869482019-04-09 15:16:11 -040066 }
67 }
68
69 var openErr error
70 for _, path := range paths {
Abhay Kumara61c5222025-11-10 07:32:50 +000071 sink, err := _sinkRegistry.newSink(path)
William Kurkianea869482019-04-09 15:16:11 -040072 if err != nil {
Abhay Kumara61c5222025-11-10 07:32:50 +000073 openErr = multierr.Append(openErr, fmt.Errorf("open sink %q: %w", path, err))
William Kurkianea869482019-04-09 15:16:11 -040074 continue
75 }
76 writers = append(writers, sink)
77 closers = append(closers, sink)
78 }
79 if openErr != nil {
Abhay Kumara61c5222025-11-10 07:32:50 +000080 closeAll()
81 return nil, nil, openErr
William Kurkianea869482019-04-09 15:16:11 -040082 }
83
Abhay Kumara61c5222025-11-10 07:32:50 +000084 return writers, closeAll, nil
William Kurkianea869482019-04-09 15:16:11 -040085}
86
87// CombineWriteSyncers is a utility that combines multiple WriteSyncers into a
88// single, locked WriteSyncer. If no inputs are supplied, it returns a no-op
89// WriteSyncer.
90//
91// It's provided purely as a convenience; the result is no different from
92// using zapcore.NewMultiWriteSyncer and zapcore.Lock individually.
93func CombineWriteSyncers(writers ...zapcore.WriteSyncer) zapcore.WriteSyncer {
94 if len(writers) == 0 {
Abhay Kumara61c5222025-11-10 07:32:50 +000095 return zapcore.AddSync(io.Discard)
William Kurkianea869482019-04-09 15:16:11 -040096 }
97 return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...))
98}