1
1
import 'package:logger/src/filters/development_filter.dart' ;
2
- import 'package:logger/src/printers/pretty_printer.dart' ;
3
- import 'package:logger/src/outputs/console_output.dart' ;
4
2
import 'package:logger/src/log_filter.dart' ;
5
- import 'package:logger/src/log_printer.dart' ;
6
3
import 'package:logger/src/log_output.dart' ;
4
+ import 'package:logger/src/log_printer.dart' ;
5
+ import 'package:logger/src/outputs/console_output.dart' ;
6
+ import 'package:logger/src/printers/pretty_printer.dart' ;
7
7
8
8
/// [Level] s to control logging output. Logging can be enabled to include all
9
9
/// levels above certain [Level] .
@@ -33,10 +33,8 @@ class OutputEvent {
33
33
OutputEvent (this .level, this .lines);
34
34
}
35
35
36
- @Deprecated ('Use a custom LogFilter instead' )
37
36
typedef LogCallback = void Function (LogEvent event);
38
37
39
- @Deprecated ('Use a custom LogOutput instead' )
40
38
typedef OutputCallback = void Function (OutputEvent event);
41
39
42
40
/// Use instances of logger to send log messages to the [LogPrinter] .
@@ -46,6 +44,10 @@ class Logger {
46
44
/// All logs with levels below this level will be omitted.
47
45
static Level level = Level .verbose;
48
46
47
+ static final Set <LogCallback > _logCallbacks = {};
48
+
49
+ static final Set <OutputCallback > _outputCallbacks = {};
50
+
49
51
final LogFilter _filter;
50
52
final LogPrinter _printer;
51
53
final LogOutput _output;
@@ -112,13 +114,19 @@ class Logger {
112
114
}
113
115
var logEvent = LogEvent (level, message, error, stackTrace);
114
116
if (_filter.shouldLog (logEvent)) {
117
+ for (var callback in _logCallbacks) {
118
+ callback (logEvent);
119
+ }
115
120
var output = _printer.log (logEvent);
116
121
117
122
if (output.isNotEmpty) {
118
123
var outputEvent = OutputEvent (level, output);
119
124
// Issues with log output should NOT influence
120
125
// the main software behavior.
121
126
try {
127
+ for (var callback in _outputCallbacks) {
128
+ callback (outputEvent);
129
+ }
122
130
_output.output (outputEvent);
123
131
} catch (e, s) {
124
132
print (e);
@@ -135,4 +143,28 @@ class Logger {
135
143
_printer.destroy ();
136
144
_output.destroy ();
137
145
}
146
+
147
+ /// Register a [LogCallback] which is called for each new [LogEvent] .
148
+ static void addLogListener (LogCallback callback) {
149
+ _logCallbacks.add (callback);
150
+ }
151
+
152
+ /// Removes a [LogCallback] which was previously registered.
153
+ ///
154
+ /// Returns whether the callback was successfully removed.
155
+ static bool removeLogListener (LogCallback callback) {
156
+ return _logCallbacks.remove (callback);
157
+ }
158
+
159
+ /// Register an [OutputCallback] which is called for each new [OutputEvent] .
160
+ static void addOutputListener (OutputCallback callback) {
161
+ _outputCallbacks.add (callback);
162
+ }
163
+
164
+ /// Removes a [OutputCallback] which was previously registered.
165
+ ///
166
+ /// Returns whether the callback was successfully removed.
167
+ static void removeOutputListener (OutputCallback callback) {
168
+ _outputCallbacks.remove (callback);
169
+ }
138
170
}
0 commit comments