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

Broken file output #51

Closed KevinCFechtel closed 8 months ago

KevinCFechtel commented 8 months ago

Hello, I have the problem that the logger unfortunately does not send all log events to the file log output, or that some of them are written broken to the file. The console output works without any problems. As an example I have here the file output and the console output. File output:

[I] TIME: 2023-12-25T14:11:13.242861 FluxNews - initializeDB : Starting initializeDB
ig values[I] TIME: 2023-12-25T14:11:13.264610 FluxNews - initializeDB : Finished initializeDB
[I] TIME: 2023-12-25T14:11:13.353699 FluxNews - renewAllNewsCount : Starting
rene
ing all news c
unt
ter
[I] TIME: 2023-12-25T14:11:13.371997 FluxNews - queryNewsFromDB : Finished querying news from DB[I] TIME: 2023-12-25T14:11:13.373921 FluxNews - updateNewsStatusInDB : Finished updating starred counter
[I] TIME: 2023-12-25T14:11:13.376115 FluxNews - renewAllNewsCount : Finished renewing all news count
[I] TIME: 2023-12-25T14:11:13.427975 FluxNews - queryCategoriesFromDB : Finished querying categories
from DB[I] TIME: 2023-12-25T14:11:13.429207 FluxNews - renewAllNewsCount : Finished renewing all news count

Console output:

flutter: [I] TIME: 2023-12-25T14:11:13.240602 FluxNews - readConfigValues : Finished read config values
flutter: [I] TIME: 2023-12-25T14:11:13.242861 FluxNews - initializeDB : Starting initializeDB
flutter: [I] TIME: 2023-12-25T14:11:13.248581 FluxNews - initializeDB : Finished initializeDB
flutter: [I] TIME: 2023-12-25T14:11:13.264610 FluxNews - initializeDB : Finished initializeDB
flutter: [I] TIME: 2023-12-25T14:11:13.349413 FluxNews - readConfig : Starting read config
flutter: [I] TIME: 2023-12-25T14:11:13.350533 FluxNews - module : Value: Oldest first
flutter: [I] TIME: 2023-12-25T14:11:13.351978 FluxNews - readConfig : Finished read config
flutter: [I] TIME: 2023-12-25T14:11:13.352651 FluxNews - queryNewsFromDB : Starting querying news from DB
flutter: [I] TIME: 2023-12-25T14:11:13.353424 FluxNews - updateNewsStatusInDB : Starting updating starred counter
flutter: [I] TIME: 2023-12-25T14:11:13.353699 FluxNews - renewAllNewsCount : Starting renewing all news count
flutter: [I] TIME: 2023-12-25T14:11:13.371997 FluxNews - queryNewsFromDB : Finished querying news from DB
flutter: [I] TIME: 2023-12-25T14:11:13.373921 FluxNews - updateNewsStatusInDB : Finished updating starred counter
flutter: [I] TIME: 2023-12-25T14:11:13.376115 FluxNews - renewAllNewsCount : Finished renewing all news count
flutter: [I] TIME: 2023-12-25T14:11:13.427651 FluxNews - renewAllNewsCount : Starting renewing all news count
flutter: [I] TIME: 2023-12-25T14:11:13.427975 FluxNews - queryCategoriesFromDB : Finished querying categories from DB
flutter: [I] TIME: 2023-12-25T14:11:13.429207 FluxNews - renewAllNewsCount : Finished renewing all news count

This is the code that is used for logging:

import 'dart:io';

import 'package:flux_news_desktop/flux_news_state.dart';
import 'package:intl/intl.dart';
import 'package:logger/logger.dart';

void logThis(
    String module, String message, Level loglevel, FluxNewsState appState) {
  if (appState.loggingFilePath != "") {
    DateTime now = DateTime.now();
    DateFormat formatter = DateFormat('yyyy-MM-dd');
    String formattedDate = formatter.format(now);
    File logFile =
        File('${appState.loggingFilePath}/FluxNewsLogs-$formattedDate.txt');
    String logMessage = "${FluxNewsState.logTag} - $module : $message";
    Logger logger = Logger(
      filter: ProductionFilter(),
      printer: SimplePrinter(printTime: true, colors: false),
      output: MultiOutput([FileOutput(file: logFile), ConsoleOutput()]),
    );
    logger.log(loglevel, logMessage);
  } else {
    String logMessage = "${FluxNewsState.logTag} - $module : $message";
    Logger logger = Logger(
      printer: PrettyPrinter(methodCount: 0),
    );
    logger.log(loglevel, logMessage);
  }
}
Bungeefan commented 8 months ago

Hi, it seems that you are creating a new logger every time you are logging something. This means a new file handle is created every time you call your logThis function. This could mess with the file handling of Dart, resulting in race-conditions and other weird behavior, which would explain the broken and missing logs in your file.

Please try making the logger static and re-use it for all your log calls, then test again.

KevinCFechtel commented 8 months ago

Hi, thank you very much, that was exactly the mistake! The logger is now working correctly.