Hanami.app["logger"] does not work as expected

Hey,
I tried to use the Hanami.app["logger"] and wanted to set it at a Sequel Connection, but the logs look like this:

# Logfile created on 2022-11-10 11:05:57 +0100 by logger.rb/v1.5.0
[services] [DEBUG] [2022-11-10 11:07:17 +0100] 
[services] [INFO] [2022-11-10 11:07:40 +0100] 
[services] [INFO] [2022-11-10 11:07:56 +0100] 
[services] [INFO] [2022-11-10 11:08:29 +0100] 

If I use the Hanami.app["logger"] directly, I found out, that I have to use:

Hanami.app["logger"].warn({ message: "hello warning" })

to get it logging.

[services] [WARN] [2022-11-10 11:34:28 +0100] hello warning

But using it like this,

Hanami.app["logger"].warn("hello warning")

results in

[services] [WARN] [2022-11-10 11:09:16 +0100] 

It seems the issue lies here: dry-logger/dispatcher.rb at dc15c91625887fef68efb4ade2fa0f6013e95f24 · dry-rb/dry-logger · GitHub
or am I misunderstanding it?
Furthermore, I think the default logging in development should be coloured, shouldn’t it? Because that’s not the case for me.

Thanks

Thanks for bringing this up.

So, the default logger is actually geared towards rack-specific logging and it’s structured by default (that’s why it expects log payloads rather than a plain string). It’s handling log payloads in a special way and it’s formatting log lines in a special way. It’s not suitable for general-purpose logging. I’m going to improve this by creating a dedicated logging backend for rack requests and a general purpose logger for everything else (we may also want to have an sql-specific logger).

BTW, in the future, if you find that something is not working, please feel free to report an issue on GitHub rather than a thread here.

1 Like

All right. So for now, a workaround would be to register a general purpose logger?

# i.e.
register("logging.logger", Hanami::Logger.new("services"))

Ok, if you want I can create an issue.

@wuarmin there’s no Hanami::Logger anymore, you could do this though:

register("logging.logger", Dry.Logger(:your_app_name)

Oh and btw I forgot to mention that it doesn’t support coloring yet but I’ll add it eventually, can’t promise if I’ll make it before 2.0 final though.

1 Like