Logo

dev-resources.site

for different kinds of informations.

Elixir logging to (multiple) file(s) using metadata_filter

Published at
2/17/2022
Categories
elixir
metadata
logging
log
Author
darnahsan
Categories
4 categories in total
elixir
open
metadata
open
logging
open
log
open
Author
9 person written this
darnahsan
open
Elixir logging to (multiple) file(s) using metadata_filter

This is a follow up post for JSON Logging in Elixir using a custom formatter where we looked in to how to log request logs to a file using a custom formatter. We require below hex dependencies in our project to log request logs and other app logs.

{:plug_logger_json, "~> 0.7.0"},
{:logger_file_backend, "~> 0.0.12"}
Enter fullscreen mode Exit fullscreen mode

Unlike non functional languages where you have log objects per log file and can use them to write to different log

require "logger"
request_logger = Logger.new('/src/obliviator/log/obliviator_request.log')
request_logger.debug("I'm a request debug log")

stream_logger = Logger.new('/src/obliviator/log/stream.log')
stream_logger.info("I'm a stream info log")

Enter fullscreen mode Exit fullscreen mode

but in Elixir you only have a Logger object and need to differentiate based on metadata assigned to write to the associated log. metadata_filter can be assigned on multiple fields. We can separate logs by each application running on our VM or by key identifying our file.

# log by identifying application 
metadata_filter: [application: :plug_logger_json]
Enter fullscreen mode Exit fullscreen mode
#log by tagging log by key value
metadata_filter: [log: :stream]
Enter fullscreen mode Exit fullscreen mode

The full config would be as below

use Mix.Config

config :plug_logger_json,
  filtered_keys: ["password", "authorization"],
  suppressed_keys: ["api_version", "log_type"]

config :logger,
  backends: [
    {LoggerFileBackend, :request_log},
    {LoggerFileBackend, :message_log},
    {LoggerFileBackend, :debugging_log}
  ],
  utc_log: true,
  handle_otp_reports: true

# configuration for the {LoggerFileBackend, :request_log} backend
config :logger, :request_log,
  format: {Obliviator.Formatter.RequestLog, :format},
  metadata: :all,
  path: "/src/obliviator/log/obliviator_request.log",
  level: :info,
  metadata_filter: [application: :plug_logger_json]

config :logger, :message_log,
  format: {Obliviator.Formatter.MessageLog, :format},
  metadata: :all,
  path: "/src/obliviator/log/stream.log",
  level: :info,
  metadata_filter: [log: :stream]

config :logger, :debugging_log,
  format: {Obliviator.Formatter.MessageLog, :format},
  metadata: :all,
  path: "/src/obliviator/log/obliviator_debugging.log",
  level: :debug,
  metadata_filter: [log: :debugging]
Enter fullscreen mode Exit fullscreen mode

Once the config is in place data can be logged as follows

# log to :stream
Logger.info("Stream msg", log: :stream)

#log to :debugging
Logger.debug("Debugging", log: :debugging)
Enter fullscreen mode Exit fullscreen mode

Elixir logging might look limiting at the start but with a powerful custom formatter and metadata filtering ability its is sufficiently better than many ;)

metadata Article's
30 articles in total
Favicon
How to customize Next.js metadata
Favicon
Metadata, Data Dictionary, and Catalog in a DBMS: Understanding the Differences and Their Roles
Favicon
Improving TypeScript Metadata Type Safety with ts-reflector
Favicon
Reflect MetaData
Favicon
The Machines Would Appreciate More Structured Data
Favicon
How to Assign Different Version Data to a Rented Strategy via Strategy Rental Code Metadata
Favicon
Including extra meta data with a resource response
Favicon
PicoGym Practice Write Up for Forensics Challenge(10pt): information
Favicon
Connecting with your Database with the Information Schema
Favicon
Next.js: How to Create Open Graph Social Media Cards
Favicon
Next.js: favicon, SVG icon, Apple & Chrome icons
Favicon
Security Tips: Metadata
Favicon
Error Message Bibliometrix R - replacement has 0 rows, data has XXX
Favicon
Securing Your Data Lake with Apache Atlas: The Ultimate Guide
Favicon
What I learned as a Subject Matter Expert while creating my product
Favicon
Why metadata management is indispensable for successful data evaluation
Favicon
First P2E game on Tableland (2)
Favicon
AppstoreSpy’s API – Your fresh and keen metadata
Favicon
What is .metadata file in Flutter Project?
Favicon
First P2E game on Tableland (1)
Favicon
Use Aiven's metadata parser to understand how your data flows
Favicon
Badges - TL;DR for your repository's README
Favicon
Elixir logging to (multiple) file(s) using metadata_filter
Favicon
Some housekeeping (why I help as a mod)
Favicon
ShardingSphere’s Metadata Loading Process
Favicon
Metadata: What Is It, And How Does It Boost Your Business?
Favicon
Resumo sobre a versão 2 do serviço de metadados de instância AWS EC2 (IMDSv2)
Favicon
Check your head 🤔
Favicon
How to gets uploaded image Metadata on the front-end
Favicon
On metadata in Hugo - or turning tags to keywords

Featured ones: