dart-lang / core

This repository is home to core Dart packages.
https://pub.dev/publishers/dart.dev
BSD 3-Clause "New" or "Revised" License
17 stars 4 forks source link

Shortcut for creating child loggers #478

Open predatorx7 opened 1 year ago

predatorx7 commented 1 year ago
class Logger {
  // ...

  Logger call(String childName) {
    return Logger('$fullName.$childName');
  }
}

With the above call method in the Logger class, creating child loggers could be more convenient.

Example

For a method foo, and bar in class MyClass:

Current way

class MyClass {
  void foo() {
    final log = Logger('MyClass.foo');
    log.info('..some log..');
  }
  void bar() {
    final log = Logger('MyClass.bar');
    log.info('..some log..');
  }
}

Proposed way

class MyClass {
  final log = Logger('MyClass');
  void foo() {
    final log = this.log('foo');
    log.info('..some log..');
  }
  void bar() {
    final log = this.log('bar');
    log.info('..some log..');
  }
}
BEEugene commented 7 months ago

Would be like in Python logger getChild method, it would be nice :) Also it could be done like this without any additional modification final child = Logger('${log.fullName}.childname');

predatorx7 commented 7 months ago

I agree. That's how I'm creating child loggers right now.