Skip to content

Commit

Permalink
refactor: structured logging in applogic
Browse files Browse the repository at this point in the history
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
boxdot committed Dec 18, 2024
1 parent 8d3949f commit 1ab9f34
Show file tree
Hide file tree
Showing 18 changed files with 299 additions and 299 deletions.
81 changes: 7 additions & 74 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions app/lib/logging.dart
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'
};
}
24 changes: 3 additions & 21 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,18 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:prototype/app.dart';
import 'package:prototype/core/api/mobile_logging.dart';
import 'package:prototype/core/frb_generated.dart';
import 'package:prototype/logging.dart';

void main() async {
_initRust();
_initLogging();
await RustLib.init();
initLogging();

runApp(const App());
}

void _initLogging() {
Logger.root.level = kDebugMode ? Level.FINE : Level.INFO;
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});
}

Future<void> _initRust() async {
// FRB
await RustLib.init();
// Logging
createLogStream().listen((event) {
print('Rust: ${event.level} ${event.tag} ${event.msg} ${event.timeMillis}');
});
}

void showErrorBanner(
ScaffoldMessengerState messengerState,
String errorDescription,
Expand Down
15 changes: 6 additions & 9 deletions applogic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ description = "Multi-platform client application logic"
[lib]
crate-type = ["cdylib", "staticlib", "lib"]

[target.'cfg(target_os = "android")'.dependencies]
android_logger = { version = "0.14" }

[target.'cfg(target_os = "ios")'.dependencies]
oslog = { version = "0.2" }

[dependencies]
log = { version = "0.4", features = ["kv"] }
simplelog = { version = "0.12" }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = [
"env-filter",
"parking_lot",
] }
parking_lot = "0.12"
uuid = { version = "1", features = ["v4"] }
phnxcoreclient = { path = "../coreclient" }
phnxapiclient = { path = "../apiclient" }
Expand All @@ -32,7 +30,6 @@ serde_json = "1"
tokio = { version = "1.39", features = ["rt", "macros"] }
flutter_rust_bridge = { version = "=2.7.0", features = ["chrono", "uuid"] }
notify-rust = "4"
lazy_static = "1.5"
chrono = { workspace = true }
jni = "0.21"

Expand Down
39 changes: 39 additions & 0 deletions applogic/src/api/logging.rs
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)
}
Loading

0 comments on commit 1ab9f34

Please sign in to comment.