log method
Log a message to Humio.
The severity
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 severity,
String message, {
Object error,
StackTrace stackTrace,
Map<String, dynamic> fields,
Map<String, String> tags,
}) async {
if (_ingestToken?.isEmpty ?? true)
throw 'Humio ingest token is not defined';
if (ingestUrl?.isEmpty ?? true) throw 'Humio ingest URL is not defined';
// If no tags are specified we will create a default one
if (tags == null)
tags = {
'environment': 'dev',
};
else if (tags['environment'] == null) tags['environment'] = 'dev';
// 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;
var attributes = Map<String, dynamic>();
if (error != null) attributes['error'] = error;
if (stackTrace != null) attributes['stacktrace'] = stackTrace.toString();
if (fields != null) attributes['fields'] = fields;
dynamic event = {
'timestamp': DateTime.now().toUtc().toIso8601String(),
if (setRawMessage) 'rawstring': message,
'attributes': attributes
};
final body = {
'tags': tags,
'events': [event],
};
var requestJson = jsonEncode([body]);
var dio = Dio();
Response<dynamic> response;
try {
response = await dio.post(
ingestUrl,
data: requestJson,
options: Options(
contentType: 'application/json',
headers: {'Authorization': 'Bearer $_ingestToken'},
),
);
} on DioError catch (e) {
print('Humio log error: ${e.message}');
return false;
}
return response.statusCode == 200;
}