gmile

gmile

Custom formatter for logs coming from :ssl application. How?

I’m failing to use my own custom module to format log entries coming from :ssl application, under OTP 25. Here’s my code:

require Logger

defmodule MyFormatter do
  def format(level, message, _timestamp, _metadata) do
    ['[', level, ']', 'Simon says:', message, ?\n]
  end
end

:inets.start()
:ssl.start()

:ok = :logger.update_handler_config(:ssl_handler, :formatter, {MyFormatter, %{}})

:httpc.request('https://github.com')

The output still prints what looks like a default message:

11:50:31.761 [warning] Description: ~c"Server authenticity is not verified since certificate path validation is not enabled"
     Reason: ~c"The option {verify, verify_peer} and one of the options 'cacertfile' or 'cacerts' are required to enable this."

If I check handler configuration via :logger.get_handler_config(:ssl_handler) it looks as expected:

{:ok,
 %{
   config: %{
     burst_limit_enable: true,
     burst_limit_max_count: 500,
     burst_limit_window_time: 1000,
     drop_mode_qlen: 200,
     filesync_repeat_interval: :no_repeat,
     flush_qlen: 1000,
     overload_kill_enable: false,
     overload_kill_mem_size: 3000000,
     overload_kill_qlen: 20000,
     overload_kill_restart_after: 5000,
     sync_mode_qlen: 10,
     type: :standard_io
   },
   filter_default: :stop,
   filters: [
     filter_non_ssl: {&:logger_filters.domain/2, {:log, :sub, [:otp, :ssl]}}
   ],
   formatter: {MyFormatter, %{}},
   id: :ssl_handler,
   level: :debug,
   module: :logger_std_h
 }}

What I might be doing wrong? :thinking:

Most Liked

hauleth

hauleth

Yes, the values that are passed to the formatter aren’t strings. That is your problem. Check out documentation

https://erlang.org/doc/man/logger.html#FModule:format-2

It says that 1st argument is log_event() which is a map, and 2nd argument is a formatter_config() which happens to be map as well. Your function then should return unicode:chardata(). In your case you break that contract and instead you return list that is mix between chartists, maps, and integers.

hauleth

hauleth

If these values are JSON encodable (hint - usually they are not).

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement