You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(elastic): better support for logging raised and caught exceptions and fix formatter crash (#120)
* fix(elastic): better support for logging raised and caught exceptions and fix formatter crash
Logging caught exceptions can be done with crash_reason arg on logger methods, e.g. Logger.error("something went wrong", crash_reason: {e, __STACKTRACE__})
Formatter crash would sometimes appear on exception logs because `message` would be an array instead of a regular string
* docs(logger_json): add crash_reason to well-known metadata keys
---------
Co-authored-by: Bart van Oort <bart.van.oort@opinity.nl>
"message" => "Process #PID<0.322.0> raised an exception\n** (RuntimeError) runtime error\n test/logger_json/formatters/elastic_test.exs:191: anonymous fn/0 in LoggerJSON.Formatters.ElasticTest.\"test logs exceptions\"/1"
66
67
}
67
68
```
68
69
@@ -89,12 +90,44 @@ defmodule LoggerJSON.Formatters.Elastic do
89
90
"error.code" => 42,
90
91
"error.id" => "oops_id",
91
92
"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.stack_trace" => "** (LoggerJSON.Formatters.ElasticTest.TestException) oops!\n test/logger_json/formatters/elastic_test.exs:223: anonymous fn/0 in LoggerJSON.Formatters.ElasticTest.\"test logs exceptions with id and code\"/1\n",
"message" => "Process #PID<0.325.0> raised an exception\n** (LoggerJSON.Formatters.ElasticTest.TestException) oops!\n test/logger_json/formatters/elastic_test.exs:223: anonymous fn/0 in LoggerJSON.Formatters.ElasticTest.\"test logs exceptions with id and code\"/1"
96
97
}
97
98
```
99
+
100
+
You can also choose to log caught exceptions with a custom message.
101
+
For example, after catching an exception with `try`/`rescue`:
102
+
103
+
```elixir
104
+
try do
105
+
raise "oops"
106
+
rescue
107
+
e in RuntimeError -> Logger.error("Something went wrong", crash_reason: {e, __STACKTRACE__})
0 commit comments