go.temporal.io/server@v1.23.0/common/log/replay_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/sdk/workflow"
    29  
    30  	"go.temporal.io/server/common/log/tag"
    31  )
    32  
    33  const extraSkipForReplayLogger = 1
    34  
    35  type (
    36  	replayLogger struct {
    37  		logger            Logger
    38  		ctx               workflow.Context
    39  		enableLogInReplay bool
    40  	}
    41  )
    42  
    43  var _ Logger = (*replayLogger)(nil)
    44  
    45  // NewReplayLogger creates a logger which is aware of Temporal replay mode
    46  func NewReplayLogger(logger Logger, ctx workflow.Context, enableLogInReplay bool) *replayLogger {
    47  	if sl, ok := logger.(SkipLogger); ok {
    48  		logger = sl.Skip(extraSkipForReplayLogger)
    49  	}
    50  	return &replayLogger{
    51  		logger:            logger,
    52  		ctx:               ctx,
    53  		enableLogInReplay: enableLogInReplay,
    54  	}
    55  }
    56  
    57  func (r *replayLogger) Debug(msg string, tags ...tag.Tag) {
    58  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
    59  		return
    60  	}
    61  	r.logger.Debug(msg, tags...)
    62  }
    63  
    64  func (r *replayLogger) Info(msg string, tags ...tag.Tag) {
    65  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
    66  		return
    67  	}
    68  	r.logger.Info(msg, tags...)
    69  }
    70  
    71  func (r *replayLogger) Warn(msg string, tags ...tag.Tag) {
    72  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
    73  		return
    74  	}
    75  	r.logger.Warn(msg, tags...)
    76  }
    77  
    78  func (r *replayLogger) Error(msg string, tags ...tag.Tag) {
    79  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
    80  		return
    81  	}
    82  	r.logger.Error(msg, tags...)
    83  }
    84  
    85  func (r *replayLogger) DPanic(msg string, tags ...tag.Tag) {
    86  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
    87  		return
    88  	}
    89  	r.logger.DPanic(msg, tags...)
    90  }
    91  
    92  func (r *replayLogger) Panic(msg string, tags ...tag.Tag) {
    93  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
    94  		return
    95  	}
    96  	r.logger.Panic(msg, tags...)
    97  }
    98  
    99  func (r *replayLogger) Fatal(msg string, tags ...tag.Tag) {
   100  	if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
   101  		return
   102  	}
   103  	r.logger.Fatal(msg, tags...)
   104  }
   105  
   106  func (r *replayLogger) With(tags ...tag.Tag) Logger {
   107  	return &replayLogger{
   108  		logger:            newWithLogger(r.logger, tags...),
   109  		ctx:               r.ctx,
   110  		enableLogInReplay: r.enableLogInReplay,
   111  	}
   112  }