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 22 "github.com/go-logr/logr" 23 "github.com/go-logr/stdr" 24 ) 25 26 // globalLogger is the logging interface used within the otel api and sdk provide details of the internals. 27 // 28 // The default logger uses stdr which is backed by the standard `log.Logger` 29 // interface. This logger will only show messages at the Error Level. 30 var globalLogger atomic.Pointer[logr.Logger] 31 32 func init() { 33 SetLogger(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))) 34 } 35 36 // SetLogger overrides the globalLogger with l. 37 // 38 // To see Warn messages use a logger with `l.V(1).Enabled() == true` 39 // To see Info messages use a logger with `l.V(4).Enabled() == true` 40 // To see Debug messages use a logger with `l.V(8).Enabled() == true`. 41 func SetLogger(l logr.Logger) { 42 globalLogger.Store(&l) 43 } 44 45 func getLogger() logr.Logger { 46 return *globalLogger.Load() 47 } 48 49 // Info prints messages about the general state of the API or SDK. 50 // This should usually be less than 5 messages a minute. 51 func Info(msg string, keysAndValues ...interface{}) { 52 getLogger().V(4).Info(msg, keysAndValues...) 53 } 54 55 // Error prints messages about exceptional states of the API or SDK. 56 func Error(err error, msg string, keysAndValues ...interface{}) { 57 getLogger().Error(err, msg, keysAndValues...) 58 } 59 60 // Debug prints messages about all internal changes in the API or SDK. 61 func Debug(msg string, keysAndValues ...interface{}) { 62 getLogger().V(8).Info(msg, keysAndValues...) 63 } 64 65 // Warn prints messages about warnings in the API or SDK. 66 // Not an error but is likely more important than an informational event. 67 func Warn(msg string, keysAndValues ...interface{}) { 68 getLogger().V(1).Info(msg, keysAndValues...) 69 } 70