github.com/klaytn/klaytn@v1.12.1/log/interface.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package log
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"runtime"
    24  )
    25  
    26  const module = "module"
    27  const (
    28  	ZapLogger     = "zap"
    29  	Log15Logger   = "log15"
    30  	DefaultLogger = Log15Logger
    31  )
    32  
    33  var baseLogger Logger
    34  
    35  type Logger interface {
    36  	NewWith(keysAndValues ...interface{}) Logger
    37  	newModuleLogger(mi ModuleID) Logger
    38  	Trace(msg string, keysAndValues ...interface{})
    39  	Debug(msg string, keysAndValues ...interface{})
    40  	Info(msg string, keysAndValues ...interface{})
    41  	Warn(msg string, keysAndValues ...interface{})
    42  	Error(msg string, keysAndValues ...interface{})
    43  	ErrorWithStack(msg string, keysAndValues ...interface{})
    44  	Crit(msg string, keysAndValues ...interface{})
    45  	CritWithStack(msg string, keysAndValues ...interface{})
    46  
    47  	// GetHandler gets the handler associated with the logger.
    48  	GetHandler() Handler
    49  	// SetHandler updates the logger to write records to the specified handler.
    50  	SetHandler(h Handler)
    51  }
    52  
    53  func init() {
    54  	root.SetHandler(DiscardHandler())
    55  	SetBaseLogger()
    56  }
    57  
    58  func SetBaseLogger() {
    59  	switch DefaultLogger {
    60  	case ZapLogger:
    61  		baseLogger = genBaseLoggerZap()
    62  	case Log15Logger:
    63  		baseLogger = root
    64  	default:
    65  		baseLogger = genBaseLoggerZap()
    66  	}
    67  }
    68  
    69  func NewModuleLogger(mi ModuleID) Logger {
    70  	newLogger := baseLogger.newModuleLogger(mi)
    71  	return newLogger
    72  }
    73  
    74  // Fatalf formats a message to standard error and exits the program.
    75  // The message is also printed to standard output if standard error
    76  // is redirected to a different file.
    77  func Fatalf(format string, args ...interface{}) {
    78  	w := io.MultiWriter(os.Stdout, os.Stderr)
    79  	if runtime.GOOS == "windows" {
    80  		// The SameFile check below doesn't work on Windows.
    81  		// stdout is unlikely to get redirected though, so just print there.
    82  		w = os.Stdout
    83  	} else {
    84  		outf, _ := os.Stdout.Stat()
    85  		errf, _ := os.Stderr.Stat()
    86  		if outf != nil && errf != nil && os.SameFile(outf, errf) {
    87  			w = os.Stderr
    88  		}
    89  	}
    90  	fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
    91  	os.Exit(1)
    92  }