go.temporal.io/server@v1.23.0/common/log/with_logger.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package log 26 27 import ( 28 "go.temporal.io/server/common/log/tag" 29 ) 30 31 type withLogger struct { 32 logger Logger 33 tags []tag.Tag 34 } 35 36 var _ Logger = (*withLogger)(nil) 37 38 // With returns Logger instance that prepend every log entry with tags. If logger implements 39 // WithLogger it is used, otherwise every log call will be intercepted. 40 func With(logger Logger, tags ...tag.Tag) Logger { 41 if wl, ok := logger.(WithLogger); ok { 42 return wl.With(tags...) 43 } 44 return newWithLogger(logger, tags...) 45 } 46 47 func newWithLogger(logger Logger, tags ...tag.Tag) *withLogger { 48 return &withLogger{logger: logger, tags: tags} 49 } 50 51 func (l *withLogger) prependTags(tags []tag.Tag) []tag.Tag { 52 return append(l.tags, tags...) 53 } 54 55 // Debug writes message to the log (if enabled). 56 func (l *withLogger) Debug(msg string, tags ...tag.Tag) { 57 l.logger.Debug(msg, l.prependTags(tags)...) 58 } 59 60 // Info writes message to the log (if enabled). 61 func (l *withLogger) Info(msg string, tags ...tag.Tag) { 62 l.logger.Info(msg, l.prependTags(tags)...) 63 } 64 65 // Warn writes message to the log (if enabled). 66 func (l *withLogger) Warn(msg string, tags ...tag.Tag) { 67 l.logger.Warn(msg, l.prependTags(tags)...) 68 } 69 70 // Error writes message to the log (if enabled). 71 func (l *withLogger) Error(msg string, tags ...tag.Tag) { 72 l.logger.Error(msg, l.prependTags(tags)...) 73 } 74 75 // DPanic writes message to the log (if enabled), then calls panic() in development mode 76 func (l *withLogger) DPanic(msg string, tags ...tag.Tag) { 77 l.logger.DPanic(msg, l.prependTags(tags)...) 78 } 79 80 // Panic writes message to the log (if enabled), then calls panic() no matter what 81 func (l *withLogger) Panic(msg string, tags ...tag.Tag) { 82 l.logger.Panic(msg, l.prependTags(tags)...) 83 } 84 85 // Fatal writes message to the log no matter what, then terminates the process 86 func (l *withLogger) Fatal(msg string, tags ...tag.Tag) { 87 l.logger.Fatal(msg, l.prependTags(tags)...) 88 }