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