import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:logger/logger.dart';
import 'package:flutter/services/path_provider.dart';
import 'package:flutter/material.dart';
// 创建Logger对象
final logger = Logger(
printer: PrettyPrinter(
methodCount: 2, // 显示函数调用栈的数量
),
);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
// 初始化日志文件路径
String _logFilePath = '';
@override
void initState() {
super.initState();
_initLogFilePath();
}
// 初始化日志文件路径的异步方法
Future<void> _initLogFilePath() async {
String path = '';
if (kIsWeb) {
path = 'log.txt';
} else {
path = (await getApplicationDocumentsDirectory()).path + '/log.txt';
}
setState(() {
_logFilePath = path;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Logger Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Log Message'),
onPressed: () {
logger.d('This is a debug message.', tag: 'MyTag');
logger.i('This is an info message.');
logger.w('This is a warning message.');
logger.e('This is an error message.');
logger.v('This is a verbose message.');
logger.wtf('This is a what-the-fuck message.');
},
),
RaisedButton(
child: Text('Log Message to File'),
onPressed: () {
if (_logFilePath != '') {
final logPrinter = FilePrinter(File(_logFilePath));
final logger = Logger(printer: logPrinter);
logger.d('This is a debug message.', tag: 'MyTag');
logger.i('This is an info message.');
logger.w('This is a warning message.');
logger.e('This is an error message.');
logger.v('This is a
评论已关闭