logging icon indicating copy to clipboard operation
logging copied to clipboard

Shortcut for creating child loggers

Open predatorx7 opened this issue 1 year ago • 2 comments

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..');
  }
}

predatorx7 avatar Apr 06 '23 05:04 predatorx7

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');

BEEugene avatar Mar 29 '24 18:03 BEEugene

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

predatorx7 avatar Mar 29 '24 19:03 predatorx7