Moving from lager to logger

Hi,

In Zotonic we want to replace lager with logger.

Problem is that lager seems to remove the default logger handler, so any dependency using lager will make logger inoperable.

Does anybody have advice how to proceed with removing lager?

Cheers, Marc

4 Likes

Migrating should be step by step. In each module where need to use logger - need to use -include_lib("kernel/include/logger.hrl"). for including macroses like LOG from otp/logger.hrl at OTP-24.2.1 · erlang/otp · GitHub, then you can use it like:

lager:

lager:info("Invalid CSV file, could not fetch line with column defs (is there a LF or CR at the end?)"),

logger:

...
-include_lib("kernel/include/logger.hrl").
...
?LOG(info, "Invalid CSV file, could not fetch line with column defs (is there a LF or CR at the end?)"),

Also need to set config like:

rebar.config

...
{kernel,
 [{logger,
   [{handler, default, logger_std_h,
     #{level => info,
       config =>
	   #{sync_mode_qlen => 10000,
	     drop_mode_qlen => 10000,
	     flush_qlen     => 10000}
      }
    }
   ]}
 ]}.
...

And don’t need to forgot add kernel into *app.src.
Also you can create/add/remove/replace custom handlers use API of logger.
More info Erlang -- logger.

2 Likes

That looks quite nasty.

It looks like exometer_core and jsxrecord are the only deps mentioned by https://hex.pm over which we don’t have control. Both don’t seem to use in in their core, or even start lager.

3 Likes

All the others, like buffalo, mqtt_sessions, etc are our own.

3 Likes

Personally was not hit but his, but since you are saying that lager removes only default handler, would it be feasible to use a dedicated handler for your logs then?

3 Likes

jsxrecord

that is also controlled by us.

4 Likes

What I did for the migration was to first create my own logger.hrl that forwarded all macros to the respective lager:<level>() calls. Then I migrated all code gradually (subsystem-wise) to use those macros, and now the header is essentially just loading the OTP logger.hrl. It’s a bit annoying that logger and lager are not nicely interoperable, but this way worked for me.

4 Likes

I decided to go the brute force approach, and am replacing lager calls with the logger macros (like @vkatsuba told) everywhere and also eradicating lager from our dependencies.

5 Likes

Expect new releases of buffalo, jsxrecord, mqtt_sessions, and template_compiler soon.

6 Likes