github.com/openshift-online/ocm-sdk-go@v0.1.473/logging/go_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 Go `log` package. 18 19 package logging 20 21 import ( 22 "context" 23 "fmt" 24 "log" 25 "os" 26 ) 27 28 // GoLoggerBuilder contains the configuration and logic needed to build a logger that uses the Go 29 // `log` package. Don't create instances of this type directly, use the NewGoLoggerBuilder function 30 // instead. 31 type GoLoggerBuilder struct { 32 debugEnabled bool 33 infoEnabled bool 34 warnEnabled bool 35 errorEnabled bool 36 } 37 38 // GoLogger is a logger that uses the Go `log` package. 39 type GoLogger struct { 40 debugEnabled bool 41 infoEnabled bool 42 warnEnabled bool 43 errorEnabled bool 44 } 45 46 // NewGoLoggerBuilder creates a builder that knows how to build a logger that uses the Go `log` 47 // package. By default these loggers will have enabled the information, warning and error levels 48 func NewGoLoggerBuilder() *GoLoggerBuilder { 49 // Allocate the object: 50 builder := new(GoLoggerBuilder) 51 52 // Set default values: 53 builder.debugEnabled = false 54 builder.infoEnabled = true 55 builder.warnEnabled = true 56 builder.errorEnabled = true 57 58 return builder 59 } 60 61 // Debug enables or disables the debug level. 62 func (b *GoLoggerBuilder) Debug(flag bool) *GoLoggerBuilder { 63 b.debugEnabled = flag 64 return b 65 } 66 67 // Info enables or disables the information level. 68 func (b *GoLoggerBuilder) Info(flag bool) *GoLoggerBuilder { 69 b.infoEnabled = flag 70 return b 71 } 72 73 // Warn enables or disables the warning level. 74 func (b *GoLoggerBuilder) Warn(flag bool) *GoLoggerBuilder { 75 b.warnEnabled = flag 76 return b 77 } 78 79 // Error enables or disables the error level. 80 func (b *GoLoggerBuilder) Error(flag bool) *GoLoggerBuilder { 81 b.errorEnabled = flag 82 return b 83 } 84 85 // Build creates a new logger using the configuration stored in the builder. 86 func (b *GoLoggerBuilder) Build() (logger *GoLogger, err error) { 87 // Allocate and populate the object: 88 logger = new(GoLogger) 89 logger.debugEnabled = b.debugEnabled 90 logger.infoEnabled = b.infoEnabled 91 logger.warnEnabled = b.warnEnabled 92 logger.errorEnabled = b.errorEnabled 93 94 return 95 } 96 97 // DebugEnabled returns true iff the debug level is enabled. 98 func (l *GoLogger) DebugEnabled() bool { 99 return l.debugEnabled 100 } 101 102 // InfoEnabled returns true iff the information level is enabled. 103 func (l *GoLogger) InfoEnabled() bool { 104 return l.infoEnabled 105 } 106 107 // WarnEnabled returns true iff the warning level is enabled. 108 func (l *GoLogger) WarnEnabled() bool { 109 return l.warnEnabled 110 } 111 112 // ErrorEnabled returns true iff the error level is enabled. 113 func (l *GoLogger) ErrorEnabled() bool { 114 return l.errorEnabled 115 } 116 117 // Debug sends to the log a debug message formatted using the fmt.Sprintf function and the given 118 // format and arguments. 119 func (l *GoLogger) Debug(ctx context.Context, format string, args ...interface{}) { 120 if l.debugEnabled { 121 msg := fmt.Sprintf(format, args...) 122 // #nosec G104 123 _ = log.Output(1, msg) 124 } 125 } 126 127 // Info sends to the log an information message formatted using the fmt.Sprintf function and the 128 // given format and arguments. 129 func (l *GoLogger) Info(ctx context.Context, format string, args ...interface{}) { 130 if l.infoEnabled { 131 msg := fmt.Sprintf(format, args...) 132 // #nosec G104 133 _ = log.Output(1, msg) 134 } 135 } 136 137 // Warn sends to the log a warning message formatted using the fmt.Sprintf function and the given 138 // format and arguments. 139 func (l *GoLogger) Warn(ctx context.Context, format string, args ...interface{}) { 140 if l.warnEnabled { 141 msg := fmt.Sprintf(format, args...) 142 // #nosec G104 143 _ = log.Output(1, msg) 144 } 145 } 146 147 // Error sends to the log an error message formatted using the fmt.Sprintf function and the given 148 // format and arguments. 149 func (l *GoLogger) Error(ctx context.Context, format string, args ...interface{}) { 150 if l.errorEnabled { 151 msg := fmt.Sprintf(format, args...) 152 // #nosec G104 153 _ = log.Output(1, msg) 154 } 155 } 156 157 // Fatal sends to the log an error message formatted using the fmt.Sprintf function and the given 158 // format and arguments. After that it will os.Exit(1) 159 // This level is always enabled 160 func (l *GoLogger) Fatal(ctx context.Context, format string, args ...interface{}) { 161 msg := fmt.Sprintf(format, args...) 162 // #nosec G104 163 _ = log.Output(1, msg) 164 os.Exit(1) 165 }