-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEmailAppender.dart
153 lines (144 loc) · 3.86 KB
/
EmailAppender.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'dart:io';
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';
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
///
/// A appender for sending log entries via email
///
class EmailAppender extends Appender {
String? host;
String? user;
String? password;
int? port;
String? fromMail;
String? fromName;
late List<Address> to;
List<Address>? toCC;
List<Address>? toBCC;
bool? ssl = false;
late SmtpServer _smtpServer;
late PersistentConnection _connection;
String? templateFile;
String? template;
bool? html = false;
@override
void append(LogRecord logRecord) async {
logRecord.loggerName ??= getType();
final message = Message()
..from = Address(fromMail!, fromName)
..recipients.addAll(to)
..subject =
'Logger ${logRecord.level} at ${logRecord.getFormattedTime()}';
if (html!) {
message.html = LogRecordFormatter.formatEmail(template, logRecord,
dateFormat: dateFormat);
} else {
message.text = LogRecordFormatter.formatEmail(template, logRecord,
dateFormat: dateFormat);
}
if (IterableUtils.isNotNullOrEmpty(toCC)) {
message.ccRecipients.addAll(toCC!);
}
if (IterableUtils.isNotNullOrEmpty(toBCC)) {
message.bccRecipients.addAll(toBCC!);
}
try {
await _connection.send(message);
} catch (e) {
print(e);
return;
}
}
@override
String toString() {
return '$type $host $port $user $level';
}
@override
Future<void>? init(Map<String, dynamic> config, bool test, DateTime? date) {
created = date ?? DateTime.now();
type = AppenderType.EMAIL;
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('host')) {
host = config['host'];
} else {
throw ArgumentError('Missing host argument for EmailAppender');
}
if (config.containsKey('user')) {
user = config['user'];
}
if (config.containsKey('password')) {
password = config['password'];
}
if (config.containsKey('port')) {
port = config['port'];
} else {
throw ArgumentError('Missing port argument for EmailAppender');
}
if (config.containsKey('fromMail')) {
fromMail = config['fromMail'];
} else {
fromMail = user;
}
if (config.containsKey('fromName')) {
fromName = config['fromName'];
} else {
fromName = user;
}
if (config.containsKey('to')) {
to = [];
for (String s in config['to']) {
to.add(Address(s));
}
} else {
throw ArgumentError('Missing to argument for EmailAppender');
}
if (config.containsKey('toCC')) {
toCC = [];
for (String s in config['toCC']) {
toCC!.add(Address(s));
}
}
if (config.containsKey('toBCC')) {
toBCC = [];
for (String s in config['toBCC']) {
toBCC!.add(Address(s));
}
}
if (config.containsKey('ssl')) {
ssl = config['ssl'];
}
if (config.containsKey('html')) {
html = config['html'];
}
if (!test) {
_smtpServer = SmtpServer(host!,
port: port!, username: user, password: password, ssl: ssl!);
_connection = PersistentConnection(_smtpServer);
}
if (config.containsKey('templateFile')) {
templateFile = config['templateFile'];
var file = File(templateFile!);
template = file.readAsStringSync();
}
return null;
}
@override
Appender getInstance() {
return EmailAppender();
}
@override
String getType() {
return AppenderType.EMAIL.name;
}
}