DataDog / dd-sdk-flutter

Flutter bindings and tools for utilizing Datadog Mobile SDKs
Apache License 2.0
41 stars 40 forks source link

Using `simpleConsolePrintForLevel()` shows all logs in the console at the same log level #575

Closed nwarriner closed 3 months ago

nwarriner commented 3 months ago

Describe the bug

Using simpleConsolePrintForLevel() shows all logs in the console at the same log level. It also doesn't show logs that are the same level as this function's input.

Reproduction steps

final loggerConfiguration = DatadogLoggerConfiguration(
  customConsoleLogFunction: simpleConsolePrintForLevel(LogLevel.debug),
);
logger = DatadogSdk.instance.logs?.createLogger(loggerConfiguration);
logger?.debug("A debug message");
logger?.info("Some relevant info");
logger?.warn("An important warning...");
logger?.error("An error was met!");

SDK logs

flutter: [debug] Some relevant info
flutter: [debug] An important warning...
flutter: [debug] An error was met!

Expected behavior

Those logs should have been printed at their actual levels and not debug. It also should have printed the log: A debug message. I looked into the code for simpleConsolePrintForLevel and I think it should be refactored to look like this:

/// Print all logs to the console above the given level in the form
/// "[level] message" when [kDebugMode] is true.
CustomConsoleLogFunction simpleConsolePrintForLevel(LogLevel level) {
  return ((inLevel, message, errorMessage, errorKind, stackTrace, attributes) {
    if (kDebugMode) {
      if (inLevel.index >= level.index) {
        print('[${inLevel.name}] $message');
      }
    }
  });
}

Affected SDK versions

2.3.0

Latest working SDK version

No response

Did you confirm if the latest SDK version fixes the bug?

Yes

Flutter Version

3.19.3

Setup Type

Flutter Application

Device Information

iOS and Web

Other relevant information

No response

fuzzybinary commented 3 months ago

Hi @nwarriner,

That all looks correct. If you want to issue a PR with the fix I'd be happy to get it merged in for you.

nwarriner commented 3 months ago

I created a custom console log function that prints with color that I'll be using instead:

CustomConsoleLogFunction coloredConsolePrint({required LogLevel minLevelToShow}) {
  return (inLevel, message, errorMessage, errorKind, stackTrace, attributes) {
    if (kDebugMode) {
      if (inLevel.index >= minLevelToShow.index) {
        final color = _getColorCode(inLevel);
        print('$color[Datadog] (${inLevel.name}) $message\x1B[0m');
      }
    }
  };
}

String _getColorCode(LogLevel level) {
  switch (level) {
    case LogLevel.debug:
      return '\x1B[90m'; // Grey text
    case LogLevel.info:
      return '\x1B[97m'; // White text
    case LogLevel.warning:
      return '\x1B[33m'; // Yellow text
    case LogLevel.error:
      return '\x1B[31m'; // Red text
    default:
      return '\x1B[97m'; // White text
  }
}
fuzzybinary commented 3 months ago

This will be fixed in the next release.

vineetpal88 commented 2 days ago

i am using this version: datadog_flutter_plugin: ^2.5.0 After implementing this: final logConfiguration = DatadogLoggerConfiguration( name: LoggerServiceType.appLoggerService, remoteLogThreshold: LogLevel.debug, networkInfoEnabled: true, ); _appLogger = datadog.logs?.createLogger(logConfiguration); _appLogger ?.info('logger initialized'); _appLogger ?.error('logger error initialized');

I am not able to see full log on datadog log management dashboard that printed by android studio logcat view.

I am able to see those log who i write manually send to log management dashboard. like as mention below _appLogger ?.info('logger initialized'); _appLogger ?.error('logger error initialized');

I am explaining through some example what i want? the below log that printed on my android studio log view but it's not reflected on datadog dashboard

2024-06-28 22:07:30.765 22926-23216 flutter com.shout.dating I AppLifecycleEventMain::: paused 2024-06-28 22:07:30.765 22926-23216 flutter com.shout.dating I test{memberId: 19, activeStatus: offline} 2024-06-28 22:07:30.767 22926-23216 flutter com.shout.dating I AppLifecycleEventMain::: detached 2024-06-28 22:07:30.767 22926-23216 flutter com.shout.dating I test{memberId: 19, activeStatus: online} 2024-06-28 22:07:30.782 22926-23216 flutter com.shout.dating I Receive the onDetachedFromEngine callback, clean the native resources. 2024-06-28 22:07:30.821 22926-23218 libMEOW com.shout.dating D meow delete tls: 0xb4000071cb401180 2024-06-28 22:07:30.822 22926-23217 libMEOW com.shout.dating D meow delete tls: 0xb4000071836234c0 2024-06-28 22:07:30.824 22926-22926 Activity com.shout.dating W PerfMonitor: Slow Operation: Activity com.shout.dating/.MainActivity onDestroy took 294ms 2024-06-28 22:07:30.831 22926-22926 View com.shout.dating D [Warning] assignParent to null: this = DecorView@b90eb5c[MainActivity] 2024-06-28 22:07:30.841 22926-22926 FileUtils com.shout.dating E err write to mi_exception_log 2024-06-28 22:07:30.847 22926-22926 FileUtils com.shout.dating E err write to mi_exception_log 2024-06-28 22:07:30.866 24226-28980 ActivityManagerWrapper com.miui.home E getRecentTasks: mainTaskId=21365 userId=0 baseIntent=Intent { act=android.intent.action.MAIN flag=268435456 cmp=ComponentInfo{com.shout.dating/com.shout.dating.MainActivity} }

vineetpal88 commented 2 days ago

@fuzzybinary can you please help me on this

fuzzybinary commented 1 hour ago

@vineetpal88 Datadog doesn't intercept all logs from the system logger (in your case the Android Studio Logcat) by design. We only send the items specifically sent to Datadog loggers.