hclog_adapter.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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(convertLevel(level))
  101. }
  102. func (h HCLogAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
  103. if opts == nil {
  104. opts = &hclog.StandardLoggerOptions{}
  105. }
  106. return log.New(h.StandardWriter(opts), "", 0)
  107. }
  108. func (h HCLogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
  109. return os.Stderr
  110. }
  111. // convertLevel maps hclog levels to Logrus levels.
  112. func convertLevel(level hclog.Level) logrus.Level {
  113. switch level {
  114. case hclog.NoLevel:
  115. // Logrus does not have NoLevel, so use Info instead.
  116. return logrus.InfoLevel
  117. case hclog.Trace:
  118. return logrus.TraceLevel
  119. case hclog.Debug:
  120. return logrus.DebugLevel
  121. case hclog.Info:
  122. return logrus.InfoLevel
  123. case hclog.Warn:
  124. return logrus.WarnLevel
  125. case hclog.Error:
  126. return logrus.ErrorLevel
  127. default:
  128. return logrus.InfoLevel
  129. }
  130. }
  131. // toLogrusFields takes a list of key/value pairs passed to the hclog logger
  132. // and converts them to a map to be used as Logrus fields.
  133. func toLogrusFields(kvPairs []interface{}) map[string]interface{} {
  134. m := map[string]interface{}{}
  135. if len(kvPairs) == 0 {
  136. return m
  137. }
  138. if len(kvPairs)%2 == 1 {
  139. // There are an odd number of key/value pairs so append nil as the final value.
  140. kvPairs = append(kvPairs, nil)
  141. }
  142. for i := 0; i < len(kvPairs); i += 2 {
  143. // hclog automatically adds the timestamp field, ignore it.
  144. if kvPairs[i] != "timestamp" {
  145. merge(m, kvPairs[i], kvPairs[i+1])
  146. }
  147. }
  148. return m
  149. }
  150. // merge takes a key/value pair and converts them to strings then adds them to
  151. // the dst map.
  152. func merge(dst map[string]interface{}, k, v interface{}) {
  153. var key string
  154. switch x := k.(type) {
  155. case string:
  156. key = x
  157. case fmt.Stringer:
  158. key = safeString(x)
  159. default:
  160. key = fmt.Sprint(x)
  161. }
  162. dst[key] = v
  163. }
  164. // safeString takes an interface that implements the String() function and calls it
  165. // to attempt to convert it to a string. If a panic occurs, and it's caused by a
  166. // nil pointer, the value will be set to "NULL".
  167. func safeString(str fmt.Stringer) (s string) {
  168. defer func() {
  169. if panicVal := recover(); panicVal != nil {
  170. if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
  171. s = "NULL"
  172. } else {
  173. panic(panicVal)
  174. }
  175. }
  176. }()
  177. s = str.String()
  178. return
  179. }