github.com/openshift-online/ocm-sdk-go@v0.1.473/logging/glog_logger.go (about) 1 /* 2 Copyright (c) 2018 Red Hat, Inc. 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 // This file contains a logger that uses the `glog` package. 18 19 package logging 20 21 import ( 22 "context" 23 "fmt" 24 "os" 25 26 "github.com/golang/glog" 27 ) 28 29 // GlogLoggerBuilder contains the configuration and logic needed to build a logger that uses the 30 // glog V mechanism. Don't create instances of this type directly, use the NewGlogLoggerBuilder 31 // function instead. 32 type GlogLoggerBuilder struct { 33 debugV glog.Level 34 infoV glog.Level 35 warnV glog.Level 36 errorV glog.Level 37 } 38 39 // GlogLogger is a logger that uses the glog V mechanism. 40 type GlogLogger struct { 41 debugV glog.Level 42 infoV glog.Level 43 warnV glog.Level 44 errorV glog.Level 45 } 46 47 // NewGlogLoggerBuilder creates a builder that uses the glog V mechanism. By default errors, 48 // warnings and information messages will be written to the log if the level is 0 or greater, and 49 // debug messages will be written if the level is 1 or greater. This can be changed using the 50 // ErrorV, WarnV, InfoV and DebugV methods of the builder. For example, to write errors and warnings 51 // for level 0, information messages for level 1, and debug messages for level 2, you can create the 52 // logger like this: 53 // 54 // logger, err := client.NewGlobLoggerBuilder(). 55 // ErrorV(0). 56 // WarnV(0). 57 // InfoV(1). 58 // DebugV(2). 59 // Build() 60 // 61 // Once the logger is created these settings can't be changed. 62 func NewGlogLoggerBuilder() *GlogLoggerBuilder { 63 // Allocate the object: 64 builder := new(GlogLoggerBuilder) 65 66 // Set default values: 67 builder.debugV = 1 68 builder.infoV = 0 69 builder.warnV = 0 70 builder.errorV = 0 71 72 return builder 73 } 74 75 // DebugV sets the V value that will be used for debug messages. 76 func (b *GlogLoggerBuilder) DebugV(v glog.Level) *GlogLoggerBuilder { 77 b.debugV = v 78 return b 79 } 80 81 // InfoV sets the V value that will be used for info messages. 82 func (b *GlogLoggerBuilder) InfoV(v glog.Level) *GlogLoggerBuilder { 83 b.infoV = v 84 return b 85 } 86 87 // WarnV sets the V value that will be used for warn messages. 88 func (b *GlogLoggerBuilder) WarnV(v glog.Level) *GlogLoggerBuilder { 89 b.warnV = v 90 return b 91 } 92 93 // ErrorV sets the V value that will be used for error messages. 94 func (b *GlogLoggerBuilder) ErrorV(v glog.Level) *GlogLoggerBuilder { 95 b.errorV = v 96 return b 97 } 98 99 // Build creates a new logger using the configuration stored in the builder. 100 func (b *GlogLoggerBuilder) Build() (logger *GlogLogger, err error) { 101 // Allocate and populate the object: 102 logger = new(GlogLogger) 103 logger.debugV = b.debugV 104 logger.infoV = b.infoV 105 logger.warnV = b.warnV 106 logger.errorV = b.errorV 107 108 return 109 } 110 111 // DebugEnabled returns true iff the debug level is enabled. 112 func (l *GlogLogger) DebugEnabled() bool { 113 return bool(glog.V(l.debugV)) 114 } 115 116 // InfoEnabled returns true iff the information level is enabled. 117 func (l *GlogLogger) InfoEnabled() bool { 118 return bool(glog.V(l.infoV)) 119 } 120 121 // WarnEnabled returns true iff the warning level is enabled. 122 func (l *GlogLogger) WarnEnabled() bool { 123 return bool(glog.V(l.warnV)) 124 } 125 126 // ErrorEnabled returns true iff the error level is enabled. 127 func (l *GlogLogger) ErrorEnabled() bool { 128 return bool(glog.V(l.errorV)) 129 } 130 131 // Debug sends to the log a debug message formatted using the fmt.Sprintf function and the given 132 // format and arguments. 133 func (l *GlogLogger) Debug(ctx context.Context, format string, args ...interface{}) { 134 if glog.V(l.debugV) { 135 msg := fmt.Sprintf(format, args...) 136 glog.InfoDepth(1, msg) 137 } 138 } 139 140 // Info sends to the log an information message formatted using the fmt.Sprintf function and the 141 // given format and arguments. 142 func (l *GlogLogger) Info(ctx context.Context, format string, args ...interface{}) { 143 if glog.V(l.infoV) { 144 msg := fmt.Sprintf(format, args...) 145 glog.InfoDepth(1, msg) 146 } 147 } 148 149 // Warn sends to the log a warning message formatted using the fmt.Sprintf function and the given 150 // format and arguments. 151 func (l *GlogLogger) Warn(ctx context.Context, format string, args ...interface{}) { 152 if glog.V(l.warnV) { 153 msg := fmt.Sprintf(format, args...) 154 glog.WarningDepth(1, msg) 155 } 156 } 157 158 // Error sends to the log an error message formatted using the fmt.Sprintf function and the given 159 // format and arguments. 160 func (l *GlogLogger) Error(ctx context.Context, format string, args ...interface{}) { 161 if glog.V(l.errorV) { 162 msg := fmt.Sprintf(format, args...) 163 glog.ErrorDepth(1, msg) 164 } 165 } 166 167 // Fatal sends to the log an error message formatted using the fmt.Sprintf function and the given 168 // format and arguments. After that it will os.Exit(1) 169 // This level is always enabled 170 func (l *GlogLogger) Fatal(ctx context.Context, format string, args ...interface{}) { 171 msg := fmt.Sprintf(format, args...) 172 // #nosec G104 173 glog.ErrorDepth(1, msg) 174 os.Exit(1) 175 }