refactor: 优化日志输出,仅在调试模式下启用
- 为所有 print 语句添加 kDebugMode 检查 - 更新 KRLogUtil 工具类,Release 模式下禁用日志输出 - 优化 18 个文件中的 335+ 条日志语句 - 提升 Release 版本性能并增强安全性 (cherry picked from commit 301f1510ba81fe94fb08e013ca80b3ca708a2e15)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:kaer_with_panels/app/common/app_run_data.dart';
|
||||
import 'package:kaer_with_panels/app/common/app_config.dart';
|
||||
@@ -30,27 +31,33 @@ class BaseResponse<T> {
|
||||
|
||||
if (shouldDecrypt && cipherText.isNotEmpty && nonce.isNotEmpty) {
|
||||
try {
|
||||
print('═══════════════════════════════════════');
|
||||
print('🔐 检测到加密响应,开始解密...');
|
||||
print('📥 加密数据长度: ${cipherText.length} 字符');
|
||||
print('⏰ 时间戳: $nonce');
|
||||
if (kDebugMode) {
|
||||
print('═══════════════════════════════════════');
|
||||
print('🔐 检测到加密响应,开始解密...');
|
||||
print('📥 加密数据长度: ${cipherText.length} 字符');
|
||||
print('⏰ 时间戳: $nonce');
|
||||
}
|
||||
|
||||
final decrypted = KRAesUtil.decryptData(cipherText, nonce, AppConfig.kr_encryptionKey);
|
||||
body = jsonDecode(decrypted);
|
||||
|
||||
print('✅ 解密成功');
|
||||
print('');
|
||||
print('📦 解密后的完整数据:');
|
||||
// 格式化打印 JSON,方便调试
|
||||
final bodyStr = JsonEncoder.withIndent(' ').convert(body);
|
||||
print(bodyStr);
|
||||
print('═══════════════════════════════════════');
|
||||
if (kDebugMode) {
|
||||
print('✅ 解密成功');
|
||||
print('');
|
||||
print('📦 解密后的完整数据:');
|
||||
// 格式化打印 JSON,方便调试
|
||||
final bodyStr = JsonEncoder.withIndent(' ').convert(body);
|
||||
print(bodyStr);
|
||||
print('═══════════════════════════════════════');
|
||||
}
|
||||
|
||||
KRLogUtil.kr_i('🔓 解密成功', tag: 'BaseResponse');
|
||||
} catch (e) {
|
||||
print('❌ 解密失败: $e');
|
||||
print('⚠️ 将使用原始数据');
|
||||
print('═══════════════════════════════════════');
|
||||
if (kDebugMode) {
|
||||
print('❌ 解密失败: $e');
|
||||
print('⚠️ 将使用原始数据');
|
||||
print('═══════════════════════════════════════');
|
||||
}
|
||||
KRLogUtil.kr_e('❌ 解密失败: $e', tag: 'BaseResponse');
|
||||
body = dataMap;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import 'package:loggy/loggy.dart';
|
||||
|
||||
import '../utils/kr_aes_util.dart';
|
||||
import '../utils/kr_log_util.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
// import 'package:video/app/utils/common_util.dart';
|
||||
// import 'package:video/app/utils/log_util.dart';
|
||||
@@ -316,31 +317,47 @@ class HttpUtil {
|
||||
class MyInterceptor extends Interceptor {
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
print('>>> Request │ ${options.method} │ ${options.uri}');
|
||||
if (kDebugMode) {
|
||||
print('>>> Request │ ${options.method} │ ${options.uri}');
|
||||
}
|
||||
if (options.data != null) {
|
||||
print('Body: ${options.data}');
|
||||
if (kDebugMode) {
|
||||
print('Body: ${options.data}');
|
||||
}
|
||||
}
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||
print('<<< Response │ ${response.requestOptions.method} │ ${response.statusCode} ${response.statusMessage} │ ${response.requestOptions.uri}');
|
||||
if (kDebugMode) {
|
||||
print('<<< Response │ ${response.requestOptions.method} │ ${response.statusCode} ${response.statusMessage} │ ${response.requestOptions.uri}');
|
||||
}
|
||||
if (response.data != null) {
|
||||
print('Body: ${response.data}');
|
||||
if (kDebugMode) {
|
||||
print('Body: ${response.data}');
|
||||
}
|
||||
}
|
||||
handler.next(response);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
print('<<< Error │ ${err.requestOptions.method} │ ${err.requestOptions.uri}');
|
||||
print('Error Type: ${err.type}');
|
||||
if (kDebugMode) {
|
||||
print('<<< Error │ ${err.requestOptions.method} │ ${err.requestOptions.uri}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('Error Type: ${err.type}');
|
||||
}
|
||||
if (err.message != null) {
|
||||
print('Error Message: ${err.message}');
|
||||
if (kDebugMode) {
|
||||
print('Error Message: ${err.message}');
|
||||
}
|
||||
}
|
||||
if (err.response?.data != null) {
|
||||
print('Response Data: ${err.response?.data}');
|
||||
if (kDebugMode) {
|
||||
print('Response Data: ${err.response?.data}');
|
||||
}
|
||||
}
|
||||
handler.next(err);
|
||||
}
|
||||
@@ -350,17 +367,25 @@ class MyInterceptor extends Interceptor {
|
||||
class _KRSimpleHttpInterceptor extends Interceptor {
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
print('>>> Request │ ${options.method} │ ${options.uri}');
|
||||
if (kDebugMode) {
|
||||
print('>>> Request │ ${options.method} │ ${options.uri}');
|
||||
}
|
||||
if (options.data != null) {
|
||||
print('Body: ${options.data}');
|
||||
if (kDebugMode) {
|
||||
print('Body: ${options.data}');
|
||||
}
|
||||
|
||||
// 检查是否是加密数据(包含 data 和 time 字段)
|
||||
if (options.data is Map<String, dynamic>) {
|
||||
final data = options.data as Map<String, dynamic>;
|
||||
if (data.containsKey('data') && data.containsKey('time')) {
|
||||
try {
|
||||
print('');
|
||||
print('🔐 检测到加密请求,正在解密...');
|
||||
if (kDebugMode) {
|
||||
print('');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('🔐 检测到加密请求,正在解密...');
|
||||
}
|
||||
// 尝试解密并打印原始数据
|
||||
final encryptedData = data['data'] as String;
|
||||
final nonce = data['time'] as String;
|
||||
@@ -369,17 +394,25 @@ class _KRSimpleHttpInterceptor extends Interceptor {
|
||||
nonce,
|
||||
AppConfig.kr_encryptionKey,
|
||||
);
|
||||
print('🔓 解密后的原始请求数据:');
|
||||
if (kDebugMode) {
|
||||
print('🔓 解密后的原始请求数据:');
|
||||
}
|
||||
// 尝试格式化 JSON
|
||||
try {
|
||||
final jsonData = jsonDecode(decrypted);
|
||||
final prettyJson = JsonEncoder.withIndent(' ').convert(jsonData);
|
||||
print(prettyJson);
|
||||
if (kDebugMode) {
|
||||
print(prettyJson);
|
||||
}
|
||||
} catch (_) {
|
||||
print(decrypted);
|
||||
if (kDebugMode) {
|
||||
print(decrypted);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('⚠️ 请求解密失败: $e');
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 请求解密失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -389,9 +422,13 @@ class _KRSimpleHttpInterceptor extends Interceptor {
|
||||
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||
print('<<< Response │ ${response.requestOptions.method} │ ${response.statusCode} ${response.statusMessage} │ ${response.requestOptions.uri}');
|
||||
if (kDebugMode) {
|
||||
print('<<< Response │ ${response.requestOptions.method} │ ${response.statusCode} ${response.statusMessage} │ ${response.requestOptions.uri}');
|
||||
}
|
||||
if (response.data != null) {
|
||||
print('Body: ${response.data}');
|
||||
if (kDebugMode) {
|
||||
print('Body: ${response.data}');
|
||||
}
|
||||
|
||||
// 检查响应是否是加密数据(包含 data 和 time 字段)
|
||||
if (response.data is Map<String, dynamic>) {
|
||||
@@ -403,8 +440,12 @@ class _KRSimpleHttpInterceptor extends Interceptor {
|
||||
nestedData.containsKey('data') &&
|
||||
nestedData.containsKey('time')) {
|
||||
try {
|
||||
print('');
|
||||
print('🔐 检测到加密响应,正在解密...');
|
||||
if (kDebugMode) {
|
||||
print('');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('🔐 检测到加密响应,正在解密...');
|
||||
}
|
||||
// 尝试解密并打印原始响应数据
|
||||
final encryptedData = nestedData['data'] as String;
|
||||
final nonce = nestedData['time'] as String;
|
||||
@@ -413,17 +454,25 @@ class _KRSimpleHttpInterceptor extends Interceptor {
|
||||
nonce,
|
||||
AppConfig.kr_encryptionKey,
|
||||
);
|
||||
print('🔓 解密后的原始响应数据: ${response.requestOptions.uri}');
|
||||
if (kDebugMode) {
|
||||
print('🔓 解密后的原始响应数据:');
|
||||
}
|
||||
// 尝试格式化 JSON
|
||||
try {
|
||||
final jsonData = jsonDecode(decrypted);
|
||||
final prettyJson = JsonEncoder.withIndent(' ').convert(jsonData);
|
||||
print(prettyJson);
|
||||
if (kDebugMode) {
|
||||
print(prettyJson);
|
||||
}
|
||||
} catch (_) {
|
||||
print(decrypted);
|
||||
if (kDebugMode) {
|
||||
print(decrypted);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('⚠️ 响应解密失败: $e');
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 响应解密失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -433,13 +482,21 @@ class _KRSimpleHttpInterceptor extends Interceptor {
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
print('<<< Error │ ${err.requestOptions.method} │ ${err.requestOptions.uri}');
|
||||
print('Error Type: ${err.type}');
|
||||
if (kDebugMode) {
|
||||
print('<<< Error │ ${err.requestOptions.method} │ ${err.requestOptions.uri}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('Error Type: ${err.type}');
|
||||
}
|
||||
if (err.message != null) {
|
||||
print('Error Message: ${err.message}');
|
||||
if (kDebugMode) {
|
||||
print('Error Message: ${err.message}');
|
||||
}
|
||||
}
|
||||
if (err.response?.data != null) {
|
||||
print('Response Data: ${err.response?.data}');
|
||||
if (kDebugMode) {
|
||||
print('Response Data: ${err.response?.data}');
|
||||
}
|
||||
}
|
||||
handler.next(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user