json.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3. // of this software and associated documentation files (the "Software"), to deal
  4. // in the Software without restriction, including without limitation the rights
  5. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. // copies of the Software, and to permit persons to whom the Software is
  7. // furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in
  10. // all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. // THE SOFTWARE.
  19. package spanlog
  20. import (
  21. "encoding/json"
  22. "fmt"
  23. "github.com/opentracing/opentracing-go/log"
  24. )
  25. type fieldsAsMap map[string]string
  26. // MaterializeWithJSON converts log Fields into JSON string
  27. // TODO refactor into pluggable materializer
  28. func MaterializeWithJSON(logFields []log.Field) ([]byte, error) {
  29. fields := fieldsAsMap(make(map[string]string, len(logFields)))
  30. for _, field := range logFields {
  31. field.Marshal(fields)
  32. }
  33. if event, ok := fields["event"]; ok && len(fields) == 1 {
  34. return []byte(event), nil
  35. }
  36. return json.Marshal(fields)
  37. }
  38. func (ml fieldsAsMap) EmitString(key, value string) {
  39. ml[key] = value
  40. }
  41. func (ml fieldsAsMap) EmitBool(key string, value bool) {
  42. ml[key] = fmt.Sprintf("%t", value)
  43. }
  44. func (ml fieldsAsMap) EmitInt(key string, value int) {
  45. ml[key] = fmt.Sprintf("%d", value)
  46. }
  47. func (ml fieldsAsMap) EmitInt32(key string, value int32) {
  48. ml[key] = fmt.Sprintf("%d", value)
  49. }
  50. func (ml fieldsAsMap) EmitInt64(key string, value int64) {
  51. ml[key] = fmt.Sprintf("%d", value)
  52. }
  53. func (ml fieldsAsMap) EmitUint32(key string, value uint32) {
  54. ml[key] = fmt.Sprintf("%d", value)
  55. }
  56. func (ml fieldsAsMap) EmitUint64(key string, value uint64) {
  57. ml[key] = fmt.Sprintf("%d", value)
  58. }
  59. func (ml fieldsAsMap) EmitFloat32(key string, value float32) {
  60. ml[key] = fmt.Sprintf("%f", value)
  61. }
  62. func (ml fieldsAsMap) EmitFloat64(key string, value float64) {
  63. ml[key] = fmt.Sprintf("%f", value)
  64. }
  65. func (ml fieldsAsMap) EmitObject(key string, value interface{}) {
  66. ml[key] = fmt.Sprintf("%+v", value)
  67. }
  68. func (ml fieldsAsMap) EmitLazyLogger(value log.LazyLogger) {
  69. value(ml)
  70. }