github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/adapter/generic_sugar.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package adapter
    14  
    15  import (
    16  	"github.com/facebookincubator/go-belt"
    17  	"github.com/facebookincubator/go-belt/pkg/field"
    18  	"github.com/facebookincubator/go-belt/tool/logger/types"
    19  )
    20  
    21  // GenericSugar is a generic implementation of sugar-methods for a given types.CompactLogger.
    22  //
    23  // It does not contain any logic, it is just a bunch of method-wrappers to provide
    24  // logger.Logger implementation using a given types.CompactLogger.
    25  type GenericSugar struct {
    26  	CompactLogger CompactLogger
    27  }
    28  
    29  var _ types.Logger = (*GenericSugar)(nil)
    30  
    31  // Level implements logger.Logger.
    32  func (l GenericSugar) Level() types.Level {
    33  	return l.CompactLogger.Level()
    34  }
    35  
    36  // Log implements logger.Logger.
    37  func (l GenericSugar) Log(level types.Level, values ...any) {
    38  	l.CompactLogger.Log(level, values...)
    39  }
    40  
    41  // LogFields implements logger.Logger.
    42  func (l GenericSugar) LogFields(level types.Level, message string, fields field.AbstractFields) {
    43  	l.CompactLogger.LogFields(level, message, fields)
    44  }
    45  
    46  // Logf implements logger.Logger.
    47  func (l GenericSugar) Logf(level types.Level, format string, args ...any) {
    48  	l.CompactLogger.Logf(level, format, args...)
    49  }
    50  
    51  // Flush implements logger.Logger.
    52  func (l GenericSugar) Flush() {
    53  	l.CompactLogger.Flush()
    54  }
    55  
    56  // Emitter implements logger.Logger.
    57  func (l GenericSugar) Emitter() types.Emitter {
    58  	return l.CompactLogger.Emitter()
    59  }
    60  
    61  // WithLevel implements logger.Logger.
    62  func (l GenericSugar) WithLevel(newLevel types.Level) types.Logger {
    63  	return GenericSugar{CompactLogger: l.CompactLogger.WithLevel(newLevel)}
    64  }
    65  
    66  // WithPreHooks implements logger.Logger.
    67  func (l GenericSugar) WithPreHooks(preHooks ...types.PreHook) types.Logger {
    68  	return GenericSugar{CompactLogger: l.CompactLogger.WithPreHooks(preHooks...)}
    69  }
    70  
    71  // WithHooks implements logger.Logger.
    72  func (l GenericSugar) WithHooks(hooks ...types.Hook) types.Logger {
    73  	return GenericSugar{CompactLogger: l.CompactLogger.WithHooks(hooks...)}
    74  }
    75  
    76  // WithField implements logger.Logger.
    77  func (l GenericSugar) WithField(
    78  	key field.Key,
    79  	value field.Value,
    80  	props ...field.Property,
    81  ) types.Logger {
    82  	return GenericSugar{CompactLogger: l.CompactLogger.WithField(key, value, props...)}
    83  }
    84  
    85  // WithFields implements logger.Logger.
    86  func (l GenericSugar) WithFields(fields field.AbstractFields) types.Logger {
    87  	return GenericSugar{CompactLogger: l.CompactLogger.WithFields(fields)}
    88  }
    89  
    90  // WithTraceIDs implements logger.Logger.
    91  func (l GenericSugar) WithTraceIDs(allTraceIDs belt.TraceIDs, newTraceIDs int) belt.Tool {
    92  	return GenericSugar{CompactLogger: l.CompactLogger.WithTraceIDs(allTraceIDs, newTraceIDs).(CompactLogger)}
    93  }
    94  
    95  // WithContextFields implements logger.Logger.
    96  func (l GenericSugar) WithContextFields(allFields *field.FieldsChain, newFields int) belt.Tool {
    97  	return GenericSugar{CompactLogger: l.CompactLogger.WithContextFields(allFields, newFields).(CompactLogger)}
    98  }
    99  
   100  // WithMessagePrefix implements logger.Logger.
   101  func (l GenericSugar) WithMessagePrefix(prefix string) types.Logger {
   102  	return GenericSugar{CompactLogger: l.CompactLogger.WithMessagePrefix(prefix)}
   103  }
   104  
   105  // WithMessagePrefix implements logger.Logger.
   106  func (l GenericSugar) WithEntryProperties(props ...types.EntryProperty) types.Logger {
   107  	return GenericSugar{CompactLogger: l.CompactLogger.WithEntryProperties(props...)}
   108  }
   109  
   110  // TraceFields implements logger.Logger.
   111  func (l GenericSugar) TraceFields(message string, fields field.AbstractFields) {
   112  	l.LogFields(types.LevelTrace, message, fields)
   113  }
   114  
   115  // DebugFields implements logger.Logger.
   116  func (l GenericSugar) DebugFields(message string, fields field.AbstractFields) {
   117  	l.LogFields(types.LevelDebug, message, fields)
   118  }
   119  
   120  // InfoFields implements logger.Logger.
   121  func (l GenericSugar) InfoFields(message string, fields field.AbstractFields) {
   122  	l.LogFields(types.LevelInfo, message, fields)
   123  }
   124  
   125  // WarnFields implements logger.Logger.
   126  func (l GenericSugar) WarnFields(message string, fields field.AbstractFields) {
   127  	l.LogFields(types.LevelWarning, message, fields)
   128  }
   129  
   130  // ErrorFields implements logger.Logger.
   131  func (l GenericSugar) ErrorFields(message string, fields field.AbstractFields) {
   132  	l.LogFields(types.LevelError, message, fields)
   133  }
   134  
   135  // PanicFields implements logger.Logger.
   136  func (l GenericSugar) PanicFields(message string, fields field.AbstractFields) {
   137  	l.LogFields(types.LevelPanic, message, fields)
   138  }
   139  
   140  // FatalFields implements logger.Logger.
   141  func (l GenericSugar) FatalFields(message string, fields field.AbstractFields) {
   142  	l.LogFields(types.LevelFatal, message, fields)
   143  }
   144  
   145  // Trace implements logger.Logger.
   146  func (l GenericSugar) Trace(values ...any) {
   147  	l.Log(types.LevelTrace, values...)
   148  }
   149  
   150  // Debug implements logger.Logger.
   151  func (l GenericSugar) Debug(values ...any) {
   152  	l.Log(types.LevelDebug, values...)
   153  }
   154  
   155  // Info implements logger.Logger.
   156  func (l GenericSugar) Info(values ...any) {
   157  	l.Log(types.LevelInfo, values...)
   158  }
   159  
   160  // Warn implements logger.Logger.
   161  func (l GenericSugar) Warn(values ...any) {
   162  	l.Log(types.LevelWarning, values...)
   163  }
   164  
   165  // Error implements logger.Logger.
   166  func (l GenericSugar) Error(values ...any) {
   167  	l.Log(types.LevelError, values...)
   168  }
   169  
   170  // Panic implements logger.Logger.
   171  func (l GenericSugar) Panic(values ...any) {
   172  	l.Log(types.LevelPanic, values...)
   173  }
   174  
   175  // Fatal implements logger.Logger.
   176  func (l GenericSugar) Fatal(values ...any) {
   177  	l.Log(types.LevelFatal, values...)
   178  }
   179  
   180  // Tracef implements logger.Logger.
   181  func (l GenericSugar) Tracef(format string, args ...any) {
   182  	l.Logf(types.LevelTrace, format, args...)
   183  }
   184  
   185  // Debugf implements logger.Logger.
   186  func (l GenericSugar) Debugf(format string, args ...any) {
   187  	l.Logf(types.LevelDebug, format, args...)
   188  }
   189  
   190  // Infof implements logger.Logger.
   191  func (l GenericSugar) Infof(format string, args ...any) {
   192  	l.Logf(types.LevelInfo, format, args...)
   193  }
   194  
   195  // Warnf implements logger.Logger.
   196  func (l GenericSugar) Warnf(format string, args ...any) {
   197  	l.Logf(types.LevelWarning, format, args...)
   198  }
   199  
   200  // Errorf implements logger.Logger.
   201  func (l GenericSugar) Errorf(format string, args ...any) {
   202  	l.Logf(types.LevelError, format, args...)
   203  }
   204  
   205  // Panicf implements logger.Logger.
   206  func (l GenericSugar) Panicf(format string, args ...any) {
   207  	l.Logf(types.LevelPanic, format, args...)
   208  }
   209  
   210  // Fatalf implements logger.Logger.
   211  func (l GenericSugar) Fatalf(format string, args ...any) {
   212  	l.Logf(types.LevelFatal, format, args...)
   213  }