Skip to content

Commit 1ab9f34

Browse files
committed
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]`.
1 parent 8d3949f commit 1ab9f34

File tree

18 files changed

+299
-299
lines changed

18 files changed

+299
-299
lines changed

Cargo.lock

Lines changed: 7 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/lib/logging.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:logging/logging.dart';
3+
import 'package:prototype/core/api/logging.dart';
4+
5+
/// Initializes Dart and Rust logging
6+
///
7+
/// Also configures the format of the logs.
8+
void initLogging() {
9+
// Init Dart logging
10+
Logger.root.level = kDebugMode ? Level.FINE : Level.INFO;
11+
Logger.root.onRecord.listen((record) {
12+
print(
13+
'[F] ${record.time} ${record.level.name} ${record.loggerName}: ${record.message}');
14+
});
15+
16+
// Rust Logging
17+
createLogStream().listen((event) {
18+
print(
19+
'[R] ${event.time.toLocal()} ${event.level.asString} ${event.target}: ${event.msg}');
20+
});
21+
}
22+
23+
extension on LogEntryLevel {
24+
String get asString => switch (this) {
25+
LogEntryLevel.trace => 'TRACE',
26+
LogEntryLevel.debug => 'DEBUG',
27+
LogEntryLevel.info => ' INFO',
28+
LogEntryLevel.warn => ' WARN',
29+
LogEntryLevel.error => 'ERROR'
30+
};
31+
}

app/lib/main.dart

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,18 @@
22
//
33
// SPDX-License-Identifier: AGPL-3.0-or-later
44

5-
import 'package:flutter/foundation.dart';
65
import 'package:flutter/material.dart';
7-
import 'package:logging/logging.dart';
86
import 'package:prototype/app.dart';
9-
import 'package:prototype/core/api/mobile_logging.dart';
107
import 'package:prototype/core/frb_generated.dart';
8+
import 'package:prototype/logging.dart';
119

1210
void main() async {
13-
_initRust();
14-
_initLogging();
11+
await RustLib.init();
12+
initLogging();
1513

1614
runApp(const App());
1715
}
1816

19-
void _initLogging() {
20-
Logger.root.level = kDebugMode ? Level.FINE : Level.INFO;
21-
Logger.root.onRecord.listen((record) {
22-
print('${record.level.name}: ${record.time}: ${record.message}');
23-
});
24-
}
25-
26-
Future<void> _initRust() async {
27-
// FRB
28-
await RustLib.init();
29-
// Logging
30-
createLogStream().listen((event) {
31-
print('Rust: ${event.level} ${event.tag} ${event.msg} ${event.timeMillis}');
32-
});
33-
}
34-
3517
void showErrorBanner(
3618
ScaffoldMessengerState messengerState,
3719
String errorDescription,

applogic/Cargo.toml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ description = "Multi-platform client application logic"
1313
[lib]
1414
crate-type = ["cdylib", "staticlib", "lib"]
1515

16-
[target.'cfg(target_os = "android")'.dependencies]
17-
android_logger = { version = "0.14" }
18-
19-
[target.'cfg(target_os = "ios")'.dependencies]
20-
oslog = { version = "0.2" }
21-
2216
[dependencies]
23-
log = { version = "0.4", features = ["kv"] }
24-
simplelog = { version = "0.12" }
17+
tracing = "0.1"
18+
tracing-subscriber = { version = "0.3", features = [
19+
"env-filter",
20+
"parking_lot",
21+
] }
22+
parking_lot = "0.12"
2523
uuid = { version = "1", features = ["v4"] }
2624
phnxcoreclient = { path = "../coreclient" }
2725
phnxapiclient = { path = "../apiclient" }
@@ -32,7 +30,6 @@ serde_json = "1"
3230
tokio = { version = "1.39", features = ["rt", "macros"] }
3331
flutter_rust_bridge = { version = "=2.7.0", features = ["chrono", "uuid"] }
3432
notify-rust = "4"
35-
lazy_static = "1.5"
3633
chrono = { workspace = true }
3734
jni = "0.21"
3835

applogic/src/api/logging.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-FileCopyrightText: 2024 Phoenix R&D GmbH <[email protected]>
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-or-later
4+
5+
use chrono::{DateTime, Utc};
6+
7+
use crate::StreamSink;
8+
9+
pub struct LogEntry {
10+
pub time: DateTime<Utc>,
11+
pub level: LogEntryLevel,
12+
pub tag: String,
13+
pub target: String,
14+
pub msg: String,
15+
}
16+
17+
pub enum LogEntryLevel {
18+
Trace,
19+
Debug,
20+
Info,
21+
Warn,
22+
Error,
23+
}
24+
25+
impl From<tracing::Level> for LogEntryLevel {
26+
fn from(level: tracing::Level) -> Self {
27+
match level {
28+
tracing::Level::TRACE => LogEntryLevel::Trace,
29+
tracing::Level::DEBUG => LogEntryLevel::Debug,
30+
tracing::Level::INFO => LogEntryLevel::Info,
31+
tracing::Level::WARN => LogEntryLevel::Warn,
32+
tracing::Level::ERROR => LogEntryLevel::Error,
33+
}
34+
}
35+
}
36+
37+
pub fn create_log_stream(s: StreamSink<LogEntry>) {
38+
crate::logging::dart::set_stream_sink(s)
39+
}

0 commit comments

Comments
 (0)