-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHttpAppender.dart
67 lines (60 loc) · 1.7 KB
/
HttpAppender.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'package:basic_utils/basic_utils.dart';
import 'package:log_4_dart_2/log_4_dart_2.dart';
import 'package:log_4_dart_2/src/LogRecordFormatter.dart';
///
/// A appender for sending log entries via http
///
class HttpAppender extends Appender {
/// The destination for the http post request
String? url;
/// The headers to send with the request
Map<String, String>? headers;
@override
void append(LogRecord logRecord) {
logRecord.loggerName ??= getType();
var body = LogRecordFormatter.formatJson(logRecord, dateFormat: dateFormat);
HttpUtils.postForFullResponse(url!, body: body, headers: headers);
}
@override
String toString() {
return '$type $url $level';
}
@override
Future<void>? init(Map<String, dynamic> config, bool test, DateTime? date) {
created = date ?? DateTime.now();
type = AppenderType.HTTP;
if (config.containsKey('level')) {
level = Level.fromString(config['level']);
} else {
level = Level.INFO;
}
if (config.containsKey('dateFormat')) {
dateFormat = config['dateFormat'];
} else {
dateFormat = Appender.defaultDateFormat;
}
if (config.containsKey('url')) {
url = config['url'];
} else {
throw ArgumentError('Missing url argument for HttpAppender');
}
headers = {};
if (config.containsKey('headers')) {
List<String> h = config['headers'];
for (var s in h) {
var splitted = s.split(':');
headers!
.putIfAbsent(splitted.elementAt(0), () => splitted.elementAt(1));
}
}
return null;
}
@override
Appender getInstance() {
return HttpAppender();
}
@override
String getType() {
return AppenderType.HTTP.name;
}
}