skip to Main Content

I want to release my Flutter app and I want to find a way to disable all logs in my app

late Logger logger;

void main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  await initializeDependencies();
  logger = Logger(
      printer: PrettyPrinter(
          methodCount: 0,
          errorMethodCount: 0,
          lineLength: 150,
          colors: true,
          printEmojis: true));
  runApp(const MyApp());
}

2

Answers


  1. While there isn’t a direct way to accomplish this, here’s what you can do:

     Logger? logger; // <== Make Logger nullable
    
    void main() async {
      WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
      FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
      await initializeDependencies();
     // logger = Logger(
     //   printer: PrettyPrinter(
     //      methodCount: 0,
     //      errorMethodCount: 0,
     //      lineLength: 150,
     //      colors: true,
     //      printEmojis: true));
      runApp(const MyApp());
    }
    

    Log statement know looks like : –

    logger?.d("Logger is working!");
    
    Login or Signup to reply.
  2. I want to release my Flutter app and I want to find a way to disable
    all logs in my app

    It seems you want to disable the logs in the release mode. In this case, you don’t have to worry about it.

    The default filter of The Logger behaves as only logging in debug mode. Citing from the documentation:

    var logger = Logger(
      filter: null, // Use the default LogFilter (-> only log in debug mode)
      printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log
      output: null, // Use the default LogOutput (-> send everything to console)
    );
    

    The default value of the filter parameter would be DevelopmentFilter, which means:

    In release mode ALL logs are omitted.


    However, you can create a custom filter (for whatever reason, such as hiding logs based on their levels). Citing from the documentation:

    class MyFilter extends LogFilter {
      @override
      bool shouldLog(LogEvent event) {
        return true;
      }
    }
    

    And interestingly, it’s mentioned that:

    This will show all logs even in release mode. (NOT a good idea)

    which is exactly what you want to avoid according to what you are looking for!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search