github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/logger/options.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/logger/options.go
    14  
    15  package logger
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  )
    21  
    22  type Option func(*Options)
    23  
    24  type Options struct {
    25  	// The logging level the logger should log at. default is `InfoLevel`
    26  	Level Level
    27  	// fields to always be logged
    28  	Fields map[string]interface{}
    29  	// It's common to set this to a file, or leave it default which is `os.Stderr`
    30  	Out io.Writer
    31  	// Caller skip frame count for file:line info
    32  	CallerSkipCount int
    33  	// Alternative options
    34  	Context context.Context
    35  }
    36  
    37  // WithFields set default fields for the logger
    38  func WithFields(fields map[string]interface{}) Option {
    39  	return func(args *Options) {
    40  		args.Fields = fields
    41  	}
    42  }
    43  
    44  // WithLevel set default level for the logger
    45  func WithLevel(level Level) Option {
    46  	return func(args *Options) {
    47  		args.Level = level
    48  	}
    49  }
    50  
    51  // WithOutput set default output writer for the logger
    52  func WithOutput(out io.Writer) Option {
    53  	return func(args *Options) {
    54  		args.Out = out
    55  	}
    56  }
    57  
    58  // WithCallerSkipCount set frame count to skip
    59  func WithCallerSkipCount(c int) Option {
    60  	return func(args *Options) {
    61  		args.CallerSkipCount = c
    62  	}
    63  }
    64  
    65  func SetOption(k, v interface{}) Option {
    66  	return func(o *Options) {
    67  		if o.Context == nil {
    68  			o.Context = context.Background()
    69  		}
    70  		o.Context = context.WithValue(o.Context, k, v)
    71  	}
    72  }