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