112 lines
2.1 KiB
Dart
112 lines
2.1 KiB
Dart
import 'dart:developer' as developer;
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:nhancepolicy/config/environment.dart';
|
|
|
|
String _formatMessage(
|
|
String message, {
|
|
String? className,
|
|
String? methodName,
|
|
}) {
|
|
final timestamp = DateTime.now().toIso8601String();
|
|
final contextParts = <String>[];
|
|
if (className != null && className.isNotEmpty) {
|
|
contextParts.add(className);
|
|
}
|
|
if (methodName != null && methodName.isNotEmpty) {
|
|
contextParts.add(methodName);
|
|
}
|
|
final context = contextParts.isEmpty ? '' : '[${contextParts.join('.')}] ';
|
|
return '[$timestamp] $context$message';
|
|
}
|
|
|
|
void _log(
|
|
String level,
|
|
String message, {
|
|
String tag = 'APP',
|
|
Object? error,
|
|
StackTrace? stackTrace,
|
|
String? className,
|
|
String? methodName,
|
|
}) {
|
|
final shouldLog = kDebugMode || Environment.flavor == Flavor.dev;
|
|
if (!shouldLog) {
|
|
return;
|
|
}
|
|
final formatted =
|
|
_formatMessage(message, className: className, methodName: methodName);
|
|
final channel = '$tag:$level';
|
|
|
|
developer.log(
|
|
formatted,
|
|
name: channel,
|
|
error: error,
|
|
stackTrace: stackTrace,
|
|
);
|
|
debugPrint('[$channel] $formatted');
|
|
}
|
|
|
|
void logDebug(
|
|
Object? message, {
|
|
String tag = 'APP',
|
|
String? className,
|
|
String? methodName,
|
|
}) {
|
|
_log(
|
|
'DEBUG',
|
|
'${message ?? 'null'}',
|
|
tag: tag,
|
|
className: className,
|
|
methodName: methodName,
|
|
);
|
|
}
|
|
|
|
void logInfo(
|
|
Object? message, {
|
|
String tag = 'APP',
|
|
String? className,
|
|
String? methodName,
|
|
}) {
|
|
_log(
|
|
'INFO',
|
|
'${message ?? 'null'}',
|
|
tag: tag,
|
|
className: className,
|
|
methodName: methodName,
|
|
);
|
|
}
|
|
|
|
void logWarning(
|
|
Object? message, {
|
|
String tag = 'APP',
|
|
String? className,
|
|
String? methodName,
|
|
}) {
|
|
_log(
|
|
'WARN',
|
|
'${message ?? 'null'}',
|
|
tag: tag,
|
|
className: className,
|
|
methodName: methodName,
|
|
);
|
|
}
|
|
|
|
void logError(
|
|
Object? message, {
|
|
String tag = 'APP',
|
|
Object? error,
|
|
StackTrace? stackTrace,
|
|
String? className,
|
|
String? methodName,
|
|
}) {
|
|
_log(
|
|
'ERROR',
|
|
'${message ?? 'null'}',
|
|
tag: tag,
|
|
error: error,
|
|
stackTrace: stackTrace,
|
|
className: className,
|
|
methodName: methodName,
|
|
);
|
|
}
|