log method Null safety

Future<bool> log(
  1. String level,
  2. String message,
  3. {Object? error,
  4. StackTrace? stackTrace,
  5. Map<String, dynamic>? fields,
  6. Map<String, String>? tags}
)

Log a message to Humio.

The level is simply some value which makes sense to you. The message is the important part of the log statement. If you want to log an error the error and stackTrace should be given. You can provide additional values using the fields.

Humio segment data into indexes called data sources. An index will be created for each unique pair of tags.

You can call this method directly - but we recommend you call it using the HumioExtensions.

Implementation

Future<bool> log(
  String level,
  String message, {
  Object? error,
  StackTrace? stackTrace,
  Map<String, dynamic>? fields,
  Map<String, String>? tags,
}) async {
  // If no tags are specified we will create a default one
  if (tags == null)
    tags = {
      'level': level,
    };
  else if (tags['level'] == null) tags['level'] = level;

  // If we are logging this while debugging we should mark the log statement as such
  assert(() {
    tags!['debug'] = 'true';

    return true;
  }());

  if (fields == null) fields = {};

  if (!setRawMessage) fields['message'] = message;

  if (error != null) fields['error'] = error;
  if (stackTrace != null) fields['stacktrace'] = stackTrace.toString();

  dynamic event = {
    'timestamp': DateTime.now().toUtc().toIso8601String(),
    if (setRawMessage) 'rawstring': message,
    'attributes': fields
  };

  final body = {
    'tags': tags,
    'events': [event],
  };

  var requestJson = jsonEncode([body]);

  return await _dispatcher.dispatch(requestJson);
}