github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/hclog_adapter.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package plugin 5 6 import ( 7 "fmt" 8 "io" 9 "log" 10 "strings" 11 12 "github.com/hashicorp/go-hclog" 13 14 "github.com/mattermost/mattermost-server/v5/mlog" 15 ) 16 17 type hclogAdapter struct { 18 wrappedLogger *mlog.Logger 19 extrasKey string 20 } 21 22 func (h *hclogAdapter) Log(level hclog.Level, msg string, args ...interface{}) { 23 switch level { 24 case hclog.Trace: 25 h.Trace(msg, args...) 26 case hclog.Debug: 27 h.Debug(msg, args...) 28 case hclog.Info: 29 h.Info(msg, args...) 30 case hclog.Warn: 31 h.Warn(msg, args...) 32 case hclog.Error: 33 h.Error(msg, args...) 34 default: 35 // For unknown/unexpected log level, treat it as an error so we notice and fix the code. 36 h.Error(msg, args...) 37 } 38 } 39 40 func (h *hclogAdapter) Trace(msg string, args ...interface{}) { 41 extras := strings.TrimSpace(fmt.Sprint(args...)) 42 if extras != "" { 43 h.wrappedLogger.Debug(msg, mlog.String(h.extrasKey, extras)) 44 } else { 45 h.wrappedLogger.Debug(msg) 46 } 47 } 48 49 func (h *hclogAdapter) Debug(msg string, args ...interface{}) { 50 extras := strings.TrimSpace(fmt.Sprint(args...)) 51 if extras != "" { 52 h.wrappedLogger.Debug(msg, mlog.String(h.extrasKey, extras)) 53 } else { 54 h.wrappedLogger.Debug(msg) 55 } 56 } 57 58 func (h *hclogAdapter) Info(msg string, args ...interface{}) { 59 extras := strings.TrimSpace(fmt.Sprint(args...)) 60 if extras != "" { 61 h.wrappedLogger.Info(msg, mlog.String(h.extrasKey, extras)) 62 } else { 63 h.wrappedLogger.Info(msg) 64 } 65 } 66 67 func (h *hclogAdapter) Warn(msg string, args ...interface{}) { 68 extras := strings.TrimSpace(fmt.Sprint(args...)) 69 if extras != "" { 70 h.wrappedLogger.Warn(msg, mlog.String(h.extrasKey, extras)) 71 } else { 72 h.wrappedLogger.Warn(msg) 73 } 74 } 75 76 func (h *hclogAdapter) Error(msg string, args ...interface{}) { 77 extras := strings.TrimSpace(fmt.Sprint(args...)) 78 if extras != "" { 79 h.wrappedLogger.Error(msg, mlog.String(h.extrasKey, extras)) 80 } else { 81 h.wrappedLogger.Error(msg) 82 } 83 } 84 85 func (h *hclogAdapter) IsTrace() bool { 86 return false 87 } 88 89 func (h *hclogAdapter) IsDebug() bool { 90 return true 91 } 92 93 func (h *hclogAdapter) IsInfo() bool { 94 return true 95 } 96 97 func (h *hclogAdapter) IsWarn() bool { 98 return true 99 } 100 101 func (h *hclogAdapter) IsError() bool { 102 return true 103 } 104 105 func (h *hclogAdapter) With(args ...interface{}) hclog.Logger { 106 return h 107 } 108 109 func (h *hclogAdapter) Named(name string) hclog.Logger { 110 return h 111 } 112 113 func (h *hclogAdapter) ResetNamed(name string) hclog.Logger { 114 return h 115 } 116 117 func (h *hclogAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger { 118 return h.wrappedLogger.StdLog() 119 } 120 121 func (h *hclogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer { 122 return h.wrappedLogger.StdLogWriter() 123 } 124 125 func (h *hclogAdapter) SetLevel(hclog.Level) {} 126 127 func (h *hclogAdapter) ImpliedArgs() []interface{} { 128 return []interface{}{} 129 } 130 131 func (h *hclogAdapter) Name() string { 132 return "MattermostPluginLogger" 133 }