PlugLoggerWithMeta
PlugLoggerWithMeta is a Plug.Logger based on Plug.Logger code, focused on metadata logging.
Why
Plug.Logger does not use Logger.metadata,
so some useful information such as method and request_path are embedded into the log message
hence they must be scraped.
PlugLoggerWithMeta makes them machine readable by exporting them using Logger.metadata.
There are also other similar libraries, however this tries to behave as close as possible to Plug.Logger.
Installation
-
Add
:plug_logger_with_metadependency to your project’smix.exs:
def deps do
[
{:plug_logger_with_meta, "~> 0.1"}
]
endRun
mix deps.getReplace
Plug.LoggerwithPlugLoggerWithMeta:
--- a/endpoint.ex
+++ b/endpoint.ex
@@ -38,7 +38,7 @@ defmodule MyProject.APIWeb.Endpoint do
end
plug Plug.RequestId
- plug Plug.Logger
+ plug PlugLoggerWithMeta- Add relevant metadata to your logger configuration:
config :logger, :console, format: "[$level] $message\n"
+ metadata: [
+ :method,
+ :request_path,
+ :status_code,
+ :elapsed,
+ ]- Optional: use it with pretty_log (or any other logger formatter/backend)
pretty_log will format all metadata using logfmt.
Available Metadata
-
elapsed (e.g.
22ms) -
method (e.g.
GET) -
request_path (e.g.
/v1/my/path) -
status_code (e.g.
200) -
tag (e.g.
got_client_req,sent_reply)
About This Project
This project has been created in order to provide better logs in Astarte. We are open to any contribution and we encourage adoption of this library, also outside Astarte, in order to provide better logs to everyone.