-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: structured logging in applogic
Use tracing for logging in applogic. Replace the simplelog implementation with tracing-subscriber. Implement a tracing layer for sending logs to the Dart side. The layer is used at android and ios. On desktop, the logs are printed to the console via the tracing-subscriber formatter. Also configure the logging formatting in Dart to use the same format as the Rust tracing logging. Logs from Dart are prefixed with `[F]` and logs from Rust are prefixed with `[R]`.
- Loading branch information
Showing
18 changed files
with
299 additions
and
299 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:prototype/core/api/logging.dart'; | ||
|
||
/// Initializes Dart and Rust logging | ||
/// | ||
/// Also configures the format of the logs. | ||
void initLogging() { | ||
// Init Dart logging | ||
Logger.root.level = kDebugMode ? Level.FINE : Level.INFO; | ||
Logger.root.onRecord.listen((record) { | ||
print( | ||
'[F] ${record.time} ${record.level.name} ${record.loggerName}: ${record.message}'); | ||
}); | ||
|
||
// Rust Logging | ||
createLogStream().listen((event) { | ||
print( | ||
'[R] ${event.time.toLocal()} ${event.level.asString} ${event.target}: ${event.msg}'); | ||
}); | ||
} | ||
|
||
extension on LogEntryLevel { | ||
String get asString => switch (this) { | ||
LogEntryLevel.trace => 'TRACE', | ||
LogEntryLevel.debug => 'DEBUG', | ||
LogEntryLevel.info => ' INFO', | ||
LogEntryLevel.warn => ' WARN', | ||
LogEntryLevel.error => 'ERROR' | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2024 Phoenix R&D GmbH <[email protected]> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
use chrono::{DateTime, Utc}; | ||
|
||
use crate::StreamSink; | ||
|
||
pub struct LogEntry { | ||
pub time: DateTime<Utc>, | ||
pub level: LogEntryLevel, | ||
pub tag: String, | ||
pub target: String, | ||
pub msg: String, | ||
} | ||
|
||
pub enum LogEntryLevel { | ||
Trace, | ||
Debug, | ||
Info, | ||
Warn, | ||
Error, | ||
} | ||
|
||
impl From<tracing::Level> for LogEntryLevel { | ||
fn from(level: tracing::Level) -> Self { | ||
match level { | ||
tracing::Level::TRACE => LogEntryLevel::Trace, | ||
tracing::Level::DEBUG => LogEntryLevel::Debug, | ||
tracing::Level::INFO => LogEntryLevel::Info, | ||
tracing::Level::WARN => LogEntryLevel::Warn, | ||
tracing::Level::ERROR => LogEntryLevel::Error, | ||
} | ||
} | ||
} | ||
|
||
pub fn create_log_stream(s: StreamSink<LogEntry>) { | ||
crate::logging::dart::set_stream_sink(s) | ||
} |
Oops, something went wrong.