go.temporal.io/server@v1.23.0/common/log/lazy_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  	"sync"
    29  
    30  	"go.temporal.io/server/common/log/tag"
    31  )
    32  
    33  var _ Logger = (*lazyLogger)(nil)
    34  
    35  type (
    36  	lazyLogger struct {
    37  		logger Logger
    38  		tagFn  func() []tag.Tag
    39  
    40  		once sync.Once
    41  	}
    42  )
    43  
    44  func NewLazyLogger(logger Logger, tagFn func() []tag.Tag) *lazyLogger {
    45  	return &lazyLogger{
    46  		logger: logger,
    47  		tagFn:  tagFn,
    48  	}
    49  }
    50  
    51  func (l *lazyLogger) Debug(msg string, tags ...tag.Tag) {
    52  	l.once.Do(l.tagLogger)
    53  	l.logger.Debug(msg, tags...)
    54  }
    55  
    56  func (l *lazyLogger) Info(msg string, tags ...tag.Tag) {
    57  	l.once.Do(l.tagLogger)
    58  	l.logger.Info(msg, tags...)
    59  }
    60  
    61  func (l *lazyLogger) Warn(msg string, tags ...tag.Tag) {
    62  	l.once.Do(l.tagLogger)
    63  	l.logger.Warn(msg, tags...)
    64  }
    65  
    66  func (l *lazyLogger) Error(msg string, tags ...tag.Tag) {
    67  	l.once.Do(l.tagLogger)
    68  	l.logger.Error(msg, tags...)
    69  }
    70  
    71  func (l *lazyLogger) DPanic(msg string, tags ...tag.Tag) {
    72  	l.once.Do(l.tagLogger)
    73  	l.logger.DPanic(msg, tags...)
    74  }
    75  
    76  func (l *lazyLogger) Panic(msg string, tags ...tag.Tag) {
    77  	l.once.Do(l.tagLogger)
    78  	l.logger.Panic(msg, tags...)
    79  }
    80  
    81  func (l *lazyLogger) Fatal(msg string, tags ...tag.Tag) {
    82  	l.once.Do(l.tagLogger)
    83  	l.logger.Fatal(msg, tags...)
    84  }
    85  
    86  func (l *lazyLogger) tagLogger() {
    87  	l.logger = With(l.logger, l.tagFn()...)
    88  }