SourceHorizon / logger

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

There are no logs in cli application #72

Closed sc0rp10 closed 4 months ago

sc0rp10 commented 4 months ago

Hi! I try to create console application on Dart and logs is very important for this application, but, unfortunately, I can't see any log messages in the console even with very simple code from the example:

I created a new empty project

$ dart create . --force
Creating testlogger using template console...

  .gitignore
  analysis_options.yaml
  CHANGELOG.md
  pubspec.yaml
  README.md
  bin/testlogger.dart
  lib/testlogger.dart
  test/testlogger_test.dart

Running pub get...                     1.1s
  Resolving dependencies...
  Changed 47 dependencies!
  4 packages have newer versions incompatible with dependency constraints.
  Try `dart pub outdated` for more information.

Created project testlogger in .! In order to get started, run the following commands:

  cd .
  dart run

Then I added the Logger library:

$ dart pub add logger
Resolving dependencies...
  lints 2.1.1 (3.0.0 available)
+ logger 2.2.0
  vm_service 14.0.0 (14.2.1 available)
  web 0.4.2 (0.5.1 available)
  web_socket_channel 2.4.3 (2.4.5 available)
Changed 1 dependency!
4 packages have newer versions incompatible with dependency constraints.
Try `dart pub outdated` for more information.

I wrote dead simply script based on examples:

$ cat bin/testlogger.dart
import 'package:logger/logger.dart';

void main(List<String> arguments) {
    var logger = Logger();

    logger.w("Logger is working!");

    print("any output");
}

Finally I run the script:

$ dart bin/testlogger.dart
any output

But, unfortunately, I didn't see any logs in my console. I tried many different options - filters, outputs, levels but there is still no luck.

What can be wrong?

sc0rp10 commented 4 months ago

I dig into the code and found problematic string:

https://github.com/SourceHorizon/logger/blob/main/lib/src/filters/development_filter.dart#L12

var shouldLog = false;
assert(() {
  if (event.level.value >= level!.value) {
    shouldLog = true;
  }
  return true;
}());

Asserts have to be enabled to allow logger print logs. It's better to add this information to the readme at least, but IMO the logger shouldn't rely on asserts.

Bungeefan commented 4 months ago

Hi, this is already noted in the README, as well as in the documentation of the default filter: https://github.com/SourceHorizon/logger?tab=readme-ov-file#logfilter

The LogFilter decides which log events should be shown and which don't.
The default implementation (DevelopmentFilter) shows all logs with level >= Logger.level while in debug mode (i.e., running dart with --enable-asserts). In release mode all logs are omitted.

You are free to use the ProductionFilter which doesn't rely on asserts (i.e. debug mode).