Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not writing logs to file in release mode #43

Closed
vinaypwheelocity opened this issue Sep 5, 2023 · 8 comments
Closed

Not writing logs to file in release mode #43

vinaypwheelocity opened this issue Sep 5, 2023 · 8 comments

Comments

@vinaypwheelocity
Copy link

vinaypwheelocity commented Sep 5, 2023

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
Copy link
Member

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
Copy link

zzdota commented Oct 17, 2023

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
Copy link
Member

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
Copy link

zzdota commented Oct 19, 2023

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
Copy link
Member

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
Copy link

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
Copy link
Member

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants