github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekalog/exported.go (about)

     1  // Copyright © 2017-2021. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekalog
     7  
     8  import (
     9  	"fmt"
    10  	"io"
    11  	"time"
    12  
    13  	"github.com/qioalice/ekago/v3/internal/ekaletter"
    14  )
    15  
    16  // ------------------------------ COMMON METHODS ------------------------------ //
    17  // ---------------------------------------------------------------------------- //
    18  
    19  // Copy returns a copy of the package-level Logger. Does nothing for 'nopLogger'.
    20  //
    21  // Copy is useful when you need to build your Entry step-by-step,
    22  // adding fields, messages, etc.
    23  func Copy() (copy *Logger) {
    24  	return baseLogger.derive()
    25  }
    26  
    27  // Sync forces to flush all Integrator buffers of package Logger
    28  // and makes sure all pending Entry are written.
    29  func Sync() error {
    30  	return baseLogger.Sync()
    31  }
    32  
    33  // --------------------------- FIELDS ADDING METHODS -------------------------- //
    34  // ---------------------------------------------------------------------------- //
    35  
    36  // Methods below are code-generated.
    37  
    38  func With(f ekaletter.LetterField) *Logger {
    39  	return baseLogger.addField(f)
    40  }
    41  func WithBool(key string, value bool) *Logger {
    42  	return baseLogger.addField(ekaletter.FBool(key, value))
    43  }
    44  func WithInt(key string, value int) *Logger {
    45  	return baseLogger.addField(ekaletter.FInt(key, value))
    46  }
    47  func WithInt8(key string, value int8) *Logger {
    48  	return baseLogger.addField(ekaletter.FInt8(key, value))
    49  }
    50  func WithInt16(key string, value int16) *Logger {
    51  	return baseLogger.addField(ekaletter.FInt16(key, value))
    52  }
    53  func WithInt32(key string, value int32) *Logger {
    54  	return baseLogger.addField(ekaletter.FInt32(key, value))
    55  }
    56  func WithInt64(key string, value int64) *Logger {
    57  	return baseLogger.addField(ekaletter.FInt64(key, value))
    58  }
    59  func WithUint(key string, value uint) *Logger {
    60  	return baseLogger.addField(ekaletter.FUint(key, value))
    61  }
    62  func WithUint8(key string, value uint8) *Logger {
    63  	return baseLogger.addField(ekaletter.FUint8(key, value))
    64  }
    65  func WithUint16(key string, value uint16) *Logger {
    66  	return baseLogger.addField(ekaletter.FUint16(key, value))
    67  }
    68  func WithUint32(key string, value uint32) *Logger {
    69  	return baseLogger.addField(ekaletter.FUint32(key, value))
    70  }
    71  func WithUint64(key string, value uint64) *Logger {
    72  	return baseLogger.addField(ekaletter.FUint64(key, value))
    73  }
    74  func WithUintptr(key string, value uintptr) *Logger {
    75  	return baseLogger.addField(ekaletter.FUintptr(key, value))
    76  }
    77  func WithFloat32(key string, value float32) *Logger {
    78  	return baseLogger.addField(ekaletter.FFloat32(key, value))
    79  }
    80  func WithFloat64(key string, value float64) *Logger {
    81  	return baseLogger.addField(ekaletter.FFloat64(key, value))
    82  }
    83  func WithComplex64(key string, value complex64) *Logger {
    84  	return baseLogger.addField(ekaletter.FComplex64(key, value))
    85  }
    86  func WithComplex128(key string, value complex128) *Logger {
    87  	return baseLogger.addField(ekaletter.FComplex128(key, value))
    88  }
    89  func WithString(key string, value string) *Logger {
    90  	return baseLogger.addField(ekaletter.FString(key, value))
    91  }
    92  func WithStringFromBytes(key string, value []byte) *Logger {
    93  	return baseLogger.addField(ekaletter.FStringFromBytes(key, value))
    94  }
    95  func WithBoolp(key string, value *bool) *Logger {
    96  	return baseLogger.addField(ekaletter.FBoolp(key, value))
    97  }
    98  func WithIntp(key string, value *int) *Logger {
    99  	return baseLogger.addField(ekaletter.FIntp(key, value))
   100  }
   101  func WithInt8p(key string, value *int8) *Logger {
   102  	return baseLogger.addField(ekaletter.FInt8p(key, value))
   103  }
   104  func WithInt16p(key string, value *int16) *Logger {
   105  	return baseLogger.addField(ekaletter.FInt16p(key, value))
   106  }
   107  func WithInt32p(key string, value *int32) *Logger {
   108  	return baseLogger.addField(ekaletter.FInt32p(key, value))
   109  }
   110  func WithInt64p(key string, value *int64) *Logger {
   111  	return baseLogger.addField(ekaletter.FInt64p(key, value))
   112  }
   113  func WithUintp(key string, value *uint) *Logger {
   114  	return baseLogger.addField(ekaletter.FUintp(key, value))
   115  }
   116  func WithUint8p(key string, value *uint8) *Logger {
   117  	return baseLogger.addField(ekaletter.FUint8p(key, value))
   118  }
   119  func WithUint16p(key string, value *uint16) *Logger {
   120  	return baseLogger.addField(ekaletter.FUint16p(key, value))
   121  }
   122  func WithUint32p(key string, value *uint32) *Logger {
   123  	return baseLogger.addField(ekaletter.FUint32p(key, value))
   124  }
   125  func WithUint64p(key string, value *uint64) *Logger {
   126  	return baseLogger.addField(ekaletter.FUint64p(key, value))
   127  }
   128  func WithFloat32p(key string, value *float32) *Logger {
   129  	return baseLogger.addField(ekaletter.FFloat32p(key, value))
   130  }
   131  func WithFloat64p(key string, value *float64) *Logger {
   132  	return baseLogger.addField(ekaletter.FFloat64p(key, value))
   133  }
   134  func WithType(key string, value any) *Logger {
   135  	return baseLogger.addField(ekaletter.FType(key, value))
   136  }
   137  func WithStringer(key string, value fmt.Stringer) *Logger {
   138  	return baseLogger.addField(ekaletter.FStringer(key, value))
   139  }
   140  func WithAddr(key string, value any) *Logger {
   141  	return baseLogger.addField(ekaletter.FAddr(key, value))
   142  }
   143  func WithUnixFromStd(key string, value time.Time) *Logger {
   144  	return baseLogger.addField(ekaletter.FUnixFromStd(key, value))
   145  }
   146  func WithUnixNanoFromStd(key string, value time.Time) *Logger {
   147  	return baseLogger.addField(ekaletter.FUnixNanoFromStd(key, value))
   148  }
   149  func WithUnix(key string, value int64) *Logger {
   150  	return baseLogger.addField(ekaletter.FUnix(key, value))
   151  }
   152  func WithUnixNano(key string, value int64) *Logger {
   153  	return baseLogger.addField(ekaletter.FUnixNano(key, value))
   154  }
   155  func WithDuration(key string, value time.Duration) *Logger {
   156  	return baseLogger.addField(ekaletter.FDuration(key, value))
   157  }
   158  func WithArray(key string, value any) *Logger {
   159  	return baseLogger.addField(ekaletter.FArray(key, value))
   160  }
   161  func WithObject(key string, value any) *Logger {
   162  	return baseLogger.addField(ekaletter.FObject(key, value))
   163  }
   164  func WithMap(key string, value any) *Logger {
   165  	return baseLogger.addField(ekaletter.FMap(key, value))
   166  }
   167  func WithExtractedMap(key string, value map[string]any) *Logger {
   168  	return baseLogger.addField(ekaletter.FExtractedMap(key, value))
   169  }
   170  func WithAny(key string, value any) *Logger {
   171  	return baseLogger.addField(ekaletter.FAny(key, value))
   172  }
   173  func WithMany(fields ...ekaletter.LetterField) *Logger {
   174  	return baseLogger.addFields(fields)
   175  }
   176  func WithManyAny(fields ...any) *Logger {
   177  	return baseLogger.addFieldsParse(fields)
   178  }
   179  
   180  // ------------------------ CONDITIONAL LOGGING METHODS ----------------------- //
   181  // ---------------------------------------------------------------------------- //
   182  
   183  // If returns package Logger if cond is true, otherwise nop Logger is returned.
   184  // Thus it's useful to chaining methods - next methods in chaining will be done
   185  // only if cond is true.
   186  func If(cond bool) (defaultLogger *Logger) {
   187  	return baseLogger.If(cond)
   188  }
   189  
   190  // ------------------------------ UTILITY METHODS ----------------------------- //
   191  // ---------------------------------------------------------------------------- //
   192  
   193  // ReplaceIntegrator replaces Integrator for the package Logger to the passed one.
   194  //
   195  // Requirements:
   196  //   - Integrator is not nil (even typed nil), panic otherwise;
   197  //   - If Integrator is CommonIntegrator
   198  //     it must not be registered with some Logger before, panic otherwise;
   199  //   - If Integrator is CommonIntegrator
   200  //     it must have at least 1 registered io.Writer, panic otherwise.
   201  //
   202  // WARNING.
   203  // Replacing Integrator will drop all pre-encoded ekaletter.LetterField fields
   204  // that are might be added already to the current Integrator.
   205  func ReplaceIntegrator(newIntegrator Integrator) {
   206  	baseLogger.ReplaceIntegrator(newIntegrator)
   207  }
   208  
   209  // ReplaceEncoder is an alias for creating a new CommonIntegrator,
   210  // setting provided CI_Encoder for them and register it as a new integrator.
   211  // The synced stdout is used as writer if writer's set is empty.
   212  func ReplaceEncoder(cie CI_Encoder, writers ...io.Writer) {
   213  	ci := new(CommonIntegrator).WithEncoder(cie).WriteTo(writers...)
   214  	baseLogger.ReplaceIntegrator(ci)
   215  }