github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/container/lxd/logger.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lxd 5 6 import ( 7 "bytes" 8 "fmt" 9 10 lxdLogger "github.com/canonical/lxd/shared/logger" 11 "github.com/juju/loggo" 12 ) 13 14 // lxdLogProxy proxies LXD's log calls through the juju logger. 15 type lxdLogProxy struct { 16 logger loggo.Logger 17 } 18 19 func (p *lxdLogProxy) render(msg string, ctx []lxdLogger.Ctx) string { 20 var result bytes.Buffer 21 result.WriteString(msg) 22 if len(ctx) > 0 { 23 result.WriteString(": ") 24 } 25 26 for _, c := range ctx { 27 var afterFirst bool 28 for k, v := range c { 29 if afterFirst { 30 result.WriteString(", ") 31 } 32 afterFirst = true 33 34 result.WriteString(k) 35 result.WriteString(": ") 36 result.WriteString(fmt.Sprintf("%v", v)) 37 } 38 } 39 40 return result.String() 41 } 42 43 func (p *lxdLogProxy) Trace(msg string, ctx ...lxdLogger.Ctx) { 44 p.logger.Tracef(p.render(msg, ctx)) 45 } 46 47 func (p *lxdLogProxy) Debug(msg string, ctx ...lxdLogger.Ctx) { 48 // NOTE(axw) the LXD client logs a lot of detail at 49 // "debug" level, which is its highest level of logging. 50 // We transform this to Trace, to avoid spamming our 51 // logs with too much information. 52 p.logger.Tracef(p.render(msg, ctx)) 53 } 54 55 func (p *lxdLogProxy) Info(msg string, ctx ...lxdLogger.Ctx) { 56 p.logger.Infof(p.render(msg, ctx)) 57 } 58 59 func (p *lxdLogProxy) Warn(msg string, ctx ...lxdLogger.Ctx) { 60 p.logger.Warningf(p.render(msg, ctx)) 61 } 62 63 func (p *lxdLogProxy) Error(msg string, ctx ...lxdLogger.Ctx) { 64 p.logger.Errorf(p.render(msg, ctx)) 65 } 66 67 func (p *lxdLogProxy) Crit(msg string, ctx ...lxdLogger.Ctx) { 68 p.logger.Criticalf(p.render(msg, ctx)) 69 } 70 71 func (p *lxdLogProxy) Fatal(msg string, ctx ...lxdLogger.Ctx) { 72 p.logger.Criticalf("Fatal: %s", p.render(msg, ctx)) 73 } 74 75 func (p *lxdLogProxy) Panic(msg string, ctx ...lxdLogger.Ctx) { 76 p.logger.Criticalf("Panic: %s", p.render(msg, ctx)) 77 } 78 79 func (p *lxdLogProxy) AddContext(_ lxdLogger.Ctx) lxdLogger.Logger { 80 return p 81 } 82 83 func init() { 84 lxdLogger.Log = &lxdLogProxy{ 85 logger: loggo.GetLogger("lxd"), 86 } 87 }