Skip to content

Commit be34c26

Browse files
committed
Fix JSON examples in Elastic formatter
1 parent 2a37993 commit be34c26

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

lib/logger_json/formatters/elastic.ex

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,83 @@ defmodule LoggerJSON.Formatters.Elastic do
1818
1919
Example of an info log (`Logger.info("Hello")` without any metadata):
2020
21-
%{
22-
"@timestamp" => "2024-05-17T16:20:00.000Z",
23-
"ecs.version" => "8.11.0",
24-
"log.level" => "info",
25-
"log.logger" => "Elixir.LoggerJSON.Formatters.ElasticTest",
26-
"log.origin" => %{
27-
"file.name" => ~c"/app/logger_json/test/formatters/elastic_test.exs",
28-
"file.line" => 18,
29-
"function" => "test logs an LogEntry of every level/1"
30-
},
31-
"message" => "Hello"
32-
}
21+
```elixir
22+
%{
23+
"@timestamp" => "2024-05-17T16:20:00.000Z",
24+
"ecs.version" => "8.11.0",
25+
"log.level" => "info",
26+
"log.logger" => "Elixir.LoggerJSON.Formatters.ElasticTest",
27+
"log.origin" => %{
28+
"file.name" => ~c"/app/logger_json/test/formatters/elastic_test.exs",
29+
"file.line" => 18,
30+
"function" => "test logs an LogEntry of every level/1"
31+
},
32+
"message" => "Hello"
33+
}
34+
```
3335
3436
Example of logging by keywords or by map (Logger.info(%{message: "Hello", foo: :bar, fiz: %{buz: "buz"}})).
3537
The keywords or map items are added to the top-level of the log entry:
3638
37-
%{
38-
"@timestamp" => "2024-05-17T16:20:00.000Z",
39-
"ecs.version" => "8.11.0",
40-
"fiz" => %{"buz" => "buz"},
41-
"foo" => "bar",
42-
"log.level" => "debug",
43-
"log.logger" => "Elixir.LoggerJSON.Formatters.ElasticTest",
44-
"log.origin" => %{
45-
"file.line" => 68,
46-
"file.name" => ~c"/app/logger_json/test/formatters/elastic_test.exs",
47-
"function" => "test logs an LogEntry with a map payload containing message/1"},
48-
"message" => "Hello"
49-
}
39+
```elixir
40+
%{
41+
"@timestamp" => "2024-05-17T16:20:00.000Z",
42+
"ecs.version" => "8.11.0",
43+
"fiz" => %{"buz" => "buz"},
44+
"foo" => "bar",
45+
"log.level" => "debug",
46+
"log.logger" => "Elixir.LoggerJSON.Formatters.ElasticTest",
47+
"log.origin" => %{
48+
"file.line" => 68,
49+
"file.name" => ~c"/app/logger_json/test/formatters/elastic_test.exs",
50+
"function" => "test logs an LogEntry with a map payload containing message/1"},
51+
"message" => "Hello"
52+
}
53+
```
5054
5155
Example of logging due to raising an exception (`raise RuntimeError`):
5256
53-
%{
54-
"@timestamp" => "2024-05-17T16:20:00.000Z",
55-
"ecs.version" => "8.11.0",
56-
"error.message" => "runtime error",
57-
"error.stack_trace" => "** (RuntimeError) runtime error\\n Elixir.LoggerJSON.Formatters.ElasticTest.erl:159: anonymous fn/4 in LoggerJSON.Formatters.ElasticTest.\\"test logs exceptions\\"/1\\n",
58-
"error.type" => "Elixir.RuntimeError",
59-
"log.level" => "error",
60-
"message" => "runtime error"
61-
}
57+
```elixir
58+
%{
59+
"@timestamp" => "2024-05-17T16:20:00.000Z",
60+
"ecs.version" => "8.11.0",
61+
"error.message" => "runtime error",
62+
"error.stack_trace" => "** (RuntimeError) runtime error\\n Elixir.LoggerJSON.Formatters.ElasticTest.erl:159: anonymous fn/4 in LoggerJSON.Formatters.ElasticTest.\\"test logs exceptions\\"/1\\n",
63+
"error.type" => "Elixir.RuntimeError",
64+
"log.level" => "error",
65+
"message" => "runtime error"
66+
}
67+
```
6268
6369
Note that if you raise an exception that contains an `id` or a `code` property, they will be included in the log entry as `error.id` and `error.code` respectively.
6470
6571
Example:
6672
67-
defmodule TestException do
68-
defexception [:message, :id, :code]
69-
end
73+
```elixir
74+
defmodule TestException do
75+
defexception [:message, :id, :code]
76+
end
7077
71-
...
78+
...
7279
73-
raise TestException, id: :oops_id, code: 42, message: "oops!"
80+
raise TestException, id: :oops_id, code: 42, message: "oops!"
81+
```
7482
7583
results in:
7684
77-
%{
78-
"@timestamp" => "2024-05-17T16:20:00.000Z",
79-
"ecs.version" => "8.11.0",
80-
"error.code" => 42,
81-
"error.id" => "oops_id",
82-
"error.message" => "oops!",
83-
"error.stack_trace" => "** (LoggerJSON.Formatters.ElasticTest.TestException) oops!\n test/formatters/elastic_test.exs:190: anonymous fn/0 in LoggerJSON.Formatters.ElasticTest.\"test logs exceptions with id and code\"/1\n",
84-
"error.type" => "Elixir.LoggerJSON.Formatters.ElasticTest.TestException",
85-
"log.level" => "error",
86-
"message" => "oops!"
87-
}
85+
```elixir
86+
%{
87+
"@timestamp" => "2024-05-17T16:20:00.000Z",
88+
"ecs.version" => "8.11.0",
89+
"error.code" => 42,
90+
"error.id" => "oops_id",
91+
"error.message" => "oops!",
92+
"error.stack_trace" => "** (LoggerJSON.Formatters.ElasticTest.TestException) oops!\\n test/formatters/elastic_test.exs:190: anonymous fn/0 in LoggerJSON.Formatters.ElasticTest.\\"test logs exceptions with id and code\\"/1\\n",
93+
"error.type" => "Elixir.LoggerJSON.Formatters.ElasticTest.TestException",
94+
"log.level" => "error",
95+
"message" => "oops!"
96+
}
97+
```
8898
"""
8999
import Jason.Helpers, only: [json_map: 1]
90100
import LoggerJSON.Formatter.{MapBuilder, DateTime, Message, Metadata, Plug, RedactorEncoder}

0 commit comments

Comments
 (0)