formatCloudLoggingLog function

LogFormatter formatCloudLoggingLog({
  1. required String projectId,
})

Formats the log data into the proper format for Google Cloud Logging

Implementation

LogFormatter formatCloudLoggingLog({required String projectId}) => ({
      required Severity severity,
      required String message,
      required Map<String, String?> headers,
      Map<String, dynamic>? payload,
      Map<String, dynamic>? labels,
      bool? isError,
      Chain? chain,
      Frame? stackFrame,
    }) {
      final trace = headers['X-Cloud-Trace-Context']?.split('/').first;

      final log = <String, dynamic>{
        if (isError ?? false)
          '@type':
              'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent',
        ...?payload,
        'severity': severity.toString(),
        'message': message,
        if (trace != null)
          'logging.googleapis.com/trace': 'projects/$projectId/traces/$trace',
        if (labels != null && labels.isNotEmpty)
          'logging.googleapis.com/labels': labels,
        if (chain != null) 'stackTrace': chain.toString().trim(),
        if (stackFrame != null)
          'logging.googleapis.com/sourceLocation':
              frameToSourceInformation(stackFrame),
      };
      return jsonEncode(log);
    };