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

no file output when exit() is reached #26

Closed mediamagnet closed 1 year ago

mediamagnet commented 1 year ago

When executing a multiWriter as part of a try catch if I have the program run an exit() as part of the error handling I get no output into a file if I comment out the exit it will write to the file as expected however execution will continue which in the case of a config file I don't want to happen... I'm not sure if I'm doing something wrong or if there is something else at play here.

Bungeefan commented 1 year ago

Hi, could you please add some code which showcases your problem?

praxa-enzo commented 1 year ago

We experience the same issue. We have a logger configured which outputs to stdout and a file. The output to stdout is fine when using exit() while the file does not contain the last log lines.

mediamagnet commented 1 year ago

sorry was on vacation when you replied

catch (e) {
    if (e is TomlException) {
      multiLogger(e.toString(), Level.wtf, 'TOML Error');
      exit(13);
    } else if (e is MySQLException) {
      multiLogger(e.toString(), Level.wtf, 'MySQL Error');
      exit(13);
    } else if (e is PathNotFoundException || e is PathAccessException) {
      multiLogger(e.toString(), Level.wtf, 'Path not found');
      exit(2);
    }

and then multilogger just calls the logger from the library so i didn't have to set it up in each of the different bits of the files

Future multiLogger(String? event, Level level, [String? message]) async {
  final logfile = io.File('tessera.log');
  var logger = Logger(
      filter: ProductionFilter(),
      printer: HybridPrinter(
        PrefixPrinter(SimplePrinter(colors: false, printTime: true)),
        error: PrefixPrinter(
            PrettyPrinter(colors: false, printEmojis: true, printTime: true)),
        warning: PrefixPrinter(
            PrettyPrinter(colors: false, printEmojis: true, printTime: true)),
        wtf: PrefixPrinter(
            wtf: 'FATAL',
            PrettyPrinter(colors: false, printEmojis: true, printTime: true)),
      ),
      output: MultiOutput([
        ConsoleOutput(),
        FileOutput(
          file: logfile,
          overrideExisting: false,
          encoding: utf8,
        ),
      ]));
  logger.log(level, event, message);
  logger.close();
}
Bungeefan commented 1 year ago

I guess I found your problem. The output is probably missing due to FileOutput being silently async, which can probably never finish flushing your logs because of your exit call. The async functionality is implemented in 2.0.0 and will make logger.close() await-able. More information: #20