Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing functions in the default logger implementations #45

Closed
venkatd opened this issue May 13, 2020 · 5 comments
Closed

Support passing functions in the default logger implementations #45

venkatd opened this issue May 13, 2020 · 5 comments

Comments

@venkatd
Copy link

venkatd commented May 13, 2020

Some of our logger statements involve expensive computations like formatting objects to make the logs easy to read. To work around this, we are passing functions for the expensive log statements and they are getting evaluated in the printer right before they need to be printed.

For example:

    logger.d(() =>
        'SEND ref=$ref topic=$topic event=$event payload=' +
        summarizeGqlPayload(payload).toString());

summarizeGqlPayload in this case is relatively expensive and something we wouldn't want to be execute in production. Or at least all these little things add up to increased CPU usage. Hence we are passing a function.

Our corresponding stringify in our custom logger looks something like this to handle functions.

  String stringifyMessage(dynamic message) {
    final finalMessage = message is Function ? message() : message;
    if (finalMessage is Map) {
      return _encoder.convert(finalMessage);
    } else if (finalMessage is Iterable) {
      return _encoder.convert(finalMessage);
    } else {
      return finalMessage.toString();
    }
  }

Is this worth having as a default for the logger?

@mercutiodesign
Copy link

I was just looking for exactly this functionality and I think it would be a nice enhancement.

@venkatd
Copy link
Author

venkatd commented May 25, 2020

@mercutiodesign for now your best bet would be to implement your own printer by copying/pasting the closest one to what you need. Can check if the message is a function and evaluate it before you print.

@haarts
Copy link
Collaborator

haarts commented May 27, 2020

I rather like the idea! Since the message argument to a log call (like d) is dynamic already this should be relatively straightforward.

@smotastic
Copy link
Contributor

Hi, needed exactly the same but saw that there is no PR for this, so I just did it.
See #105

@haarts
Copy link
Collaborator

haarts commented Aug 13, 2021

Closed since #105 is merged.

@haarts haarts closed this as completed Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants