hclog_adapter.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2021 Workrise Technologies Inc.
  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. package csplugin
  15. import (
  16. "fmt"
  17. "io"
  18. "log"
  19. "os"
  20. "reflect"
  21. "github.com/hashicorp/go-hclog"
  22. "github.com/sirupsen/logrus"
  23. )
  24. // NewHCLogAdapter takes an instance of a Logrus logger and returns an hclog
  25. // logger in the form of an HCLogAdapter.
  26. func NewHCLogAdapter(l *logrus.Logger, name string) hclog.Logger {
  27. return &HCLogAdapter{l, name, nil}
  28. }
  29. // HCLogAdapter implements the hclog interface. Plugins use hclog to send
  30. // log entries back to ephemeral-iam and this adapter allows for those logs
  31. // to be handled by ephemeral-iam's Logrus logger.
  32. type HCLogAdapter struct {
  33. log *logrus.Logger
  34. name string
  35. impliedArgs []interface{}
  36. }
  37. func (h HCLogAdapter) Log(level hclog.Level, msg string, args ...interface{}) {
  38. switch level {
  39. case hclog.NoLevel:
  40. return
  41. case hclog.Trace:
  42. h.Trace(msg, args...)
  43. case hclog.Debug:
  44. h.Debug(msg, args...)
  45. case hclog.Info:
  46. h.Info(msg, args...)
  47. case hclog.Warn:
  48. h.Warn(msg, args...)
  49. case hclog.Error:
  50. h.Error(msg, args...)
  51. }
  52. }
  53. func (h HCLogAdapter) Trace(msg string, args ...interface{}) {
  54. h.log.WithFields(toLogrusFields(args)).Trace(msg)
  55. }
  56. func (h HCLogAdapter) Debug(msg string, args ...interface{}) {
  57. h.log.WithFields(toLogrusFields(args)).Debug(msg)
  58. }
  59. func (h HCLogAdapter) Info(msg string, args ...interface{}) {
  60. h.log.WithFields(toLogrusFields(args)).Info(msg)
  61. }
  62. func (h HCLogAdapter) Warn(msg string, args ...interface{}) {
  63. h.log.WithFields(toLogrusFields(args)).Warn(msg)
  64. }
  65. func (h HCLogAdapter) Error(msg string, args ...interface{}) {
  66. h.log.WithFields(toLogrusFields(args)).Error(msg)
  67. }
  68. func (h HCLogAdapter) IsTrace() bool {
  69. return h.log.GetLevel() >= logrus.TraceLevel
  70. }
  71. func (h HCLogAdapter) IsDebug() bool {
  72. return h.log.GetLevel() >= logrus.DebugLevel
  73. }
  74. func (h HCLogAdapter) IsInfo() bool {
  75. return h.log.GetLevel() >= logrus.InfoLevel
  76. }
  77. func (h HCLogAdapter) IsWarn() bool {
  78. return h.log.GetLevel() >= logrus.WarnLevel
  79. }
  80. func (h HCLogAdapter) IsError() bool {
  81. return h.log.GetLevel() >= logrus.ErrorLevel
  82. }
  83. func (h HCLogAdapter) ImpliedArgs() []interface{} {
  84. // Not supported.
  85. return nil
  86. }
  87. func (h HCLogAdapter) With(args ...interface{}) hclog.Logger {
  88. return &h
  89. }
  90. func (h HCLogAdapter) Name() string {
  91. return h.name
  92. }
  93. func (h HCLogAdapter) Named(name string) hclog.Logger {
  94. return NewHCLogAdapter(h.log, name)
  95. }
  96. func (h HCLogAdapter) ResetNamed(name string) hclog.Logger {
  97. return &h
  98. }
  99. func (h *HCLogAdapter) SetLevel(level hclog.Level) {
  100. h.log.SetLevel(level2logrus(level))
  101. }
  102. func (h HCLogAdapter) GetLevel() hclog.Level {
  103. return level2hclog(h.log.GetLevel())
  104. }
  105. func (h HCLogAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
  106. if opts == nil {
  107. opts = &hclog.StandardLoggerOptions{}
  108. }
  109. return log.New(h.StandardWriter(opts), "", 0)
  110. }
  111. func (h HCLogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
  112. return os.Stderr
  113. }
  114. // level2logrus maps hclog levels to Logrus levels.
  115. func level2logrus(level hclog.Level) logrus.Level {
  116. switch level {
  117. case hclog.NoLevel:
  118. // Logrus does not have NoLevel, so use Info instead.
  119. return logrus.InfoLevel
  120. case hclog.Trace:
  121. return logrus.TraceLevel
  122. case hclog.Debug:
  123. return logrus.DebugLevel
  124. case hclog.Info:
  125. return logrus.InfoLevel
  126. case hclog.Warn:
  127. return logrus.WarnLevel
  128. case hclog.Error:
  129. return logrus.ErrorLevel
  130. default:
  131. return logrus.InfoLevel
  132. }
  133. }
  134. func level2hclog(level logrus.Level) hclog.Level {
  135. switch level {
  136. case logrus.TraceLevel:
  137. return hclog.Trace
  138. case logrus.DebugLevel:
  139. return hclog.Debug
  140. case logrus.InfoLevel:
  141. return hclog.Info
  142. case logrus.WarnLevel:
  143. return hclog.Warn
  144. case logrus.ErrorLevel:
  145. return hclog.Error
  146. default:
  147. return hclog.Info
  148. }
  149. }
  150. // toLogrusFields takes a list of key/value pairs passed to the hclog logger
  151. // and converts them to a map to be used as Logrus fields.
  152. func toLogrusFields(kvPairs []interface{}) map[string]interface{} {
  153. m := map[string]interface{}{}
  154. if len(kvPairs) == 0 {
  155. return m
  156. }
  157. if len(kvPairs)%2 == 1 {
  158. // There are an odd number of key/value pairs so append nil as the final value.
  159. kvPairs = append(kvPairs, nil)
  160. }
  161. for i := 0; i < len(kvPairs); i += 2 {
  162. // hclog automatically adds the timestamp field, ignore it.
  163. if kvPairs[i] != "timestamp" {
  164. merge(m, kvPairs[i], kvPairs[i+1])
  165. }
  166. }
  167. return m
  168. }
  169. // merge takes a key/value pair and converts them to strings then adds them to
  170. // the dst map.
  171. func merge(dst map[string]interface{}, k, v interface{}) {
  172. var key string
  173. switch x := k.(type) {
  174. case string:
  175. key = x
  176. case fmt.Stringer:
  177. key = safeString(x)
  178. default:
  179. key = fmt.Sprint(x)
  180. }
  181. dst[key] = v
  182. }
  183. // safeString takes an interface that implements the String() function and calls it
  184. // to attempt to convert it to a string. If a panic occurs, and it's caused by a
  185. // nil pointer, the value will be set to "NULL".
  186. func safeString(str fmt.Stringer) (s string) {
  187. defer func() {
  188. if panicVal := recover(); panicVal != nil {
  189. if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
  190. s = "NULL"
  191. } else {
  192. panic(panicVal)
  193. }
  194. }
  195. }()
  196. s = str.String()
  197. return
  198. }