如何使用Logger软件包在Flutter Web应用中实现日志记录

问题描述 投票:0回答:1

这是我尝试使用记录器的代码:

import 'dart:convert'; import 'dart:io'; import 'package:add_2_calendar/add_2_calendar.dart'; import 'package:deal_diligence/Providers/event_provider.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:http/http.dart' as http; import 'package:logger/logger.dart'; final GoogleSignIn _googleSignIn = GoogleSignIn( clientId: dotenv.env["GOOGLE_CALENDAR_CLIENT_ID"], scopes: [ 'https://www.googleapis.com/auth/calendar', ], ); class AddEventsToAllCalendars { static GoogleSignInAuthentication? auth; static final ddLog = Logger( filter: null, printer: PrettyPrinter(), output: FileOutput(file: File("../lib/log.txt")), ); static void addEvent(Events eventCal) async { // Define your custom format using DateFormat GoogleSignInAuthentication? auth; if (!kIsWeb) { Add2Calendar.addEvent2Cal(buildEvent(eventCal)); } else { try { final signInAccountSilently = await _googleSignIn.signInSilently(); if (signInAccountSilently != null) { auth = await signInAccountSilently.authentication; } else { final signInAccount = await _googleSignIn.signIn(); auth = await signInAccount?.authentication; } if (auth == null) return; const String calendarId = "primary"; // Use 'primary' for the default calendar. const String url = 'https://www.googleapis.com/calendar/v3/calendars/$calendarId/events'; debugPrint("EventDate: ${eventCal.eventDate?.toIso8601String()}"); debugPrint("EventDuration: ${eventCal.eventDuration}"); final event = { "summary": eventCal.eventName, "description": eventCal.eventDescription, "location": eventCal.location, // Add location "start": { "dateTime": eventCal.eventDate?.toUtc().toIso8601String(), "timeZone": "UTC", }, "end": { "dateTime": eventCal.eventDate ?.add(Duration( minutes: int.parse(eventCal.eventDuration != "" ? eventCal.eventDuration ?? "30" : '30'))) .toUtc() .toIso8601String(), "timeZone": "UTC", }, "recurrence": [ "RRULE:FREQ=DAILY;INTERVAL=${eventCal.interval};COUNT=${eventCal.occurrences}" ], // Recurrence rule: daily with interval and occurrences }; final response = await http.post( Uri.parse(url), headers: { 'Authorization': 'Bearer ${auth.accessToken}', 'Content-Type': 'application/json', }, body: jsonEncode(event), ); if (response.statusCode == 200) { debugPrint('Event created successfully'); } else { debugPrint('Error creating event: ${response.statusCode}'); } } catch (e) { debugPrint("RethrowError: ${e.toString()}"); ddLog.e("ERROR: ${e.toString()}"); <<<< LOGGING HERE rethrow; } } } static Future<void> addMultipleEvent(Events eventCal) async { // Define your custom format using DateFormat if (!kIsWeb) { Add2Calendar.addEvent2Cal(buildEvent(eventCal)); } else { try { if (auth == null) { final signInAccountSilently = await _googleSignIn.signInSilently(); if (signInAccountSilently != null) { auth = await signInAccountSilently.authentication; } else { final signInAccount = await _googleSignIn.signIn(); auth = await signInAccount?.authentication; } } if (auth == null) return; // for (var eventCal in eventsList) { const String calendarId = "primary"; // Use 'primary' for the default calendar. const String url = 'https://www.googleapis.com/calendar/v3/calendars/$calendarId/events'; final event = { "summary": eventCal.eventName, "description": eventCal.eventDescription, "location": eventCal.location, // Add location "start": { "dateTime": eventCal.eventDate?.toUtc().toIso8601String(), "timeZone": "UTC", }, "end": { "dateTime": eventCal.eventDate ?.add(Duration( minutes: int.parse(eventCal.eventDuration != "" ? eventCal.eventDuration ?? "30" : '30'))) .toUtc() .toIso8601String(), "timeZone": "UTC", }, // Recurrence rule: daily with interval and occurrences }; final response = await http.post( Uri.parse(url), headers: { 'Authorization': 'Bearer ${auth?.accessToken}', 'Content-Type': 'application/json', }, body: jsonEncode(event), ); if (response.statusCode == 200) { debugPrint('Event created successfully'); } else { debugPrint('Error creating event: ${response.statusCode}'); } // } } catch (e) { debugPrint("RethrowError: ${e.toString()}"); ddLog.e("ERROR: ${e.toString()}"); <<< LOGGING HERE rethrow; } } } static Event buildEvent(Events event) { Frequency freq = Frequency.yearly; if (event.frequency != "" && event.frequency != null) { if (event.frequency == 'daily') { freq = Frequency.daily; } else if (event.frequency == 'weekly') { freq = Frequency.weekly; } else if (event.frequency == 'monthly') { freq = Frequency.monthly; } } if (event.eventDuration == "" || event.eventDuration == null) { event.eventDuration = "30"; } return Event( title: event.eventName!, description: event.eventDescription, location: event.location, startDate: event.eventDate!, endDate: event.eventStartTime! .add(Duration(minutes: int.parse(event.eventDuration!))), allDay: event.allDay, // iosParams: const IOSParams( // reminder: Duration(minutes: 40), // url: "http://example.com", // ), androidParams: const AndroidParams( emailInvites: ["[email protected]"], ), recurrence: Recurrence( frequency: freq, endDate: event.recurrenceEndDate, ), ); } }

感谢任何帮助

Web \ WASM不支持logger软件包,因为它导入了DART:IO。 您可以在

loggerDart

中查看更多详细信息 在“平台支持”部分下。 您还可以查看stack Overflow

您可以尝试
logging

以记录Web应用程序。

flutter logging
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.