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.

json_formatter_test.go 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package logrus
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. )
  10. func TestErrorNotLost(t *testing.T) {
  11. formatter := &JSONFormatter{}
  12. b, err := formatter.Format(WithField("error", errors.New("wild walrus")))
  13. if err != nil {
  14. t.Fatal("Unable to format entry: ", err)
  15. }
  16. entry := make(map[string]interface{})
  17. err = json.Unmarshal(b, &entry)
  18. if err != nil {
  19. t.Fatal("Unable to unmarshal formatted entry: ", err)
  20. }
  21. if entry["error"] != "wild walrus" {
  22. t.Fatal("Error field not set")
  23. }
  24. }
  25. func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
  26. formatter := &JSONFormatter{}
  27. b, err := formatter.Format(WithField("omg", errors.New("wild walrus")))
  28. if err != nil {
  29. t.Fatal("Unable to format entry: ", err)
  30. }
  31. entry := make(map[string]interface{})
  32. err = json.Unmarshal(b, &entry)
  33. if err != nil {
  34. t.Fatal("Unable to unmarshal formatted entry: ", err)
  35. }
  36. if entry["omg"] != "wild walrus" {
  37. t.Fatal("Error field not set")
  38. }
  39. }
  40. func TestFieldClashWithTime(t *testing.T) {
  41. formatter := &JSONFormatter{}
  42. b, err := formatter.Format(WithField("time", "right now!"))
  43. if err != nil {
  44. t.Fatal("Unable to format entry: ", err)
  45. }
  46. entry := make(map[string]interface{})
  47. err = json.Unmarshal(b, &entry)
  48. if err != nil {
  49. t.Fatal("Unable to unmarshal formatted entry: ", err)
  50. }
  51. if entry["fields.time"] != "right now!" {
  52. t.Fatal("fields.time not set to original time field")
  53. }
  54. if entry["time"] != "0001-01-01T00:00:00Z" {
  55. t.Fatal("time field not set to current time, was: ", entry["time"])
  56. }
  57. }
  58. func TestFieldClashWithMsg(t *testing.T) {
  59. formatter := &JSONFormatter{}
  60. b, err := formatter.Format(WithField("msg", "something"))
  61. if err != nil {
  62. t.Fatal("Unable to format entry: ", err)
  63. }
  64. entry := make(map[string]interface{})
  65. err = json.Unmarshal(b, &entry)
  66. if err != nil {
  67. t.Fatal("Unable to unmarshal formatted entry: ", err)
  68. }
  69. if entry["fields.msg"] != "something" {
  70. t.Fatal("fields.msg not set to original msg field")
  71. }
  72. }
  73. func TestFieldClashWithLevel(t *testing.T) {
  74. formatter := &JSONFormatter{}
  75. b, err := formatter.Format(WithField("level", "something"))
  76. if err != nil {
  77. t.Fatal("Unable to format entry: ", err)
  78. }
  79. entry := make(map[string]interface{})
  80. err = json.Unmarshal(b, &entry)
  81. if err != nil {
  82. t.Fatal("Unable to unmarshal formatted entry: ", err)
  83. }
  84. if entry["fields.level"] != "something" {
  85. t.Fatal("fields.level not set to original level field")
  86. }
  87. }
  88. func TestFieldClashWithRemappedFields(t *testing.T) {
  89. formatter := &JSONFormatter{
  90. FieldMap: FieldMap{
  91. FieldKeyTime: "@timestamp",
  92. FieldKeyLevel: "@level",
  93. FieldKeyMsg: "@message",
  94. },
  95. }
  96. b, err := formatter.Format(WithFields(Fields{
  97. "@timestamp": "@timestamp",
  98. "@level": "@level",
  99. "@message": "@message",
  100. "timestamp": "timestamp",
  101. "level": "level",
  102. "msg": "msg",
  103. }))
  104. if err != nil {
  105. t.Fatal("Unable to format entry: ", err)
  106. }
  107. entry := make(map[string]interface{})
  108. err = json.Unmarshal(b, &entry)
  109. if err != nil {
  110. t.Fatal("Unable to unmarshal formatted entry: ", err)
  111. }
  112. for _, field := range []string{"timestamp", "level", "msg"} {
  113. if entry[field] != field {
  114. t.Errorf("Expected field %v to be untouched; got %v", field, entry[field])
  115. }
  116. remappedKey := fmt.Sprintf("fields.%s", field)
  117. if remapped, ok := entry[remappedKey]; ok {
  118. t.Errorf("Expected %s to be empty; got %v", remappedKey, remapped)
  119. }
  120. }
  121. for _, field := range []string{"@timestamp", "@level", "@message"} {
  122. if entry[field] == field {
  123. t.Errorf("Expected field %v to be mapped to an Entry value", field)
  124. }
  125. remappedKey := fmt.Sprintf("fields.%s", field)
  126. if remapped, ok := entry[remappedKey]; ok {
  127. if remapped != field {
  128. t.Errorf("Expected field %v to be copied to %s; got %v", field, remappedKey, remapped)
  129. }
  130. } else {
  131. t.Errorf("Expected field %v to be copied to %s; was absent", field, remappedKey)
  132. }
  133. }
  134. }
  135. func TestFieldsInNestedDictionary(t *testing.T) {
  136. formatter := &JSONFormatter{
  137. DataKey: "args",
  138. }
  139. logEntry := WithFields(Fields{
  140. "level": "level",
  141. "test": "test",
  142. })
  143. logEntry.Level = InfoLevel
  144. b, err := formatter.Format(logEntry)
  145. if err != nil {
  146. t.Fatal("Unable to format entry: ", err)
  147. }
  148. entry := make(map[string]interface{})
  149. err = json.Unmarshal(b, &entry)
  150. if err != nil {
  151. t.Fatal("Unable to unmarshal formatted entry: ", err)
  152. }
  153. args := entry["args"].(map[string]interface{})
  154. for _, field := range []string{"test", "level"} {
  155. if value, present := args[field]; !present || value != field {
  156. t.Errorf("Expected field %v to be present under 'args'; untouched", field)
  157. }
  158. }
  159. for _, field := range []string{"test", "fields.level"} {
  160. if _, present := entry[field]; present {
  161. t.Errorf("Expected field %v not to be present at top level", field)
  162. }
  163. }
  164. // with nested object, "level" shouldn't clash
  165. if entry["level"] != "info" {
  166. t.Errorf("Expected 'level' field to contain 'info'")
  167. }
  168. }
  169. func TestJSONEntryEndsWithNewline(t *testing.T) {
  170. formatter := &JSONFormatter{}
  171. b, err := formatter.Format(WithField("level", "something"))
  172. if err != nil {
  173. t.Fatal("Unable to format entry: ", err)
  174. }
  175. if b[len(b)-1] != '\n' {
  176. t.Fatal("Expected JSON log entry to end with a newline")
  177. }
  178. }
  179. func TestJSONMessageKey(t *testing.T) {
  180. formatter := &JSONFormatter{
  181. FieldMap: FieldMap{
  182. FieldKeyMsg: "message",
  183. },
  184. }
  185. b, err := formatter.Format(&Entry{Message: "oh hai"})
  186. if err != nil {
  187. t.Fatal("Unable to format entry: ", err)
  188. }
  189. s := string(b)
  190. if !(strings.Contains(s, "message") && strings.Contains(s, "oh hai")) {
  191. t.Fatal("Expected JSON to format message key")
  192. }
  193. }
  194. func TestJSONLevelKey(t *testing.T) {
  195. formatter := &JSONFormatter{
  196. FieldMap: FieldMap{
  197. FieldKeyLevel: "somelevel",
  198. },
  199. }
  200. b, err := formatter.Format(WithField("level", "something"))
  201. if err != nil {
  202. t.Fatal("Unable to format entry: ", err)
  203. }
  204. s := string(b)
  205. if !strings.Contains(s, "somelevel") {
  206. t.Fatal("Expected JSON to format level key")
  207. }
  208. }
  209. func TestJSONTimeKey(t *testing.T) {
  210. formatter := &JSONFormatter{
  211. FieldMap: FieldMap{
  212. FieldKeyTime: "timeywimey",
  213. },
  214. }
  215. b, err := formatter.Format(WithField("level", "something"))
  216. if err != nil {
  217. t.Fatal("Unable to format entry: ", err)
  218. }
  219. s := string(b)
  220. if !strings.Contains(s, "timeywimey") {
  221. t.Fatal("Expected JSON to format time key")
  222. }
  223. }
  224. func TestFieldDoesNotClashWithCaller(t *testing.T) {
  225. SetReportCaller(false)
  226. formatter := &JSONFormatter{}
  227. b, err := formatter.Format(WithField("func", "howdy pardner"))
  228. if err != nil {
  229. t.Fatal("Unable to format entry: ", err)
  230. }
  231. entry := make(map[string]interface{})
  232. err = json.Unmarshal(b, &entry)
  233. if err != nil {
  234. t.Fatal("Unable to unmarshal formatted entry: ", err)
  235. }
  236. if entry["func"] != "howdy pardner" {
  237. t.Fatal("func field replaced when ReportCaller=false")
  238. }
  239. }
  240. func TestFieldClashWithCaller(t *testing.T) {
  241. SetReportCaller(true)
  242. formatter := &JSONFormatter{}
  243. e := WithField("func", "howdy pardner")
  244. e.Caller = &runtime.Frame{Function: "somefunc"}
  245. b, err := formatter.Format(e)
  246. if err != nil {
  247. t.Fatal("Unable to format entry: ", err)
  248. }
  249. entry := make(map[string]interface{})
  250. err = json.Unmarshal(b, &entry)
  251. if err != nil {
  252. t.Fatal("Unable to unmarshal formatted entry: ", err)
  253. }
  254. if entry["fields.func"] != "howdy pardner" {
  255. t.Fatalf("fields.func not set to original func field when ReportCaller=true (got '%s')",
  256. entry["fields.func"])
  257. }
  258. if entry["func"] != "somefunc" {
  259. t.Fatalf("func not set as expected when ReportCaller=true (got '%s')",
  260. entry["func"])
  261. }
  262. SetReportCaller(false) // return to default value
  263. }
  264. func TestJSONDisableTimestamp(t *testing.T) {
  265. formatter := &JSONFormatter{
  266. DisableTimestamp: true,
  267. }
  268. b, err := formatter.Format(WithField("level", "something"))
  269. if err != nil {
  270. t.Fatal("Unable to format entry: ", err)
  271. }
  272. s := string(b)
  273. if strings.Contains(s, FieldKeyTime) {
  274. t.Error("Did not prevent timestamp", s)
  275. }
  276. }
  277. func TestJSONEnableTimestamp(t *testing.T) {
  278. formatter := &JSONFormatter{}
  279. b, err := formatter.Format(WithField("level", "something"))
  280. if err != nil {
  281. t.Fatal("Unable to format entry: ", err)
  282. }
  283. s := string(b)
  284. if !strings.Contains(s, FieldKeyTime) {
  285. t.Error("Timestamp not present", s)
  286. }
  287. }