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

Not writing logs to file in release mode #43

Closed vinaypwheelocity closed 1 year ago

vinaypwheelocity commented 1 year ago

Code :

Future<File> _getFileName(
      {bool forCreate = true, DateTime? requiredDate}) async {
    DateTime date = requiredDate ?? DateTime.now();
    Directory directory = Directory(
        "${(await getExternalStorageDirectory())?.path}/foreground_task_logs");
    if (!directory.existsSync()) {
      if (forCreate) {
        directory.createSync();
      } else {
        throw Exception('Directory Not Created');
      }
    }
    final String fileName =
        "${directory.path}/${DateFormat("dd-MM-yyyy").format(date)}.txt";
    print("File Path = $fileName");
    File outputFile = File(fileName);
    print("File Exists = ${outputFile.existsSync()}");
    if (!outputFile.existsSync()) {
      if (forCreate) {
        outputFile.createSync();
      } else {
        throw Exception("File Not Created");
      }
    }

    return outputFile;
  }

  Future<Logger> getLogger() async {
    try {
      return Logger(
          printer: PrettyPrinter(
            methodCount: 0,
            printTime: false,
            noBoxingByDefault: true,
          ),
          output: FileOutput(file: await _getFileName()));
    } catch (e) {
      throw Exception(e);
    }
  }
Bungeefan commented 1 year ago

Hi, it seems that you have not provided a custom filter, by default the DevelopmentFilter is used which by design doesn't log in release mode. You can change that by providing the following to your logger:

Logger(
  filter: ProductionFilter(),
  // ...
)
zzdota commented 10 months ago

Hi, it seems that you have not provided a custom filter, by default the DevelopmentFilter is used which by design doesn't log in release mode. You can change that by providing the following to your logger:

Logger(
  filter: ProductionFilter(),
  // ...
)

Even if the code is modified, the log cannot be written to the file in release mode, but it can be written to the file normally in debug mode. Method to reproduce this problem: package a release mode apk file and install the apk on the phone. The log cannot be written to the file.

Even if I use the following code, it doesn't work

class MyFilter extends LogFilter {
   @override
   bool shouldLog(LogEvent event) {
     return true;
   }
}
Bungeefan commented 10 months ago

Hi @zzdota, I tried to reproduce your problem but wasn't able to identify any issues.

I created a FileOutput with a File in the ApplicationDocumentsDirectory, used the ProductionFilter and tried it in debug and release mode, as well as in an emulator and a real device (Android). In every case, it successfully created the file with the proper log output.

Test Code:

late File logFile;

Directory applicationDir = await getApplicationDocumentsDirectory();
logFile = File("${applicationDir.path}${Platform.pathSeparator}test.log");

// ...

static final Logger log = Logger(
    filter: ProductionFilter(),
    output: !kIsWeb
        ? MultiOutput([
            Logger.defaultOutput(),
            FileOutput(file: logFile),
          ])
        : Logger.defaultOutput(),
);
zzdota commented 10 months ago

Hi @zzdota, I tried to reproduce your problem but wasn't able to identify any issues.

I created a FileOutput with a File in the ApplicationDocumentsDirectory, used the ProductionFilter and tried it in debug and release mode, as well as in an emulator and a real device (Android). In every case, it successfully created the file with the proper log output.

Test Code:

late File logFile;

Directory applicationDir = await getApplicationDocumentsDirectory();
logFile = File("${applicationDir.path}${Platform.pathSeparator}test.log");

// ...

static final Logger log = Logger(
    filter: ProductionFilter(),
    output: !kIsWeb
        ? MultiOutput([
            Logger.defaultOutput(),
            FileOutput(file: logFile),
          ])
        : Logger.defaultOutput(),
);

First of all, I admit that you are right, flutter build --release, this way of debugging a mobile phone with a data cable is normal; However, if you use flutter build apk to package the apk, install the apk on your phone, and then run this application, this application uses the logger library to print logs and store the logs in files, something will appear. The log is not written to the file,

Bungeefan commented 10 months ago

Hi @zzdota, again, I tried to reproduce it and couldn't find any problem. I built the APKs (release and debug), transferred them over to the phone, installed them manually and the log file was created just fine in both modes.

Another question, as the device file explorer isn't available for release builds, how do you check if the file exists? And in which location are you trying to save it?

magic3584 commented 8 months ago

Hi @zzdota, again, I tried to reproduce it and couldn't find any problem. I built the APKs (release and debug), transferred them over to the phone, installed them manually and the log file was created just fine in both modes.

Another question, as the device file explorer isn't available for release builds, how do you check if the file exists? And in which location are you trying to save it?

So, If

the device file explorer isn't available for release builds

how can you be sure that

the log file was created just fine in both modes.?

Is the device need to be root?

Bungeefan commented 8 months ago

So, If

the device file explorer isn't available for release builds

how can you be sure that

the log file was created just fine in both modes.?

Is the device need to be root?

Hi @magic3584, my app tells me that. I just coded a simple file check that tells me if the log file exists and what its content is. Yeah, a rooted phone would work too.

magic3584 commented 8 months ago

@Bungeefan Thanks guy, I have written a demo and you are right.