vkryukov

vkryukov

LoggerBackends - SQL Logger backends functionality for Elixir v1.15+

Hi, I’ve created an SQL logger backend using LoggerBackends: GitHub - vkryukov/logger_backends_sql · GitHub.

It safely stores all metadata and can be configured to use an existing Repo and/or Schema.

The simplest setup uses an SQLite3 database for logs:

# config/config.exs
config :logger, LoggerBackends.SQL,
  level: :debug,
  path: "./logs.sqlite"

Logs will be written to the logs table. You can also use an existing repo and customize the table name:

# config/config.exs
config :logger, LoggerBackends.SQL,
   repo: MyApp.Repo,
   table_name: "log_messages"

You can use your own Schema for log messages - just ensure the schema module defines a changeset/1 function that accepts time, message, and meta arguments.

Feedback welcome!

https://github.com/elixir-lang/logger_backends

First Post!

vkryukov

vkryukov

While we are here, I would like people’s thoughts on the best way to configure the backend during the runtime.

LoggerBackends offers configure/2, which can be called like this:

LoggerBackends.configure(LoggerBackends.SQL,
   option1: value1,
   option2: value2,
   ...
)

and will send a :configure event to the backend that it can capture. The reference implementation for LoggerBackend.Console is here. This call will only succeed after a backend has already been added with LoggerBackend.add(LoggerBackends.SQL) (as someone needs to listen to these events).

However, you definitely want to set the path to a SQLite log database before adding the backend to perform all the initialization in the backend’s init function.

Something like this would be nice

LoggerBackends.add(LoggerBackends.SQL,
   path: "./my.sqlite", 
   repo: nil
)

However, LoggerBackends.add/2 only processes the :flush option and ignores everything else.

So my current solution is to expose my own configure/1 to do the runtime config:

LoggerBackends.SQL.configure(
  path: "./test1.sqlite",
  repo: nil
)
LoggerBackends.add(LoggerBackends.SQL)

The previous option with LoggerBackend.add/2 is IMHO cleaner, but requires changing the existing contract.

Thoughts?

Where Next?

Popular in Announcing Top

OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 14361 106
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New
mspanc
I am pleased to announce an initial release of the Membrane Framework - an Elixir-based framework with special focus on processing multim...
New
RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 12905 134
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement