github.com/blend/go-sdk@v1.20220411.3/logger/maybe.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"context"
    12  	"os"
    13  )
    14  
    15  // IsLoggerSet returns if the logger instance is set.
    16  func IsLoggerSet(log interface{}) bool {
    17  	if typed, ok := log.(*Logger); ok {
    18  		return typed != nil
    19  	}
    20  	return log != nil
    21  }
    22  
    23  // MaybeDrainContext drains a logger if it's a valid reference and can be drained.
    24  func MaybeDrainContext(ctx context.Context, log interface{}) {
    25  	if typed, ok := log.(*Logger); ok && typed != nil {
    26  		typed.DrainContext(ctx)
    27  	}
    28  }
    29  
    30  // MaybeTrigger triggers an event if the logger is set.
    31  //
    32  // DEPRECATION(1.2021*): this method will be changed to drop the context and use `context.Background()`.
    33  func MaybeTrigger(ctx context.Context, log Triggerable, e Event) {
    34  	if !IsLoggerSet(log) {
    35  		return
    36  	}
    37  	log.TriggerContext(ctx, e)
    38  }
    39  
    40  // MaybeTriggerContext triggers an event if the logger is set in a given context.
    41  func MaybeTriggerContext(ctx context.Context, log Triggerable, e Event) {
    42  	if !IsLoggerSet(log) {
    43  		return
    44  	}
    45  	log.TriggerContext(ctx, e)
    46  }
    47  
    48  // MaybeInfo triggers Info if the logger is set.
    49  func MaybeInfo(log InfoReceiver, args ...interface{}) {
    50  	if !IsLoggerSet(log) {
    51  		return
    52  	}
    53  	log.Info(args...)
    54  }
    55  
    56  // MaybeInfoContext triggers Info in a given context if the logger.
    57  func MaybeInfoContext(ctx context.Context, log InfoReceiver, args ...interface{}) {
    58  	if !IsLoggerSet(log) {
    59  		return
    60  	}
    61  	log.InfoContext(ctx, args...)
    62  }
    63  
    64  // MaybeInfof triggers Infof if the logger is set.
    65  func MaybeInfof(log InfofReceiver, format string, args ...interface{}) {
    66  	if !IsLoggerSet(log) {
    67  		return
    68  	}
    69  	log.Infof(format, args...)
    70  }
    71  
    72  // MaybeInfofContext triggers Infof in a given context if the logger is set.
    73  func MaybeInfofContext(ctx context.Context, log InfofReceiver, format string, args ...interface{}) {
    74  	if !IsLoggerSet(log) {
    75  		return
    76  	}
    77  	log.InfofContext(ctx, format, args...)
    78  }
    79  
    80  // MaybeDebug triggers Debug if the logger is set.
    81  func MaybeDebug(log DebugReceiver, args ...interface{}) {
    82  	if !IsLoggerSet(log) {
    83  		return
    84  	}
    85  	log.Debug(args...)
    86  }
    87  
    88  // MaybeDebugContext triggers Debug in a given context if the logger is set.
    89  func MaybeDebugContext(ctx context.Context, log DebugReceiver, args ...interface{}) {
    90  	if !IsLoggerSet(log) {
    91  		return
    92  	}
    93  	log.DebugContext(ctx, args...)
    94  }
    95  
    96  // MaybeDebugf triggers Debugf if the logger is set.
    97  func MaybeDebugf(log DebugfReceiver, format string, args ...interface{}) {
    98  	if !IsLoggerSet(log) {
    99  		return
   100  	}
   101  	log.Debugf(format, args...)
   102  }
   103  
   104  // MaybeDebugfContext triggers Debugf in a given context if the logger is set.
   105  func MaybeDebugfContext(ctx context.Context, log DebugfReceiver, format string, args ...interface{}) {
   106  	if !IsLoggerSet(log) {
   107  		return
   108  	}
   109  	log.DebugfContext(ctx, format, args...)
   110  }
   111  
   112  // MaybeWarningf triggers Warningf if the logger is set.
   113  func MaybeWarningf(log WarningfReceiver, format string, args ...interface{}) {
   114  	if !IsLoggerSet(log) {
   115  		return
   116  	}
   117  	log.Warningf(format, args...)
   118  }
   119  
   120  // MaybeWarningfContext triggers Warningf in a given context if the logger is set.
   121  func MaybeWarningfContext(ctx context.Context, log WarningfReceiver, format string, args ...interface{}) {
   122  	if !IsLoggerSet(log) {
   123  		return
   124  	}
   125  	log.WarningfContext(ctx, format, args...)
   126  }
   127  
   128  // MaybeWarning triggers Warning if the logger is set.
   129  func MaybeWarning(log WarningReceiver, err error) {
   130  	if !IsLoggerSet(log) || err == nil {
   131  		return
   132  	}
   133  	log.Warning(err)
   134  }
   135  
   136  // MaybeWarningContext triggers Warning in a given context if the logger is set.
   137  func MaybeWarningContext(ctx context.Context, log WarningReceiver, err error) {
   138  	if !IsLoggerSet(log) || err == nil {
   139  		return
   140  	}
   141  	log.WarningContext(ctx, err)
   142  }
   143  
   144  // MaybeErrorf triggers Errorf if the logger is set.
   145  func MaybeErrorf(log ErrorfReceiver, format string, args ...interface{}) {
   146  	if !IsLoggerSet(log) {
   147  		return
   148  	}
   149  	log.Errorf(format, args...)
   150  }
   151  
   152  // MaybeErrorfContext triggers Errorf in a given context if the logger is set.
   153  func MaybeErrorfContext(ctx context.Context, log ErrorfReceiver, format string, args ...interface{}) {
   154  	if !IsLoggerSet(log) {
   155  		return
   156  	}
   157  	log.ErrorfContext(ctx, format, args...)
   158  }
   159  
   160  // MaybeError triggers Error if the logger is set.
   161  func MaybeError(log ErrorReceiver, err error) {
   162  	if !IsLoggerSet(log) || err == nil {
   163  		return
   164  	}
   165  	log.Error(err)
   166  }
   167  
   168  // MaybeErrorContext triggers Error in a given context if the logger is set.
   169  func MaybeErrorContext(ctx context.Context, log ErrorReceiver, err error) {
   170  	if !IsLoggerSet(log) || err == nil {
   171  		return
   172  	}
   173  	log.ErrorContext(ctx, err)
   174  }
   175  
   176  // MaybeFatalf triggers Fatalf if the logger is set.
   177  func MaybeFatalf(log FatalfReceiver, format string, args ...interface{}) {
   178  	if !IsLoggerSet(log) {
   179  		return
   180  	}
   181  	log.Fatalf(format, args...)
   182  }
   183  
   184  // MaybeFatalfContext triggers Fatalf in a given context if the logger is set.
   185  func MaybeFatalfContext(ctx context.Context, log FatalfReceiver, format string, args ...interface{}) {
   186  	if !IsLoggerSet(log) {
   187  		return
   188  	}
   189  	log.FatalfContext(ctx, format, args...)
   190  }
   191  
   192  // MaybeFatal triggers Fatal if the logger is set.
   193  func MaybeFatal(log FatalReceiver, err error) {
   194  	if !IsLoggerSet(log) || err == nil {
   195  		return
   196  	}
   197  	log.Fatal(err)
   198  }
   199  
   200  // MaybeFatalContext triggers Fatal in a given context if the logger is set.
   201  func MaybeFatalContext(ctx context.Context, log FatalReceiver, err error) {
   202  	if !IsLoggerSet(log) || err == nil {
   203  		return
   204  	}
   205  	log.FatalContext(ctx, err)
   206  }
   207  
   208  // MaybeFatalExit triggers Fatal if the logger is set and the error is set, and exit(1)s.
   209  func MaybeFatalExit(log FatalCloser, err error) {
   210  	if !IsLoggerSet(log) || err == nil {
   211  		return
   212  	}
   213  	log.Fatal(err)
   214  	log.Close()
   215  	os.Exit(1)
   216  }