mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 10:29:06 +02:00
Fix module/method/fileAndLineNumber calculations in logging service
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,26 +0,0 @@
|
|||||||
mixin class FileAndLineNumber {
|
|
||||||
static String get _parts => StackTrace.current
|
|
||||||
.toString()
|
|
||||||
.split("\n")[2]
|
|
||||||
.split(RegExp("#2"))[1]
|
|
||||||
.trim();
|
|
||||||
|
|
||||||
static String? get module =>
|
|
||||||
_parts.split(".").firstOrNull?.replaceFirst("new ", "");
|
|
||||||
static String? get method {
|
|
||||||
if (_parts.length <= 1) return null;
|
|
||||||
return _parts[1].split(" ").firstOrNull?.replaceAll("<anonymous", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
static String? get fileAndLine {
|
|
||||||
final List<String> fileAndLineParts = [
|
|
||||||
...?_parts.split("(package:").lastOrNull?.split(":"),
|
|
||||||
];
|
|
||||||
|
|
||||||
if (fileAndLineParts.length <= 2) {
|
|
||||||
return fileAndLineParts.firstOrNull;
|
|
||||||
}
|
|
||||||
|
|
||||||
return "${fileAndLineParts[0]}:${fileAndLineParts[1]}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import "dart:async";
|
import "dart:async";
|
||||||
|
import "dart:developer" as dev;
|
||||||
|
|
||||||
import "package:arcane_framework/src/services/logging/logging_interface_extensions.dart";
|
|
||||||
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
import "package:arcane_helper_utils/arcane_helper_utils.dart";
|
||||||
|
|
||||||
part "logging_enums.dart";
|
part "logging_enums.dart";
|
||||||
@@ -12,7 +12,7 @@ part "logging_interface.dart";
|
|||||||
/// The `ArcaneLogger` provides a centralized way to log messages across
|
/// The `ArcaneLogger` provides a centralized way to log messages across
|
||||||
/// different parts of an application. It supports multiple logging interfaces,
|
/// different parts of an application. It supports multiple logging interfaces,
|
||||||
/// metadata, and platform-specific error handling.
|
/// metadata, and platform-specific error handling.
|
||||||
class ArcaneLogger with FileAndLineNumber {
|
class ArcaneLogger {
|
||||||
ArcaneLogger._internal();
|
ArcaneLogger._internal();
|
||||||
|
|
||||||
static final ArcaneLogger _instance = ArcaneLogger._internal();
|
static final ArcaneLogger _instance = ArcaneLogger._internal();
|
||||||
@@ -176,45 +176,67 @@ class ArcaneLogger with FileAndLineNumber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
metadata ??= <String, String>{};
|
metadata ??= <String, String>{};
|
||||||
|
|
||||||
metadata.putIfAbsent("timestamp", () => DateTime.now().toIso8601String());
|
metadata.putIfAbsent("timestamp", () => DateTime.now().toIso8601String());
|
||||||
|
|
||||||
|
String? parts;
|
||||||
|
String? filenameAndLineNumber;
|
||||||
|
if (!skipAutodetection) {
|
||||||
|
try {
|
||||||
|
parts = StackTrace.current
|
||||||
|
.toString()
|
||||||
|
.split("\n")[2]
|
||||||
|
.split(RegExp("#2"))[1]
|
||||||
|
.trim();
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
module ??= parts?.split(".").firstOrNull?.replaceFirst("new ", "");
|
||||||
|
|
||||||
|
method ??= ((parts?.split(".").length ?? 0) <= 1)
|
||||||
|
? null
|
||||||
|
: parts
|
||||||
|
?.split(".")[1]
|
||||||
|
.split(" ")
|
||||||
|
.firstOrNull
|
||||||
|
?.replaceAll("<anonymous", "");
|
||||||
|
|
||||||
|
final List<String> fileAndLineParts = [
|
||||||
|
...?parts?.split("(package:").lastOrNull?.split(":"),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (fileAndLineParts.length <= 2) {
|
||||||
|
filenameAndLineNumber = fileAndLineParts.firstOrNull;
|
||||||
|
} else {
|
||||||
|
filenameAndLineNumber = "${fileAndLineParts[0]}:${fileAndLineParts[1]}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Module management
|
||||||
if (module.isNotEmptyOrNull) {
|
if (module.isNotEmptyOrNull) {
|
||||||
metadata.putIfAbsent("module", () => module!);
|
metadata.putIfAbsent("module", () => module!);
|
||||||
} else if (!metadata.containsKey("module") &&
|
|
||||||
metadata["module"].isNullOrEmpty) {
|
|
||||||
try {
|
|
||||||
if (FileAndLineNumber.module.isNotNullOrEmpty) {
|
|
||||||
metadata.putIfAbsent("module", () => FileAndLineNumber.module!);
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method managmeent
|
||||||
if (method.isNotEmptyOrNull) {
|
if (method.isNotEmptyOrNull) {
|
||||||
metadata.putIfAbsent("method", () => method!);
|
metadata.putIfAbsent("method", () => method!);
|
||||||
} else if (!metadata.containsKey("method") &&
|
|
||||||
metadata["method"].isNullOrEmpty) {
|
|
||||||
try {
|
|
||||||
if (FileAndLineNumber.method.isNotNullOrEmpty) {
|
|
||||||
metadata.putIfAbsent("method", () => FileAndLineNumber.method!);
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!metadata.containsKey("filenameAndLineNumber") &&
|
// Filename and line number management
|
||||||
metadata["filenameAndLineNumber"].isNullOrEmpty) {
|
if (filenameAndLineNumber.isNotNullOrEmpty) {
|
||||||
try {
|
|
||||||
if (FileAndLineNumber.fileAndLine.isNotNullOrEmpty) {
|
|
||||||
metadata.putIfAbsent(
|
metadata.putIfAbsent(
|
||||||
"filenameAndLineNumber",
|
"filenameAndLineNumber",
|
||||||
() => FileAndLineNumber.fileAndLine!,
|
() => filenameAndLineNumber!,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (_) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata.addAll(additionalMetadata);
|
metadata.addAll(additionalMetadata);
|
||||||
|
|
||||||
|
module ??= metadata.containsKey("module") ? metadata["module"] : null;
|
||||||
|
method ??= metadata.containsKey("method") ? metadata["method"] : null;
|
||||||
|
|
||||||
|
dev.log(
|
||||||
|
"Module: $module, Method: $method",
|
||||||
|
);
|
||||||
|
|
||||||
// Send logs to registered interface(s)
|
// Send logs to registered interface(s)
|
||||||
for (final LoggingInterface i in I._interfaces) {
|
for (final LoggingInterface i in I._interfaces) {
|
||||||
i.log(
|
i.log(
|
||||||
|
|||||||
Reference in New Issue
Block a user