github.com/ssgreg/logf@v1.4.1/logfc/context.go (about)

     1  package logfc
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ssgreg/logf"
     7  )
     8  
     9  // New returns a new context.Context with the given logger associated with it.
    10  //
    11  // Note: the given logger will hide any other logger which
    12  // was associated with this context before.
    13  func New(parent context.Context, logger *logf.Logger) context.Context {
    14  	return logf.NewContext(parent, logger)
    15  }
    16  
    17  // Get returns the logf.Logger associated with ctx
    18  // or log.DisabledLogger() if no value is associated.
    19  // Successive calls to Get return the same result.
    20  func Get(ctx context.Context) *logf.Logger {
    21  	logger := logf.FromContext(ctx)
    22  	if logger == nil {
    23  		logger = logf.DisabledLogger()
    24  	}
    25  
    26  	return logger
    27  }
    28  
    29  // MustGet returns the logf.Logger associated with ctx
    30  // or panics if no value is associated.
    31  // Successive calls to MustGet return the same result.
    32  func MustGet(ctx context.Context) *logf.Logger {
    33  	logger := logf.FromContext(ctx)
    34  	if logger == nil {
    35  		panic("logfc: provided context has no logf.Logger associated")
    36  	}
    37  
    38  	return logger
    39  }
    40  
    41  // With returns a new context.Context with new logf.Logger inside
    42  // with provided fields appended to current logf.Logger associated with ctx
    43  // If there is no logf.Logger is associated with ctx, logf.DisabledLogger()
    44  // is used as a base logger.
    45  func With(ctx context.Context, fields ...logf.Field) context.Context {
    46  	return New(ctx, Get(ctx).With(fields...))
    47  }
    48  
    49  // MustWith returns a new context.Context with provided fields appended
    50  // to fields of logf.Logger associated with ctx
    51  // or panics if no logf.Logger is associated with ctx.
    52  func MustWith(ctx context.Context, fields ...logf.Field) context.Context {
    53  	return New(ctx, MustGet(ctx).With(fields...))
    54  }
    55  
    56  // WithName returns a new context.Context with a new logf.Logger
    57  // adding the given name to the original one.
    58  // If there is no logf.Logger is associated with ctx, logf.DisabledLogger()
    59  // is used as a base logger.
    60  // Name separator is a period.
    61  func WithName(ctx context.Context, name string) context.Context {
    62  	return New(ctx, Get(ctx).WithName(name))
    63  }
    64  
    65  // WithLevel returns a new context.Context with a new logf.Logger
    66  // with the given additional level checker.
    67  // If there is no logf.Logger is associated with ctx, logf.DisabledLogger()
    68  // is used as a base logger.
    69  func WithLevel(ctx context.Context, level logf.LevelCheckerGetter) context.Context {
    70  	return New(ctx, Get(ctx).WithLevel(level))
    71  }
    72  
    73  // MustWithName returns a new context.Context with a new logf.Logger
    74  // adding the given name to the original one
    75  // or panics if no logf.Logger is associated with ctx.
    76  // Name separator is a period.
    77  func MustWithName(ctx context.Context, name string) context.Context {
    78  	return New(ctx, MustGet(ctx).WithName(name))
    79  }
    80  
    81  // WithCaller returns a new context.Context with a new logf.Logger
    82  // that adds a special annotation parameters
    83  // to each logging message, such as the filename and line number of a caller.
    84  // If there is no logf.Logger is associated with ctx, logf.DisabledLogger()
    85  // is used as a base logger.
    86  func WithCaller(ctx context.Context) context.Context {
    87  	return New(ctx, Get(ctx).WithCaller())
    88  }
    89  
    90  // MustWithCaller returns a new context.Context with a logf.Logger
    91  // that adds a special annotation parameters
    92  // to each logging message, such as the filename and line number of a caller
    93  // or panics if no logf.Logger is associated with ctx.
    94  func MustWithCaller(ctx context.Context) context.Context {
    95  	return New(ctx, MustGet(ctx).WithCaller())
    96  }
    97  
    98  // WithCallerSkip returns a new context.Context with a new logf.Logger
    99  // that adds a special annotation parameters with additional n skipped frames
   100  // to each logging message, such as the filename and line number of a caller.
   101  // If there is no logf.Logger is associated with ctx, logf.DisabledLogger()
   102  // is used as a base logger.
   103  func WithCallerSkip(ctx context.Context, n int) context.Context {
   104  	return New(ctx, Get(ctx).WithCallerSkip(n))
   105  }
   106  
   107  // MustWithCallerSkip returns a new context.Context with a logf.Logger
   108  // that adds a special annotation parameters with additional n skipped frames
   109  // to each logging message, such as the filename and line number of a caller
   110  // or panics if no logf.Logger is associated with ctx.
   111  func MustWithCallerSkip(ctx context.Context, n int) context.Context {
   112  	return New(ctx, MustGet(ctx).WithCallerSkip(n))
   113  }
   114  
   115  // AtLevel calls the given fn if logging a message at the specified level
   116  // is enabled, passing a logf.LogFunc with the bound level
   117  // or does nothing if there is no logf.Logger associated with ctx.
   118  func AtLevel(ctx context.Context, level logf.Level, fn func(logf.LogFunc)) {
   119  	Get(ctx).AtLevel(level, fn)
   120  }
   121  
   122  // MustAtLevel calls the given fn if logging a message at the specified level
   123  // is enabled, passing a logf.LogFunc with the bound level
   124  // or panics if there is no logf.Logger associated with ctx.
   125  func MustAtLevel(ctx context.Context, level logf.Level, fn func(logf.LogFunc)) {
   126  	MustGet(ctx).AtLevel(level, fn)
   127  }
   128  
   129  // Debug logs a debug message with the given text, optional fields and
   130  // fields passed to the logf.Logger using With function
   131  // or logs nothing if no logf.Logger is associated with ctx.
   132  func Debug(ctx context.Context, text string, fs ...logf.Field) {
   133  	Get(ctx).WithCallerSkip(1).Debug(text, fs...)
   134  }
   135  
   136  // MustDebug logs a debug message with the given text, optional fields and
   137  // fields passed to the logf.Logger using With function
   138  // or panics if no logf.Logger is associated with ctx.
   139  func MustDebug(ctx context.Context, text string, fs ...logf.Field) {
   140  	MustGet(ctx).WithCallerSkip(1).Debug(text, fs...)
   141  }
   142  
   143  // Info logs an info message with the given text, optional fields and
   144  // fields passed to the logf.Logger using With function
   145  // or logs nothing if no logf.Logger is associated with ctx.
   146  func Info(ctx context.Context, text string, fs ...logf.Field) {
   147  	Get(ctx).WithCallerSkip(1).Info(text, fs...)
   148  }
   149  
   150  // MustInfo logs an info message with the given text, optional fields and
   151  // fields passed to the logf.Logger using With function
   152  // or panics if no logf.Logger is associated with ctx.
   153  func MustInfo(ctx context.Context, text string, fs ...logf.Field) {
   154  	MustGet(ctx).WithCallerSkip(1).Info(text, fs...)
   155  }
   156  
   157  // Warn logs a warning message with the given text, optional fields and
   158  // fields passed to the logf.Logger using With function
   159  // or logs nothing if no logf.Logger is associated with ctx.
   160  func Warn(ctx context.Context, text string, fs ...logf.Field) {
   161  	Get(ctx).WithCallerSkip(1).Warn(text, fs...)
   162  }
   163  
   164  // MustWarn logs a warning message with the given text, optional fields and
   165  // fields passed to the logf.Logger using With function
   166  // or panics if no logf.Logger is associated with ctx.
   167  func MustWarn(ctx context.Context, text string, fs ...logf.Field) {
   168  	MustGet(ctx).WithCallerSkip(1).Warn(text, fs...)
   169  }
   170  
   171  // Error logs an error message with the given text, optional fields and
   172  // fields passed to the logf.Logger using With function
   173  // or logs nothing if no logf.Logger is associated with ctx.
   174  func Error(ctx context.Context, text string, fs ...logf.Field) {
   175  	Get(ctx).WithCallerSkip(1).Error(text, fs...)
   176  }
   177  
   178  // MustError logs an error message with the given text, optional fields and
   179  // fields passed to the logf.Logger using With function
   180  // or panics if no logf.Logger is associated with ctx.
   181  func MustError(ctx context.Context, text string, fs ...logf.Field) {
   182  	MustGet(ctx).WithCallerSkip(1).Error(text, fs...)
   183  }