Flutter日志管理插件emas_tlog的使用
Flutter日志管理插件emas_tlog的使用
介绍
远程日志服务提供远程手机日志拉取功能,解决移动App线上异常排查困难的问题。远程日志服务支持Android/iOS应用类型。
产品架构
- 移动App集成SDK。
- 远程日志服务通过心跳探测识别已安装App的移动终端,并进行日志拉取配置。
- 远程日志服务拉取指定移动终端App的用户日志,并对拉取任务进行管理。
- 远程日志服务查看已从终端设备拉取至控制台的用户日志。
本项目是根据官方来制作的flutter版本。
快速开始
flutter配置:
dependencies:
emas_tlog: ^0.0.2
初始化:
// 方法的声明:
static void init(String appKey, String appSecret, String rsaPublicKey,
String appKeyIos, String appSecretIos, String rsaPublicKeyIos,
{String androidChannel = "line", String userNick = "NoLogin", ApmLogType? type, bool debug = true}) {
// xxxxx
}
// 方法的调用:
EmasTlog.init("**", "**", "**",
"**", "**", "**",
androidChannel: "HEHE", userNick: "lalala2");
参数说明:
appKey
:应用的公钥appSecret
:应用的密钥rsaPublicKey
:RSA公钥appKeyIos
:iOS的emas.appKeyappSecretIos
:iOS的emas.appSecretrsaPublicKeyIos
:iOS的appmonitor.tlog.rsaSecretandroidChannel
: 渠道 (iOS指定App Store)userNick
: 用户昵称说明: 默认值NoLogintype
:日志上传类型(注:iOS若不传,默认是I)debug
:底层运行日志打印(true测试环境开启,false正式环境关闭)(iOS可不传)
日常使用:
// 方法的声明:
static void init(String appKey, String appSecret, String rsaPublicKey,
String appKeyIos, String appSecretIos, String rsaPublicKeyIos,
{String androidChannel = "line", String userNick = "NoLogin", ApmLogType? type, bool debug = true}) {
// xxxxx
}
// 方法的调用:
EmasTlog.log(ApmLogType.I, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.V, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.W, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.E, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.D, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.I, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
// 说明:
// module为模块业务,可以为空
// 主动上传日志
EmasTlog.comment();
// 修改用户名(用于登录切换用户)
EmasTlog.updateNickName(name);
Android配置:
- 在根项目Android目录build.gradle配置如下代码:
ext {
tlog = [
openUtdid : true
]
}
如果项目编译期报类似如下错误:
Duplicate class com.ta.utdid2.a.a.a found in modules jetified-alicloud-android-utdid-2.5.1-proguard (com.aliyun.ams:alicloud-android-utdid:2.5.1-proguard) and jetified-utdid-1.5.2.1 (com.umeng.umsdk:utdid:1.5.2.1)
则代码需要调整为:
ext {
tlog = [
openUtdid : false
]
}
配置展示:
ext {
tlog = [
openUtdid : true
]
}
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
根app AndroidManifest配置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
** >
<application
**
tools:replace="android:label">
</manifest>
iOS配置说明:
- 在Flutter项目的iOS端的Podfile中添加如下索引库地址:
# alicloud
source "https://github.com/CocoaPods/Specs.git"
source "https://github.com/aliyun/aliyun-specs.git"
- 在Flutter项目的iOS端的info.plist文件中添加如下代码:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSBonjourServices</key>
<array>
<string>_dartobservatory._tcp</string>
</array>
<key>NSLocalNetworkUsageDescription</key>
<string>Main</string>
- 在iOS端项目Build Setting中,将Allow Non-modular Includes In Framework Modules设置为YES
iOS报错情况解决:
问题1:
Flutter: target has transitive dependencies that include statically linked binariesxxxx
解决问题1:
target 'Runner' do
# use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
注释掉use_frameworks!
如果存在其他含Swift文件的插件的情况,这会造成新问题(Swift与OC汇编找不到文件)
故,以上解决办法不可取,需要保留use_frameworks!
加入下面的代码即可:
pre_install do |installer|
# workaround for https://github.com/CocoaPods/CocoaPods/issues/3289 静态库
Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
end
完整示例Demo
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:emas_tlog/emas_tlog.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
EmasTlog.init("", "", "",
"", "", "",
androidChannel: "HEHE", userNick: "lalala2");
}
[@override](/user/override)
Widget build(BuildContext context) {
// 发送不同类型的日志
EmasTlog.log(ApmLogType.I, "tag2_1231231I", module: "hehe2_flutterI", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.V, "tag2_1231231", module: "hehe2_flutter", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.W, "tag2_1231231W", module: "hehe2_flutterW", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.E, "tag2_1231231E", module: "hehe2_flutterE", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.D, "tag2_1231231D", module: "hehe2_flutterD", tag: "tag_hehe2");
EmasTlog.log(ApmLogType.I, "tag2_1231231I", module: "hehe2_flutterI", tag: "tag_hehe2");
// 主动上传日志
// EmasTlog.comment();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}
更多关于Flutter日志管理插件emas_tlog的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter日志管理插件emas_tlog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
emas_tlog
是阿里云 EMAS(Enterprise Mobile Application Service)提供的一个 Flutter 插件,用于移动应用的日志管理。它可以帮助开发者收集、上传和管理应用中的日志信息,便于后续的监控和分析。
以下是 emas_tlog
插件的基本使用方法:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 emas_tlog
插件的依赖:
dependencies:
flutter:
sdk: flutter
emas_tlog: ^x.x.x # 请使用最新版本
然后,运行 flutter pub get
来安装依赖。
2. 初始化插件
在你的 Flutter 应用中初始化 emas_tlog
插件。通常,你可以在 main.dart
文件中进行初始化。
import 'package:emas_tlog/emas_tlog.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 emas_tlog
await EmasTlog.init(
appKey: 'your_app_key', // 替换为你的 App Key
appSecret: 'your_app_secret', // 替换为你的 App Secret
channel: 'your_channel', // 替换为你的渠道信息
version: '1.0.0', // 替换为你的应用版本
);
runApp(MyApp());
}
3. 记录日志
你可以使用 EmasTlog
提供的 API 来记录不同级别的日志。
import 'package:emas_tlog/emas_tlog.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 记录日志
EmasTlog.debug('This is a debug message');
EmasTlog.info('This is an info message');
EmasTlog.warn('This is a warning message');
EmasTlog.error('This is an error message');
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
4. 上传日志
你可以手动触发日志上传:
EmasTlog.upload();
或者,你可以配置自动上传策略,例如在应用启动时、退出时或定时上传。
5. 配置日志级别
你可以设置日志的级别,控制哪些级别的日志会被记录和上传。
EmasTlog.setLogLevel(LogLevel.INFO); // 只记录 INFO 及以上级别的日志