Skip to content

secomind/plug_logger_with_meta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 standard Logger metadata mechanisms.

There are also other similar libraries, however this tries to behave as close as possible to Plug.Logger.

Installation

  • Add :plug_logger_with_meta dependency to your project's mix.exs:
def deps do
  [
    {:plug_logger_with_meta, "~> 0.1"}
  ]
end
  • Run mix deps.get

  • Replace Plug.Logger with PlugLoggerWithMeta:

--- 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.

About

Plug.Logger replacement focused on metadata logging.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages