-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: LoggingManager implementation * fix: typo * refactor: rename LoggingManager to LoggingService * refactor: make LoggingService global and initialized as first in bootstrap * fix: typo * chore: typos
- Loading branch information
1 parent
94a1193
commit 0e7f02c
Showing
7 changed files
with
163 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
catalyst_voices/packages/catalyst_voices_shared/lib/src/logging/logging_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import 'dart:async'; | ||
import 'dart:developer' as developer; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
export 'package:logging/logging.dart' show Level, Logger; | ||
|
||
abstract interface class LoggingService { | ||
factory LoggingService() { | ||
return _LoggingServiceImpl(root: Logger.root); | ||
} | ||
|
||
set level(Level newValue); | ||
|
||
set printLogs(bool newValue); | ||
|
||
void dispose(); | ||
} | ||
|
||
final class _LoggingServiceImpl implements LoggingService { | ||
final Logger root; | ||
|
||
StreamSubscription<LogRecord>? _recordsSub; | ||
|
||
_LoggingServiceImpl({ | ||
required this.root, | ||
}) : _printLogs = false { | ||
hierarchicalLoggingEnabled = true; | ||
|
||
root.level = Level.OFF; | ||
_recordsSub = root.onRecord.listen(_onLogRecord); | ||
} | ||
|
||
@override | ||
set level(Level newValue) { | ||
if (root.level == newValue) { | ||
return; | ||
} | ||
root.level = newValue; | ||
} | ||
|
||
bool get printLogs => _printLogs; | ||
|
||
bool _printLogs; | ||
|
||
@override | ||
set printLogs(bool newValue) { | ||
if (_printLogs == newValue) { | ||
return; | ||
} | ||
_printLogs = newValue; | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_recordsSub?.cancel(); | ||
_recordsSub = null; | ||
} | ||
|
||
void _onLogRecord(LogRecord log) { | ||
if (_printLogs) { | ||
_printLogRecord(log); | ||
} | ||
} | ||
|
||
void _printLogRecord(LogRecord log) { | ||
if (log.level >= Level.SEVERE) { | ||
final error = log.error; | ||
final errorDetails = FlutterErrorDetails( | ||
exception: error is Exception ? error : Exception(error), | ||
stack: log.stackTrace, | ||
library: log.loggerName, | ||
context: ErrorDescription(log.message), | ||
); | ||
FlutterError.dumpErrorToConsole(errorDetails); | ||
return; | ||
} | ||
|
||
developer.log( | ||
log.message, | ||
time: log.time, | ||
sequenceNumber: log.sequenceNumber, | ||
level: log.level.value, | ||
name: log.loggerName, | ||
zone: log.zone, | ||
error: log.error, | ||
stackTrace: log.stackTrace, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters