slog icon indicating copy to clipboard operation
slog copied to clipboard

Expansion of log macro depends on autoderef, which may pick Drain instead of Logger

Open kornelski opened this issue 5 years ago • 3 comments

use slog::Drain; // required to call .fuse() on logger
let log: Arc<slog::Logger> = log;
slog::error!(log, "WTF");

Gives:

   |
115 |                     slog::error!(log, "WTF");
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::result::Result`
    |
    = note: expected type `()`
               found type `std::result::Result<(), slog::private::NeverStruct>`

This is because log.log() finds <log as Drain>::log() rather than <log as Logger>::log().

This compiles:

let log: &slog::Logger = &log;
slog::error!(log, "WTF");

but it's PITA, because I have to use Arc to share the logger in my application, and typecasting before every use of the macros kills the convenience.

The best thing would be for log!() expansion to do let log: &slog::Logger = &log; itself instead of expecting log.log() to do that implicitly.

kornelski avatar Mar 17 '19 23:03 kornelski

Or alternatively, have log!() call slog::Logger::log(&log, ...)

tmccombs avatar Mar 18 '19 02:03 tmccombs

I have to use Arc to share the logger in my application

Logger is basically a wrapper over Arc, so that's weird.

dpc avatar Mar 18 '19 22:03 dpc

I think it's a good idea anywa, please PR. :)

dpc avatar Mar 18 '19 22:03 dpc