Logger and how to specify level?

Hi guys

I’m switching from lager to logger.

While I’m able to log info, warn, and error messages using:

  • error_logger:info_msg/2
  • error_logger:warning_msg/2
  • error_logger:error_msg/2

I couldn’t find a way to log messages using the following levels:
emergency | alert | critical | info | debug | all | none

There’s no function such as:
error_logger:alert_msg/2

Finally, how one can define the logging template to get this output format:

2024-08-24T09:49:40.169028+02:00  [alert_level] [module_name.line_number] logged_message_here

Many thanks
Z.

I believe my Erlang bits episode could be useful to you https://youtu.be/xMvwYtUkT0A?feature=shared. It explains how to log and how to configure the logging levels. I used a structured logging format but you could probably use the configuration example to figure out the style you want.

1 Like

@chiroptical Thanks. I’ve managed to make it work getting the correct output format.

1 Like

If you have any suggestions about how the logger documentation could be improved, please let me know, or better yet, submit a pr!

1 Like

@garazdawi Thanks. Let’s take the console logger. In my case, this seems to work:

{handler, default, logger_std_h,
 #{config    => #{file => "console.log"}

But not this:

{handler, default, logger_std_h,
 #{config    => #{file => "console.log", type => wrap}

However, type => wrap perfectly works with logger_disk_log_h.
Maybe it’s just me not fully understanding Logger.

A wrap log is a disk_log concept which is why that type does not exist in the standard handler.

There is a list of all the parameters that the different built-in logger handlers take in the respective documentation:

1 Like

@garazdawi I better see now. Last question please.

Is this possible to create a Logger for one particular task?

Let me explain. I’d like to log all messages related to FTP into /var/lib/sftp.log.
I’ve an secure FTP server where customers deposit files. Every time a file is processed, I’d like to log a message into /var/lib/sftp.log using Logger.

There are many ways to achieve that, what I would do is to tag each log event from your ftp service with its own domain, like this:

logger:info(#{ event => reading_file, file => Filename }, #{ domain => [zabrane, ftp] }).

and then setup a new backend with a filter that only includes that domain:

logger:add_handler(ftp_logger, logger_std_h,
      #{ config => #{ file => "/var/log/sftp"},
         filters => [{ftp_domain, {fun logger_filters:domain/2,
                                   {stop, not_equal, [zabrane, ftp]}}}]}).

or doing it in sys.config:

{handler, ftp_logger, logger_std_h,
      #{ config => #{ file => "/var/log/sftp"},
         filters => [{ftp_domain, {fun logger_filters:domain/2,
                                   {stop, not_equal, [zabrane, ftp]}}}]}}
1 Like

@garazdawi you made my day :pray:

1 Like

@garazdawi I’ve noticed that calling the following always returns ok. Nothing is printed in the console:

> logger:info("hello ftp", #{ domain => [zabrane, ftp] }).
ok

Is this an expected behaviour?
How to allow printing thing in the console as well?

The default logger handler only logs messages that don’t have a domain or that are part of the otp domain. So you will have to add a filter that adds logging of your domain. You can do that like this:

logger:add_handler_filter(default, ftp_domain,
            {ftp_domain, {fun logger_filters:domain/2,
                          {log, equal, [zabrane, ftp]}}}).
1 Like

@garazdawi Indeed. This worked:

    logger:add_handler_filter(default, ftp_domain,
      {fun logger_filters:domain/2, {log, equal, [zabrane, ftp]}}),