You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.4 KiB

  1. package logrus
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. "testing"
  7. )
  8. func TestErrorNotLost(t *testing.T) {
  9. formatter := &JSONFormatter{}
  10. b, err := formatter.Format(WithField("error", errors.New("wild walrus")))
  11. if err != nil {
  12. t.Fatal("Unable to format entry: ", err)
  13. }
  14. entry := make(map[string]interface{})
  15. err = json.Unmarshal(b, &entry)
  16. if err != nil {
  17. t.Fatal("Unable to unmarshal formatted entry: ", err)
  18. }
  19. if entry["error"] != "wild walrus" {
  20. t.Fatal("Error field not set")
  21. }
  22. }
  23. func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
  24. formatter := &JSONFormatter{}
  25. b, err := formatter.Format(WithField("omg", errors.New("wild walrus")))
  26. if err != nil {
  27. t.Fatal("Unable to format entry: ", err)
  28. }
  29. entry := make(map[string]interface{})
  30. err = json.Unmarshal(b, &entry)
  31. if err != nil {
  32. t.Fatal("Unable to unmarshal formatted entry: ", err)
  33. }
  34. if entry["omg"] != "wild walrus" {
  35. t.Fatal("Error field not set")
  36. }
  37. }
  38. func TestFieldClashWithTime(t *testing.T) {
  39. formatter := &JSONFormatter{}
  40. b, err := formatter.Format(WithField("time", "right now!"))
  41. if err != nil {
  42. t.Fatal("Unable to format entry: ", err)
  43. }
  44. entry := make(map[string]interface{})
  45. err = json.Unmarshal(b, &entry)
  46. if err != nil {
  47. t.Fatal("Unable to unmarshal formatted entry: ", err)
  48. }
  49. if entry["fields.time"] != "right now!" {
  50. t.Fatal("fields.time not set to original time field")
  51. }
  52. if entry["time"] != "0001-01-01T00:00:00Z" {
  53. t.Fatal("time field not set to current time, was: ", entry["time"])
  54. }
  55. }
  56. func TestFieldClashWithMsg(t *testing.T) {
  57. formatter := &JSONFormatter{}
  58. b, err := formatter.Format(WithField("msg", "something"))
  59. if err != nil {
  60. t.Fatal("Unable to format entry: ", err)
  61. }
  62. entry := make(map[string]interface{})
  63. err = json.Unmarshal(b, &entry)
  64. if err != nil {
  65. t.Fatal("Unable to unmarshal formatted entry: ", err)
  66. }
  67. if entry["fields.msg"] != "something" {
  68. t.Fatal("fields.msg not set to original msg field")
  69. }
  70. }
  71. func TestFieldClashWithLevel(t *testing.T) {
  72. formatter := &JSONFormatter{}
  73. b, err := formatter.Format(WithField("level", "something"))
  74. if err != nil {
  75. t.Fatal("Unable to format entry: ", err)
  76. }
  77. entry := make(map[string]interface{})
  78. err = json.Unmarshal(b, &entry)
  79. if err != nil {
  80. t.Fatal("Unable to unmarshal formatted entry: ", err)
  81. }
  82. if entry["fields.level"] != "something" {
  83. t.Fatal("fields.level not set to original level field")
  84. }
  85. }
  86. func TestJSONEntryEndsWithNewline(t *testing.T) {
  87. formatter := &JSONFormatter{}
  88. b, err := formatter.Format(WithField("level", "something"))
  89. if err != nil {
  90. t.Fatal("Unable to format entry: ", err)
  91. }
  92. if b[len(b)-1] != '\n' {
  93. t.Fatal("Expected JSON log entry to end with a newline")
  94. }
  95. }
  96. func TestJSONMessageKey(t *testing.T) {
  97. formatter := &JSONFormatter{
  98. FieldMap: FieldMap{
  99. FieldKeyMsg: "message",
  100. },
  101. }
  102. b, err := formatter.Format(&Entry{Message: "oh hai"})
  103. if err != nil {
  104. t.Fatal("Unable to format entry: ", err)
  105. }
  106. s := string(b)
  107. if !(strings.Contains(s, "message") && strings.Contains(s, "oh hai")) {
  108. t.Fatal("Expected JSON to format message key")
  109. }
  110. }
  111. func TestJSONLevelKey(t *testing.T) {
  112. formatter := &JSONFormatter{
  113. FieldMap: FieldMap{
  114. FieldKeyLevel: "somelevel",
  115. },
  116. }
  117. b, err := formatter.Format(WithField("level", "something"))
  118. if err != nil {
  119. t.Fatal("Unable to format entry: ", err)
  120. }
  121. s := string(b)
  122. if !strings.Contains(s, "somelevel") {
  123. t.Fatal("Expected JSON to format level key")
  124. }
  125. }
  126. func TestJSONTimeKey(t *testing.T) {
  127. formatter := &JSONFormatter{
  128. FieldMap: FieldMap{
  129. FieldKeyTime: "timeywimey",
  130. },
  131. }
  132. b, err := formatter.Format(WithField("level", "something"))
  133. if err != nil {
  134. t.Fatal("Unable to format entry: ", err)
  135. }
  136. s := string(b)
  137. if !strings.Contains(s, "timeywimey") {
  138. t.Fatal("Expected JSON to format time key")
  139. }
  140. }
  141. func TestJSONDisableTimestamp(t *testing.T) {
  142. formatter := &JSONFormatter{
  143. DisableTimestamp: true,
  144. }
  145. b, err := formatter.Format(WithField("level", "something"))
  146. if err != nil {
  147. t.Fatal("Unable to format entry: ", err)
  148. }
  149. s := string(b)
  150. if strings.Contains(s, FieldKeyTime) {
  151. t.Error("Did not prevent timestamp", s)
  152. }
  153. }
  154. func TestJSONEnableTimestamp(t *testing.T) {
  155. formatter := &JSONFormatter{}
  156. b, err := formatter.Format(WithField("level", "something"))
  157. if err != nil {
  158. t.Fatal("Unable to format entry: ", err)
  159. }
  160. s := string(b)
  161. if !strings.Contains(s, FieldKeyTime) {
  162. t.Error("Timestamp not present", s)
  163. }
  164. }