Coder Social home page Coder Social logo

nivisi / contextual_logging Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 16 KB

✏️ A mixin for Dart classes that brings contextual logging functionality.

Home Page: https://pub.dev/packages/contextual_logging

License: MIT License

Dart 100.00%
context contextual dart flutter library log logging package plugin pub pubdev dartlang logger logging-library

contextual_logging's Introduction

contextual_logging pub version

✏️ A mixin for Dart classes that brings contextual logging functionality to your class.

Print messages like this w/o any effort.

10:03:00 [I] MyController : Initializing ...
10:03:01 [I] MyController : Initialized!

🌐 Is built on top of the logger package.

Table of contents

What's contextual logging?

We all know log messages. They are printed to the console, to the files or whatever. Dart provides us with methods for logging like:

print('A message');
debugPrint('Another message');

Good enough to debug. But when you actually need to investigate users' journey, it is not. You'll need context. The context here answers the question who has printed the message?.

Adding context could be done like this:

print('My Controller : A message');
debugPrint('My Controller : Another message');

... and it will work. Though to write the context every time is pretty boring. This is what contextual_logging solves.

Logger

The mixin

Attach the mixin it to your class that you want to use for logging:

class MyController with ContextualLogging

And now go for it!

class MyController with ContextualLogging {
  Future<void> init() async {
    log.i('Initializing ...'); // <-- Access logging via the log field
    await fetchData();
    log.i('Initialized!');
  }
}

You will see this in the console:

10:03:00 [I] MyController : Initializing ...
10:03:01 [I] MyController : Initialized!

Configuration

By default, a logger is created for every object that has a ContextualLogging mixin attached to it. Once you attach the mixin, you'll be able to configure the logger for this object.

Context

logContext property is what Contextual Logger adds to the log message in front of the main message. By default it has a value of this.toString(). Override it to whatever you want:

class MyController with ContextualLogging {
  @override
  String get logContext => 'SomeOtherContext`;
  
  void test() {
    log.i('Test'); // 19:12:00 [I] SomeOtherContext : Test
  }
}

Custom logger

If you want to use a custom logger, feel free to override the customLogger property:

class MyController with ContextualLogging {
  @override
  Logger get customLogger => Logger(/* Configure it in whatever way you want! */);
  
  void test() {
    log.i('Test'); // Still access it via the `log` property!
  }
}

Default logger config everywhere

If you want to reconfigure loggers for all the object at once, do this before your app starts:

// ContextualLogger mixin uses this defaultLogger by default to get a logger for the object it was attached to.
ContextualLoggingConfig.defaultLogger = (forObject) => MyBeautifulLogger(forObject);

Once you do it, every ContextualLogger mixin will create loggers like this.

Contextual Log Printer

💡 A printer is what formats your messages.

What's this?

When setting the ContextualLoggingConfig.defaultLogger property, you can create a logger and provide any printer you want. Or you can use the default printer used by ContextualLogger, the ContextualLogPrinter. This printer is what makes the messages look like this:

12:01:00 [I] SomeOtherContext : Test

Instead of this:

Test

Configuring the printer

There are plenty of properties you can change:

Property Type Description Default
forObject Object The object for which the logger was created. Use a string if you want to have it as a prefix. this
timeFormat DateFormat Format of the current timestamp HH:mm:ss
timeInUtc bool Whether the current timestamp must be in UTC false
printTime bool Whether to print the timestamp true
logLevelLabel Function Log level prefix for messages [I], [W] etc

Example

So imagine you've overridden the printer like this:

ContextualLoggingConfig.defaultLogger = (forObject) {
  return Logger(
    printer: ContextualLogPrinter(
      forObject: forObject,
      printTime: false, // Note!
    ),
  );
};

// This will make your messages look like this:

[I] MyController : A message

Log level label

Log level lebel is what allows you to distinguish the level of a message. The logger package allows you to use these levels:

Level Function Default Emoji
Verbose log.v [V] None
Debug log.d [D] 🐛
Info log.i [I] 💡
Warning log.w [W] ⚠️
Error log.e [E] ⛔️
Wtf log.wtf [WTF] 🗿
Nothing log.log(Level.nothing, ...) None None

Override the logLevelLabel property to make your own prefixes!

ContextualLoggingConfig.defaultLogger = (forObject) {
  return Logger(
    printer: ContextualLogPrinter(
      forObject: forObject,
      logLevelLabel: (level) {
        /* Return a prefix for the given level! */
      },
    ),
  );
  
// Or do this to enable emojis level!
ContextualLoggingConfig.defaultLogger = (forObject) {
  return Logger(
    printer: ContextualLogPrinter(
      forObject: forObject,
      logLevelLabel: ContextualLogPrinter.emojiLevelLabel,
    ),
  );

contextual_logging's People

Contributors

nivisi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.