github.com/sunvim/utils@v0.1.0/log/log.go (about)

     1  // Copyright (c) 2021 mobus sunsc0220@gmail.com
     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 log
    16  
    17  import (
    18  	"os"
    19  	"time"
    20  
    21  	"github.com/rs/zerolog"
    22  )
    23  
    24  var _ zerolog.Logger
    25  
    26  type Level int8
    27  
    28  const (
    29  	// DebugLevel defines debug log level.
    30  	DebugLevel Level = iota
    31  	// InfoLevel defines info log level.
    32  	InfoLevel
    33  	// WarnLevel defines warn log level.
    34  	WarnLevel
    35  	// ErrorLevel defines error log level.
    36  	ErrorLevel
    37  	// FatalLevel defines fatal log level.
    38  	FatalLevel
    39  	// PanicLevel defines panic log level.
    40  	PanicLevel
    41  	// NoLevel defines an absent log level.
    42  	NoLevel
    43  	// Disabled disables the logger.
    44  	Disabled
    45  
    46  	// TraceLevel defines trace log level.
    47  	TraceLevel Level = -1
    48  	// Values less than TraceLevel are handled as numbers.
    49  )
    50  
    51  func NewLogger() zerolog.Logger {
    52  	output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
    53  	return zerolog.New(output).With().Timestamp().Logger()
    54  }
    55  
    56  func SetLogLevel(l Level) {
    57  	switch l {
    58  	default:
    59  		zerolog.SetGlobalLevel(zerolog.InfoLevel)
    60  	case DebugLevel:
    61  		zerolog.SetGlobalLevel(zerolog.DebugLevel)
    62  	case InfoLevel:
    63  		zerolog.SetGlobalLevel(zerolog.InfoLevel)
    64  	case WarnLevel:
    65  		zerolog.SetGlobalLevel(zerolog.WarnLevel)
    66  	case ErrorLevel:
    67  		zerolog.SetGlobalLevel(zerolog.ErrorLevel)
    68  	case FatalLevel:
    69  		zerolog.SetGlobalLevel(zerolog.FatalLevel)
    70  	case PanicLevel:
    71  		zerolog.SetGlobalLevel(zerolog.PanicLevel)
    72  	case NoLevel:
    73  		zerolog.SetGlobalLevel(zerolog.NoLevel)
    74  	case Disabled:
    75  		zerolog.SetGlobalLevel(zerolog.Disabled)
    76  	case TraceLevel:
    77  		zerolog.SetGlobalLevel(zerolog.TraceLevel)
    78  	}
    79  
    80  }