github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/logger.go (about) 1 package hedera 2 3 /*- 4 * 5 * Hedera Go SDK 6 * 7 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 import ( 24 "fmt" 25 "os" 26 "time" 27 28 "github.com/rs/zerolog" 29 ) 30 31 type LogLevel string 32 33 const ( 34 LoggerLevelTrace LogLevel = "TRACE" 35 LoggerLevelDebug LogLevel = "DEBUG" 36 LoggerLevelInfo LogLevel = "INFO" 37 LoggerLevelWarn LogLevel = "WARN" 38 LoggerLevelError LogLevel = "ERROR" 39 LoggerLevelDisabled LogLevel = "DISABLED" 40 ) 41 42 type Logger interface { 43 SetSilent(isSilent bool) 44 SetLevel(level LogLevel) 45 SubLoggerWithLevel(level LogLevel) Logger 46 Error(msg string, keysAndValues ...interface{}) 47 Warn(msg string, keysAndValues ...interface{}) 48 Info(msg string, keysAndValues ...interface{}) 49 Debug(msg string, keysAndValues ...interface{}) 50 Trace(msg string, keysAndValues ...interface{}) 51 } 52 53 type DefaultLogger struct { 54 logger *zerolog.Logger 55 level LogLevel 56 } 57 58 func NewLogger(component string, level LogLevel) *DefaultLogger { 59 var logger zerolog.Logger 60 logger = zerolog.New(os.Stdout).With().Str("module", component).Timestamp().Logger() 61 62 if os.Getenv("HEDERA_SDK_GO_LOG_PRETTY") != "" { 63 // Pretty logging 64 output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} 65 logger = zerolog.New(output).With().Str("module", component).Timestamp().Logger() 66 } 67 68 logger = loggerForLevel(logger, level) 69 return &DefaultLogger{ 70 logger: &logger, 71 level: level, 72 } 73 } 74 75 func (l *DefaultLogger) SetSilent(isSilent bool) { 76 if isSilent { 77 logger := l.logger.Level(zerolog.Disabled) 78 l.logger = &logger 79 } else { 80 l.SetLevel(l.level) 81 } 82 } 83 84 func loggerForLevel(logger zerolog.Logger, level LogLevel) zerolog.Logger { 85 switch level { 86 case LoggerLevelTrace: 87 return logger.Level(zerolog.TraceLevel) 88 case LoggerLevelDebug: 89 return logger.Level(zerolog.DebugLevel) 90 case LoggerLevelInfo: 91 return logger.Level(zerolog.InfoLevel) 92 case LoggerLevelWarn: 93 return logger.Level(zerolog.WarnLevel) 94 case LoggerLevelError: 95 return logger.Level(zerolog.ErrorLevel) 96 default: 97 return logger.Level(zerolog.Disabled) 98 } 99 } 100 101 func (l *DefaultLogger) SetLevel(level LogLevel) { 102 l.level = level 103 logger := loggerForLevel(*l.logger, level) 104 l.logger = &logger 105 } 106 107 func (l *DefaultLogger) SubLoggerWithLevel(level LogLevel) Logger { 108 l.level = level 109 logger := loggerForLevel(*l.logger, level) 110 111 return &DefaultLogger{ 112 logger: &logger, 113 level: level, 114 } 115 } 116 117 func (l *DefaultLogger) Warn(msg string, keysAndValues ...interface{}) { 118 addFields(l.logger.Warn(), msg, keysAndValues...) 119 } 120 121 func (l *DefaultLogger) Error(msg string, keysAndValues ...interface{}) { 122 addFields(l.logger.Error(), msg, keysAndValues...) 123 } 124 125 func (l *DefaultLogger) Trace(msg string, keysAndValues ...interface{}) { 126 addFields(l.logger.Trace(), msg, keysAndValues...) 127 } 128 129 func (l *DefaultLogger) Debug(msg string, keysAndValues ...interface{}) { 130 addFields(l.logger.Debug(), msg, keysAndValues...) 131 } 132 133 func (l *DefaultLogger) Info(msg string, keysAndValues ...interface{}) { 134 addFields(l.logger.Info(), msg, keysAndValues...) 135 } 136 137 func argsToFields(keysAndValues ...interface{}) map[string]interface{} { 138 fields := make(map[string]interface{}) 139 for i := 0; i < len(keysAndValues); i += 2 { 140 fields[fmt.Sprint(keysAndValues[i])] = keysAndValues[i+1] 141 } 142 return fields 143 } 144 145 func addFields(event *zerolog.Event, msg string, keysAndValues ...interface{}) { 146 for key, value := range argsToFields(keysAndValues...) { 147 switch v := value.(type) { 148 case string: 149 event.Str(key, v) 150 case int64: 151 event.Int64(key, v) 152 case time.Duration: 153 event.Dur(key, v) 154 default: 155 event.Str(key, fmt.Sprint(value)) 156 } 157 } 158 159 event.Msg(msg) 160 }