github.com/braveheart12/insolar-09-08-19@v0.8.7/log/log.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package log 18 19 import ( 20 "io" 21 stdlog "log" 22 "strings" 23 "time" 24 25 "github.com/pkg/errors" 26 27 "github.com/insolar/insolar/configuration" 28 "github.com/insolar/insolar/core" 29 ) 30 31 const defaultSkipCallNumber = 3 32 const timestampFormat = time.RFC3339Nano 33 34 // NewLog creates logger instance with particular configuration 35 func NewLog(cfg configuration.Log) (core.Logger, error) { 36 var logger core.Logger 37 var err error 38 39 switch strings.ToLower(cfg.Adapter) { 40 case "logrus": 41 logger, err = newLogrusAdapter(cfg) 42 case "zerolog": 43 logger, err = newZerologAdapter(cfg) 44 default: 45 err = errors.New("unknown adapter") 46 } 47 48 if err != nil { 49 return nil, errors.Wrap(err, "invalid logger config") 50 } 51 52 err = logger.SetLevel(cfg.Level) 53 if err != nil { 54 return nil, errors.Wrap(err, "invalid logger config") 55 } 56 57 return logger, nil 58 } 59 60 // GlobalLogger creates global logger with correct skipCallNumber 61 // TODO: make it private again 62 var GlobalLogger = func() core.Logger { 63 holder := configuration.NewHolder().MustInit(false) 64 logger, err := NewLog(holder.Configuration.Log) 65 if err != nil { 66 stdlog.Println("warning:", err.Error()) 67 } 68 69 //logger.skipCallNumber = defaultSkipCallNumber + 1 70 if err := logger.SetLevel(holder.Configuration.Log.Level); err != nil { 71 stdlog.Println("warning:", err.Error()) 72 } 73 return logger 74 }() 75 76 func SetGlobalLogger(logger core.Logger) { 77 GlobalLogger = logger 78 } 79 80 // SetLevel lets log level for global logger 81 func SetLevel(level string) error { 82 return GlobalLogger.SetLevel(level) 83 } 84 85 // Debug logs a message at level Debug to the global logger. 86 func Debug(args ...interface{}) { 87 GlobalLogger.Debug(args...) 88 } 89 90 // Debugf logs a message at level Debug to the global logger. 91 func Debugf(format string, args ...interface{}) { 92 GlobalLogger.Debugf(format, args...) 93 } 94 95 // Info logs a message at level Info to the global logger. 96 func Info(args ...interface{}) { 97 GlobalLogger.Info(args...) 98 } 99 100 // Infof logs a message at level Info to the global logger. 101 func Infof(format string, args ...interface{}) { 102 GlobalLogger.Infof(format, args...) 103 } 104 105 // Warn logs a message at level Warn to the global logger. 106 func Warn(args ...interface{}) { 107 GlobalLogger.Warn(args...) 108 } 109 110 // Warnf logs a message at level Warn to the global logger. 111 func Warnf(format string, args ...interface{}) { 112 GlobalLogger.Warnf(format, args...) 113 } 114 115 // Error logs a message at level Error to the global logger. 116 func Error(args ...interface{}) { 117 GlobalLogger.Error(args...) 118 } 119 120 // Errorf logs a message at level Error to the global logger. 121 func Errorf(format string, args ...interface{}) { 122 GlobalLogger.Errorf(format, args...) 123 } 124 125 // Fatal logs a message at level Fatal to the global logger. 126 func Fatal(args ...interface{}) { 127 GlobalLogger.Fatal(args...) 128 } 129 130 // Fatalf logs a message at level Fatal to the global logger. 131 func Fatalf(format string, args ...interface{}) { 132 GlobalLogger.Fatalf(format, args...) 133 } 134 135 // Panic logs a message at level Panic to the global logger. 136 func Panic(args ...interface{}) { 137 GlobalLogger.Panic(args...) 138 } 139 140 // Panicf logs a message at level Panic to the global logger. 141 func Panicf(format string, args ...interface{}) { 142 GlobalLogger.Panicf(format, args...) 143 } 144 145 // SetOutput sets the output destination for the logger. 146 func SetOutput(w io.Writer) { 147 GlobalLogger.SetOutput(w) 148 }