DataDog / dd-sdk-flutter

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

Feature Request: Expose `log` method that takes `LogLevel` parameter #594

Closed dballance closed 2 months ago

dballance commented 2 months ago

Feature description

Use Case

If you have instrumented with another logging library, it could be useful to expose a log method that takes LogLevel as a parameter to ease in mapping from another logging library to DatadogLogger.

Current State

Currently, you have to map the other logging library levels to a DD SDK method:

final logger = DatadogSdk.instance.logs?.createLogger(DatadogLoggerConfiguration());
// [LogRecord] from `logging` package
final LogRecord record = LogRecord(Level.INFO, 'message', 'LoggerName')

final logFn = switch (record.level) {
  Level.SHOUT => logger?.error,
  Level.SEVERE => logger?.error,
  Level.WARNING => logger?.warn,
  Level.INFO => logger?.info,
  Level.CONFIG => logger?.info,
  _ => logger?.debug,
};

logFn?.call(record.message, ...);

Here, the logFn is typed, but .call(...) signature is dynamic.

Proposed solution

Proposed API

final logger = DatadogSdk.instance.logs?.createLogger(DatadogLoggerConfiguration());
// [LogRecord] from `logging` package
final LogRecord record = LogRecord(Level.INFO, 'message', 'LoggerName')

final level = switch (record.level) {
  Level.SHOUT => LogLevel.emergency,
  Level.SEVERE => LogLevel.error,
  Level.WARNING => LogLevel.warning,
  Level.INFO => LogLevel.info,
  Level.CONFIG => LogLevel.notice,
  _ => LogLevel.debug,
};

logger?.log(level, message, ...)

Other relevant information

Either way you're still mapping something. The main benefit is that instead of call, which is dynamically typed, the log API would be typed.

dballance commented 2 months ago

Opened a PR in case this makes sense & is something you want to do for the API. No worries if it isn't what you're looking for! Just something I noticed while migrating from datadog_flutter.