Koszal

Koszal

Configuring the default logger through a config file

The bash script below generates and umbrela project (mix new --umbrella logcfg) and then an app (mix new demo). It then creates a task and adjusts config for the :default logger. Finally, everything is compiled and the generated task runs. (mix hello). Despite setting :type and :filesync_repeat_interval in the config, the logger still sees the default values:

19:07:52.772 [warning] config.type :standard_io
19:07:52.773 [warning] config.type :no_repeat

Why?
Elixir 1.15.7 (compiled with Erlang/OTP 26)

Koszal

#!/bin/bash

mkdir /tmp/logger-$$
cd    /tmp/logger-$$

mix new --umbrella logcfg
cd logcfg

dsn=`pwd`

cd $dsn/apps
mix new demo

mkdir -p $dsn/apps/demo/lib/mix

cat <<TASK > $dsn/apps/demo/lib/mix/hello.ex
defmodule Mix.Tasks.Hello do
  use Mix.Task
  require Logger

  def run(_) do
    :logger.get_handler_config(:default) |> IO.inspect()

    {:ok, lcfg} = :logger.get_handler_config(:default)
    Logger.warning( "config.type #{ inspect(lcfg[:config][:type]) }" )
    Logger.warning( "config.filesync_repeat_interval #{ inspect(lcfg[:config][:filesync_repeat_interval]) }" )
  end
end
TASK

cat <<CONFIG >> $dsn/config/config.exs
config :logger, :default,
  config: [
    type: :standard_error,
    filesync_repeat_interval: 5000
  ]
CONFIG

cd $dsn
mix compile
mix hello
pwd

First 8 of 8 Posts! Switch mode

cmo

cmo

Do you need to configure default_handler, not default?

Here is a working config for file logging:

config :logger, :default_handler,
  config: [
    filesync_repeat_interval: 5000,
    file_check: 5000,
    max_no_bytes: 10_000_000,
    max_no_files: 5,
    compress_on_rotate: false,
    file: ...
  ]
Koszal

Koszal

I had tried that before (and a few other things) and it still said

06:42:06.040 [warning] config.filesync_repeat_interval :no_repeat

So you either have an older Elixir version (there were changes to the logging mechanism), or it does not work in the context of the task, or there is something else I don’t understand…

Have you run the bash script after changing default to default_handler? What does it print?

K.

cmo

cmo

I’m in 1.16.3 and sorry but I won’t be running your bash script.

hauleth

hauleth

If you are using OTP 24+ and you aware of the consequences, then you can run logger:reconfigure/0 as part of your startup script. In general default handler is only partially configurable after it started.

Koszal

Koszal

You seem to apply I need some black magic whereas in fact I just want to figure out how to configure basic properties of the logger.

I have followed Elixir documentation at

It tells you to put the settings in config/config.exs and gives a snippet of code.

My current config is

config :logger, :default_handler,
  config: [
    file: ~c"/home/kos/elixir-demo.log",
    type: :standard_error,
    filesync_repeat_interval: 4000,
    file_check: 5000,
    max_no_bytes: 10_000_000,
    max_no_files: 5,
    compress_on_rotate: true
  ]

I have also tried config at the application level:

config :demo, :logger, [
  {:handler, :file_log, :logger_std_h, %{
     config: %{
       file: ~c"/home/kos/elixir-demo.log",
       type: :standard_error,
       filesync_repeat_interval: 300,
       file_check: 5000,
       max_no_bytes: 10_000_000,
       max_no_files: 5,
       compress_on_rotate: true
     },
     formatter: Logger.Formatter.new()
   }}
]

Nothing seems to affect the logging. The log file is not created, messages appear on stdout (rather than stderr), etc.

K.

cmo

cmo

why do you have this there if you’re trying to log to a file? Have you tried removing that or using :file as per the docs?

Koszal

Koszal

Yes.

You can trivially check yourself - :woman_shrugging:

hauleth

hauleth

Answer adequate to your post:

I have tested, without :type field it worked perfectly.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement