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

Getting <anonymous closure> <asynchronous suspension> using logger in Async Function #54

Closed ryanadrig closed 5 months ago

ryanadrig commented 7 months ago

Logger works seemingly normally except in async functions, where it still outputs the desired log but gets extra exceptions thrown compared to print. I tested this in two places. Here is a minimal example of my settings. TestDelHTTP.<anonymous closure> is thrown in every call to lg.d, and TestDelHTTP.<asynchronous suspension> is thrown in the "then" block, but no exceptions are thrown just using print.

Map<Level, String> customLevelEmojis = {
  Level.trace: '🔦',
  Level.debug: '🖌',
  Level.info: 'ℹ️',
  Level.warning: '⚠️',
  Level.error: '🚫',
  Level.fatal: '👾',
};

Logger lg = Logger(
  printer: PrettyPrinter(levelEmojis: customLevelEmojis ),
  level: Level.debug
);

TestDelHTTP(
    WidgetRef ref,
    String opDescription,
    Function showErrorDialog,
    Function showOpResultsPage
    )async {
  ref.read(opIsRunningProv.notifier).state = true;
  ref.read(opDescriptionProv.notifier).state = opDescription;
  lg.d("test del http");
    await http.post(
      Uri.parse("https://10.0.2.2:1515/test_del_http"),
      headers: {'Content-Type': 'application/json',},
    ).then((resp){
      lg.d("Test Del HTTP RESP ~ " + resp.body);
      showOpResultsPage(resp.body);
    }).catchError((error){
      lg.d("error testing del http " + error);
      lg.d("test del http error");
      showErrorDialog(error);
  }).whenComplete((){
      lg.d("test del http complete");
    ref.read(opDescriptionProv.notifier).state = "";
    ref.read(opIsRunningProv.notifier).state = false;
  });

}
aalpersen commented 7 months ago

Same here. if i put logger statement somewhere in begging of async code it does not trigger async suspension. but if it is somewhere in middle throws a suspension

Bungeefan commented 7 months ago

Hi, unfortunately, I think I am not quite able to understand your issue.

What do you mean by "is thrown in every call to lg.d"?

@aalpersen, regarding <asynchronous suspension>, this expression is not an exception nor an error, just a notice regarding your async code flow. More information: https://github.com/flutter/flutter/issues/28588#issuecomment-468160204.


My guess would be that you are confusing an exception stack trace with the default stack trace that is printed by PrettyPrinter's methodCount (default = 2), which is even printed when no exception was provided. Actually, the methodCount is only used when no exception was provided.

Additionally, the output is just a stack trace (obtained via StackTrace.current) and not an exception.

Bungeefan commented 5 months ago

Without more information, I am not able to assist you any further. Therefore, I am going to close this issue.