github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/implementation/logrus/level.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package logrus
    14  
    15  import (
    16  	"fmt"
    17  
    18  	"github.com/facebookincubator/go-belt/tool/logger/types"
    19  	"github.com/sirupsen/logrus"
    20  )
    21  
    22  // LevelToLogrus maps types.Level into logrus.Level
    23  func LevelToLogrus(level types.Level) logrus.Level {
    24  	switch level {
    25  	case types.LevelTrace:
    26  		return logrus.TraceLevel
    27  	case types.LevelDebug:
    28  		return logrus.DebugLevel
    29  	case types.LevelInfo:
    30  		return logrus.InfoLevel
    31  	case types.LevelWarning:
    32  		return logrus.WarnLevel
    33  	case types.LevelError:
    34  		return logrus.ErrorLevel
    35  	case types.LevelPanic:
    36  		return logrus.PanicLevel
    37  	case types.LevelFatal:
    38  		return logrus.FatalLevel
    39  	}
    40  	panic(fmt.Errorf("unexpected level: %v", level))
    41  }
    42  
    43  // LevelFromLogrus maps logrus.Level into types.Level
    44  func LevelFromLogrus(level logrus.Level) types.Level {
    45  	switch level {
    46  	case logrus.TraceLevel:
    47  		return types.LevelTrace
    48  	case logrus.DebugLevel:
    49  		return types.LevelDebug
    50  	case logrus.InfoLevel:
    51  		return types.LevelInfo
    52  	case logrus.WarnLevel:
    53  		return types.LevelWarning
    54  	case logrus.ErrorLevel:
    55  		return types.LevelError
    56  	case logrus.PanicLevel:
    57  		return types.LevelPanic
    58  	case logrus.FatalLevel:
    59  		return types.LevelFatal
    60  	}
    61  	panic(fmt.Errorf("unexpected level: %v", level))
    62  }