logEvent method

Future<void> logEvent({
  1. String? message,
  2. Map<String, Object?>? args,
  3. GoogleLoggerSeverity? severity = GoogleLoggerSeverity.DEFAULT,
  4. Map<String, String>? labels,
  5. LogEntryOperation? operation,
  6. String? functionName,
  7. String? fileName,
})

Implementation

Future<void> logEvent({
  String? message,
  Map<String, Object?>? args,
  GoogleLoggerSeverity? severity = GoogleLoggerSeverity.DEFAULT,
  Map<String, String>? labels,
  LogEntryOperation? operation,
  String? functionName,
  String? fileName,
}) async {
  try {
    final Map<String, Object> params = {};
    if (message is String) {
      params['message'] = message;
    }
    if (args is Map<String, Object?>) {
      args.forEach((key, value) {
        if (value != null) {
          params[key] = value.toString();
        }
      });
    }
    final logEntry = LogEntry(
        logName: 'projects/$projectId/logs/flutter-log',
        jsonPayload: params,
        severity: severity.toString().split('.').last,
        operation: operation,
        sourceLocation: LogEntrySourceLocation(
          function: functionName,
          file: fileName,
        ),
        resource: MonitoredResource(type: 'global'),
        labels: labels);
    final req = WriteLogEntriesRequest(entries: [logEntry]);
    _logger?.entries.write(req);
  } catch (err) {
    debugPrint(err.toString());
  }
}