github.com/gagliardetto/solana-go@v1.11.0/zap-box/utils.go (about)

     1  // Copyright 2019 dfuse Platform Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package zapbox
    16  
    17  import (
    18  	"go.uber.org/zap"
    19  	"go.uber.org/zap/zapcore"
    20  )
    21  
    22  // WithLevel returns a new context derived from ctx
    23  // that has a logger that only logs messages at or above
    24  // the given level.
    25  //
    26  // *Important!* This does not work with all underlying core
    27  //              implementation. See https://github.com/uber-go/zap/issues/581#issuecomment-600641485
    28  //              for details.
    29  func WithLevel(level zapcore.Level) zap.Option {
    30  	return zap.WrapCore(func(core zapcore.Core) zapcore.Core {
    31  		return &coreWithLevel{
    32  			Core:  core,
    33  			level: level,
    34  		}
    35  	})
    36  }
    37  
    38  type coreWithLevel struct {
    39  	zapcore.Core
    40  	level zapcore.Level
    41  }
    42  
    43  func (c *coreWithLevel) Enabled(level zapcore.Level) bool {
    44  	return c.level.Enabled(level)
    45  }
    46  
    47  func (c *coreWithLevel) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
    48  	if !c.level.Enabled(e.Level) {
    49  		return ce
    50  	}
    51  
    52  	return ce.AddCore(e, c.Core)
    53  }
    54  
    55  func (c *coreWithLevel) With(fields []zap.Field) zapcore.Core {
    56  	return &coreWithLevel{
    57  		Core:  c.Core.With(fields),
    58  		level: c.level,
    59  	}
    60  }