daroczig / logger

A lightweight, modern and flexible, log4j and futile.logger inspired logging utility for R
https://daroczig.github.io/logger
280 stars 41 forks source link

add index and namespace to message/warnings/error handler hooks #60

Closed razz-matazz closed 3 weeks ago

razz-matazz commented 3 years ago

Hello,

In my projects I log to console as well as to logfiles.

Now I want to use

logger::log_warnings()
logger::log_errors()

to see warnings and errors in my log files, too.

Now I see those messages twice in my console. I guess that logger cannot suppress the output of RStudio, but it is possible to restrict the logging to file while all other logging messages appear at both places?

Further I suggest to change the displayed "function" for those messages. For warnings it is:

function (m) { logger::log_warn(m$message) }

Example:

[13:29:46] WARN function (m) { logger::log_warn(m$message) }: the condition has length > 1 and only the first element will be used

and for errors it is simply h:

[13:29:47] ERROR h: let's stop execution here

Maybe it should be empty? (or maybe logger is able to display line number? that would be fantastic)

gabesolomon10 commented 3 years ago

First of all, this package has been super helpful to me so far. I'm experiencing a similar issue of not seeing the right displayed "function" when using

logger::log_warnings()
logger::log_errors()

I'm using the standard json formatter (formatter_json), and when errors/warnings occur inside of functions I've written, it would be great if the json "fn" field could include which function exactly led to the error/warning. Right now, I'm seeing for all of these that the json log outputs is for example:

{"time":"2020-10-05 12:09:48","level":"WARN","ns":"base","topenv":"base","fn":"eval","msg":"DE Opimization improved within 50 iterations of the max"}

It would be super helpful to have this evaluate to the actual function (instead of "eval") which called the warning for meta-analysis of which functions in my code are leading to errors/warnings. Thanks!

daroczig commented 3 years ago

is possible to restrict the logging to file while all other logging messages appear at both places?

If I understand your request correctly, you just need to set up an appended (appander_file in your case). So I'm going to close this ticket, but please reopen if I misunderstood the problem.

daroczig commented 3 years ago

It would be super helpful to have this evaluate to the actual function (instead of "eval") which called the warning for meta-analysis of which functions in my code are leading to errors/warnings.

That's indeed a great idea, and I think should be doable by looking up the call of the message. Can you please open a separate ticket / request?

razz-matazz commented 3 years ago

Hello,

As I wrote I already use appender_file. To be precise I have:

logger::log_appender(logger::appender_console, index = 1)
logger::log_appender(logger::appender_file(
    base::paste0(here::here(), "/logs/", Sys.info()[4], "_", date$TODAY, ".log"),
    max_lines = 500L, max_files = 5L),
  index = 2
)

That's why logger::log_warnings() and logger::log_errors() lead to double outputs in the console. Once from logger, once from RStudio.

razz-matazz commented 3 years ago

@daroczig It seems that people like me who are not collaborators of the repo cannot re-open closed issues.

gabesolomon10 commented 3 years ago

It would be super helpful to have this evaluate to the actual function (instead of "eval") which called the warning for meta-analysis of which functions in my code are leading to errors/warnings.

That's indeed a great idea, and I think should be doable by looking up the call of the message. Can you please open a separate ticket / request?

@daroczig No problem! Will do.

daroczig commented 3 years ago

@razz-matazz sorry that I missed some details, I've just reopened the ticket.

To clarify: you want to log ad-hoc log requests both to the log file and the console, while having the automatically captured errors and warnings only showing up in the logfile?

If that's the case, then probably the easiest solution for this is introducing the namespace parameter in log_warnings and log_errors (and log_messages as well).

Can you please confirm that it would solve the issue?

razz-matazz commented 3 years ago

To clarify: you want to log ad-hoc log requests both to the log file and the console, while having the automatically captured errors and warnings only showing up in the logfile?

Yes, exactly.

In my case I use index instead of namespace (so to say two output channels for the same namespace), but yes, I am missing those parameters in log_warnings, log_errors and log_messages.

daroczig commented 6 months ago

Thanks for the feature request! As I am busy with other things, I don't think I have the time to work in this in the coming months, so I would highly appreciate if someone can come up with a PR -- so thus I'm now setting the related "Help wanted" label.

In short, the requirements are: adding index and namespace args to log_messages, log_warnings and log_errors.

WurmPeter commented 3 weeks ago

log_errors, log_warnings and log_messages call log_level. But log_level does not take an index (is there a reason why?).

There is only one logging function that takes an index and that is logger::with_log_threshold. But with that you can only turn off logging for one index like so:

logger::with_log_threshold({
            logger::log_level(logger::ERROR, m$message, .topcall = m$call)
          }, threshold = OFF, index = 1)

But with this you cannot silence other indices.

daroczig commented 3 weeks ago

Thanks for bringing this up, @WurmPeter.

After thinking through this, I think the specs I wrote up above actually doesn't make sense ... the index parameter is not offered in the logging functions on purpose, as that's just a mean to have multiple logger run on the same namespace, but conceptually it's not designed to allow the 1st index logger to capture something, while the 2nd index skip it (except based on log threshold).

So I think the solution is what I posted originally: introducing the namespace parameter (and not the index). @razz-matazz would that work?

This way you could set up a custom namespace for the hooks (messages/errors/warnings), and keep your other loggers separate from it (e.g. via the global namespace).

PS index is used in with_log_threshold to set the log threshold, not passed to any logging fns.

razz-matazz commented 3 weeks ago

Ok, I'll go then with a second namespace, thanks.