simc / logger

Small, easy to use and extensible logger which prints beautiful logs.
https://pub.dev/packages/logger
MIT License
1.07k stars 129 forks source link

Hybrid and Prefix Printer #44

Closed tkutcher closed 4 years ago

tkutcher commented 4 years ago

Hi - new to Flutter/Dart but this has been a helpful package. This pull request would add two LogPrinter decorators.

1) PrefixPrinter in prefix_printer.dart This allows you to wrap any logger with a prefix to all of it's logs. Inspiration for this being #2 and the AndroidStudio grep-console plugin. But this could also be used just in general to decorate any logger:

final logger = Logger(
  printer:  PrefixPrinter(PrettyPrinter())
);

for example would add prefixes ("DEBUG", "INFO", etc.) to each line in the PrettyPrinter output. But since it is a decorator you could use the same thing for any other printer.

It also has optional params for each log level to specify the prefix:

PrefixPrinter(SimplePrinter(), debug: "DBUG", error: "OOPS");

Light unit tests in prefix_printer_test.dart


2) HybridPrinter in hybrid_printer.dart This allows you to use a different printer for specific log levels. Inspiration for this is that I found with my logs if they were debug logs, I just wanted a simple one-line view of the log, and with regular logs the full PrettyPrinter log. With the HybridPrinter you can accomplish that like:

final logger = Logger(
  printer:  HybridPrinter(PrettyPrinter(), debug: SimplePrinter()),
  level: config.logLevel
);

Same thing as above where there are parameters for each level.

Light unit tests in hybrid_printer_test.dart


So, just for fun, using them together (how I currently am):

final logger = Logger(
  printer:  PrefixPrinter(
      HybridPrinter(
          PrettyPrinter(
            methodCount: 2,
            colors: false,
            printTime: true
          ),
        debug: SimplePrinter()
      )),//OneLinePrinter(),
  level: config.logLevel
);

image

The workaround for #2 thus becomes turning colors off, and using prefixes you want for the grep console plugin. Not a full fix, but works nicely for me!


Thought I'd submit this MR in hopes you wanted to add it to the project (also wasn't sure to submit it to master or another branch). Project is awesome! I think these could help with more flexibility in people's own loggers. In general, I would be happy to contribute.

Cheers,

Tim

haarts commented 4 years ago

Ow! I would like for you to add your workaround for the colorless IDE terms to the README as to help others.

tkutcher commented 4 years ago

Sounds good, thanks for the feedback! Later this evening I'll clean up those things, squash the cleanup commits, and add to the README for it on this branch.

-Tim

tkutcher commented 4 years ago

Cleaned up and squashed relevant commits. So now there is one commit for the source code, and one for the note in the README.

Thanks and let me know if there's anything else!

haarts commented 4 years ago

Well done man! Thanks for you contribution. I'll be releasing a new version with this shortly.