internal_logging.go (2420B)
1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package global // import "go.opentelemetry.io/otel/internal/global" 16 17 import ( 18 "log" 19 "os" 20 "sync/atomic" 21 "unsafe" 22 23 "github.com/go-logr/logr" 24 "github.com/go-logr/stdr" 25 ) 26 27 // globalLogger is the logging interface used within the otel api and sdk provide details of the internals. 28 // 29 // The default logger uses stdr which is backed by the standard `log.Logger` 30 // interface. This logger will only show messages at the Error Level. 31 var globalLogger unsafe.Pointer 32 33 func init() { 34 SetLogger(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))) 35 } 36 37 // SetLogger overrides the globalLogger with l. 38 // 39 // To see Warn messages use a logger with `l.V(1).Enabled() == true` 40 // To see Info messages use a logger with `l.V(4).Enabled() == true` 41 // To see Debug messages use a logger with `l.V(8).Enabled() == true`. 42 func SetLogger(l logr.Logger) { 43 atomic.StorePointer(&globalLogger, unsafe.Pointer(&l)) 44 } 45 46 func getLogger() logr.Logger { 47 return *(*logr.Logger)(atomic.LoadPointer(&globalLogger)) 48 } 49 50 // Info prints messages about the general state of the API or SDK. 51 // This should usually be less than 5 messages a minute. 52 func Info(msg string, keysAndValues ...interface{}) { 53 getLogger().V(4).Info(msg, keysAndValues...) 54 } 55 56 // Error prints messages about exceptional states of the API or SDK. 57 func Error(err error, msg string, keysAndValues ...interface{}) { 58 getLogger().Error(err, msg, keysAndValues...) 59 } 60 61 // Debug prints messages about all internal changes in the API or SDK. 62 func Debug(msg string, keysAndValues ...interface{}) { 63 getLogger().V(8).Info(msg, keysAndValues...) 64 } 65 66 // Warn prints messages about warnings in the API or SDK. 67 // Not an error but is likely more important than an informational event. 68 func Warn(msg string, keysAndValues ...interface{}) { 69 getLogger().V(1).Info(msg, keysAndValues...) 70 }