github.com/blend/go-sdk@v1.20220411.3/logger/option.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  	"io"
    12  
    13  	"github.com/blend/go-sdk/env"
    14  )
    15  
    16  // Option is a logger option.
    17  type Option func(*Logger) error
    18  
    19  // OptConfig sets the logger based on a config.
    20  func OptConfig(cfg Config) Option {
    21  	return func(l *Logger) error {
    22  		l.Formatter = cfg.Formatter()
    23  		l.Flags = NewFlags(cfg.FlagsOrDefault()...)
    24  		l.Writable = NewFlags(cfg.WritableOrDefault()...)
    25  		l.Scopes = NewScopes(cfg.ScopesOrDefault()...)
    26  		l.WritableScopes = NewScopes(cfg.WritableScopesOrDefault()...)
    27  		return nil
    28  	}
    29  }
    30  
    31  // OptConfigFromEnv sets the logger based on a config read from the environment.
    32  // It will panic if there is an erro.
    33  func OptConfigFromEnv() Option {
    34  	return func(l *Logger) error {
    35  		var cfg Config
    36  		if err := env.Env().ReadInto(&cfg); err != nil {
    37  			return err
    38  		}
    39  		l.Formatter = cfg.Formatter()
    40  		l.Flags = NewFlags(cfg.FlagsOrDefault()...)
    41  		l.Writable = NewFlags(cfg.WritableOrDefault()...)
    42  		l.Scopes = NewScopes(cfg.ScopesOrDefault()...)
    43  		l.WritableScopes = NewScopes(cfg.WritableScopesOrDefault()...)
    44  		return nil
    45  	}
    46  }
    47  
    48  /*
    49  OptOutput sets the output writer for the logger.
    50  
    51  It will wrap the output with a synchronizer if it's not already wrapped. You can also use this option to "unset" the output by passing in nil.
    52  
    53  To set the output to be both stdout and a file, use the following:
    54  
    55  	file, _ := os.Open("app.log")
    56  	combined := io.MultiWriter(os.Stdout, file)
    57  	log := logger.New(logger.OptOutput(combined))
    58  
    59  */
    60  func OptOutput(output io.Writer) Option {
    61  	return func(l *Logger) error {
    62  		if output != nil {
    63  			l.Output = NewInterlockedWriter(output)
    64  		} else {
    65  			l.Output = nil
    66  		}
    67  		return nil
    68  	}
    69  }
    70  
    71  // OptPath sets an initial logger context path.
    72  //
    73  // This is useful if you want to label a logger to differentiate areas of an application
    74  // but re-use the existing logger.
    75  func OptPath(path ...string) Option {
    76  	return func(l *Logger) error { l.Scope.Path = path; return nil }
    77  }
    78  
    79  // OptLabels sets an initial logger scope labels.
    80  // This is useful if you want to add extra information for events by default (like environment).
    81  func OptLabels(labels ...Labels) Option {
    82  	return func(l *Logger) error { l.Scope.Labels = CombineLabels(labels...); return nil }
    83  }
    84  
    85  // OptJSON sets the output formatter for the logger as json.
    86  func OptJSON(opts ...JSONOutputFormatterOption) Option {
    87  	return func(l *Logger) error { l.Formatter = NewJSONOutputFormatter(opts...); return nil }
    88  }
    89  
    90  // OptText sets the output formatter for the logger as json.
    91  func OptText(opts ...TextOutputFormatterOption) Option {
    92  	return func(l *Logger) error { l.Formatter = NewTextOutputFormatter(opts...); return nil }
    93  }
    94  
    95  // OptFormatter sets the output formatter.
    96  func OptFormatter(formatter WriteFormatter) Option {
    97  	return func(l *Logger) error { l.Formatter = formatter; return nil }
    98  }
    99  
   100  // OptFlags sets the flags on the logger.
   101  func OptFlags(flags *Flags) Option {
   102  	return func(l *Logger) error { l.Flags = flags; return nil }
   103  }
   104  
   105  // OptWritable sets the writable flags on the logger.
   106  func OptWritable(flags *Flags) Option {
   107  	return func(l *Logger) error { l.Writable = flags; return nil }
   108  }
   109  
   110  // OptScopes sets the scopes on the logger.
   111  func OptScopes(scopes *Scopes) Option {
   112  	return func(l *Logger) error { l.Scopes = scopes; return nil }
   113  }
   114  
   115  // OptWritableScopes sets the writable scopes on the logger.
   116  func OptWritableScopes(scopes *Scopes) Option {
   117  	return func(l *Logger) error { l.WritableScopes = scopes; return nil }
   118  }
   119  
   120  // OptAll sets all flags enabled on the logger by default.
   121  func OptAll() Option {
   122  	return func(l *Logger) error { l.Flags.SetAll(); return nil }
   123  }
   124  
   125  // OptAllWritable sets all flags enabled on the logger by default.
   126  func OptAllWritable() Option {
   127  	return func(l *Logger) error { l.Writable.SetAll(); return nil }
   128  }
   129  
   130  // OptAllScopes sets all scopes enabled on the logger by default.
   131  func OptAllScopes() Option {
   132  	return func(l *Logger) error { l.Scopes.SetAll(); return nil }
   133  }
   134  
   135  // OptAllWritableScopes sets all scopes for writing enabled on the logger by default.
   136  func OptAllWritableScopes() Option {
   137  	return func(l *Logger) error { l.WritableScopes.SetAll(); return nil }
   138  }
   139  
   140  // OptNone sets no flags enabled on the logger by default.
   141  func OptNone() Option {
   142  	return func(l *Logger) error { l.Flags.SetNone(); return nil }
   143  }
   144  
   145  // OptNoneWritable sets no flags enabled for writing on the logger by default.
   146  func OptNoneWritable() Option {
   147  	return func(l *Logger) error { l.Writable.SetNone(); return nil }
   148  }
   149  
   150  // OptNoneScopes sets no scopes enabled on the logger by default.
   151  func OptNoneScopes() Option {
   152  	return func(l *Logger) error { l.Scopes.SetNone(); return nil }
   153  }
   154  
   155  // OptNoneWritableScopes sets no scopes enabled for writing on the logger by default.
   156  func OptNoneWritableScopes() Option {
   157  	return func(l *Logger) error { l.WritableScopes.SetNone(); return nil }
   158  }
   159  
   160  // OptEnabled sets enabled flags on the logger.
   161  func OptEnabled(flags ...string) Option {
   162  	return func(l *Logger) error { l.Flags.Enable(flags...); return nil }
   163  }
   164  
   165  // OptEnabledWritable sets enabled writable flags on the logger.
   166  func OptEnabledWritable(flags ...string) Option {
   167  	return func(l *Logger) error { l.Writable.Enable(flags...); return nil }
   168  }
   169  
   170  // OptEnabledScopes sets enabled scopes on the logger.
   171  func OptEnabledScopes(scopes ...string) Option {
   172  	return func(l *Logger) error { l.Scopes.Enable(scopes...); return nil }
   173  }
   174  
   175  // OptEnabledWritableScopes sets enabled writable scopes on the logger.
   176  func OptEnabledWritableScopes(scopes ...string) Option {
   177  	return func(l *Logger) error { l.WritableScopes.Enable(scopes...); return nil }
   178  }
   179  
   180  // OptDisabled sets disabled flags on the logger.
   181  func OptDisabled(flags ...string) Option {
   182  	return func(l *Logger) error { l.Flags.Disable(flags...); return nil }
   183  }
   184  
   185  // OptDisabledWritable sets disabled flags on the logger.
   186  func OptDisabledWritable(flags ...string) Option {
   187  	return func(l *Logger) error { l.Writable.Disable(flags...); return nil }
   188  }
   189  
   190  // OptDisabledScopes sets disabled scopes on the logger.
   191  func OptDisabledScopes(scopes ...string) Option {
   192  	return func(l *Logger) error { l.Scopes.Disable(scopes...); return nil }
   193  }
   194  
   195  // OptDisabledWritableScopes sets disabled flags on the logger.
   196  func OptDisabledWritableScopes(scopes ...string) Option {
   197  	return func(l *Logger) error { l.WritableScopes.Disable(scopes...); return nil }
   198  }