-
Notifications
You must be signed in to change notification settings - Fork 0
MW-2771 - Implement Loggly for enhanced logging in location sharing services. #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d2c973a
Implement Loggly for enhanced logging in location sharing services.
261e71a
Ensure logging functions are awaited for proper asynchronous behavior.
445fc62
MW-2771 - Add cache and send logs in batches
317b5af
MW-2771 - dart format . --line-length 160
ee280ac
MW-2771 - Refactor logging methods names, add lock to Loggly Cache
ef5e83c
Merge branch 'master' into MW-2771
rafalols File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright MeWe 2025. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
|
||
| import 'package:json_annotation/json_annotation.dart'; | ||
|
|
||
| part 'loggly_log_entry.g.dart'; | ||
|
|
||
| @JsonSerializable() | ||
| class LogglyLogEntry { | ||
| @JsonKey(name: "timestamp") | ||
| final String timestamp; | ||
| @JsonKey(name: "level") | ||
| final String level; | ||
| @JsonKey(name: "message") | ||
| final String message; | ||
| @JsonKey(name: "userId") | ||
| final String? userId; | ||
| @JsonKey(name: "tags") | ||
| final List<String> tags; | ||
| @JsonKey(name: "params") | ||
| final Map<String, dynamic>? params; | ||
|
|
||
| const LogglyLogEntry({ | ||
| required this.timestamp, | ||
| required this.level, | ||
| required this.message, | ||
| this.userId, | ||
| required this.tags, | ||
| this.params, | ||
| }); | ||
|
|
||
| factory LogglyLogEntry.fromJson(Map<String, dynamic> json) => _$LogglyLogEntryFromJson(json); | ||
|
|
||
| Map<String, dynamic> toJson() => _$LogglyLogEntryToJson(this); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright MeWe 2025. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
|
||
| import 'package:dio/dio.dart'; | ||
| import 'package:retrofit/retrofit.dart'; | ||
|
|
||
| part 'loggly_service.g.dart'; | ||
|
|
||
| @RestApi() | ||
| abstract class LogglyService { | ||
| factory LogglyService(Dio dio, {String baseUrl}) = _LogglyService; | ||
|
|
||
| /// Send logs in bulk to Loggly | ||
| /// Logs in jsonl format | ||
| @POST("bulk/{token}/tag/batch/") | ||
| Future<void> sendLogsInBulk(@Path("token") String token, @Body() String logData); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright MeWe 2025. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
|
||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
| import 'package:mewe_maps/models/loggly_log_entry.dart'; | ||
| import 'package:path_provider/path_provider.dart'; | ||
|
|
||
| class LogglyCache { | ||
| static const _fileName = 'loggly_cache.jsonl'; | ||
|
|
||
| Future<File> _getLogFile() async { | ||
| final dir = await getApplicationDocumentsDirectory(); | ||
| return File('${dir.path}/$_fileName'); | ||
| } | ||
|
|
||
| Future<void> cacheLog(LogglyLogEntry log) async { | ||
| final file = await _getLogFile(); | ||
|
dmytro-mewe marked this conversation as resolved.
Outdated
|
||
| final logLine = '${jsonEncode(log.toJson())}\n'; | ||
| await file.writeAsString(logLine, mode: FileMode.append, flush: true); | ||
| } | ||
|
|
||
| Future<List<String>> readCachedLogs() async { | ||
| final file = await _getLogFile(); | ||
| if (!await file.exists()) return []; | ||
|
|
||
| final lines = await file.readAsLines(); | ||
| return lines.toList(); | ||
| } | ||
|
|
||
| Future<void> clearCache() async { | ||
| final file = await _getLogFile(); | ||
| if (await file.exists()) { | ||
| await file.delete(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright MeWe 2025. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
| // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
|
||
| import 'dart:io'; | ||
|
|
||
| import 'package:dio/dio.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter_dotenv/flutter_dotenv.dart'; | ||
| import 'package:mewe_maps/models/loggly_log_entry.dart'; | ||
| import 'package:mewe_maps/repositories/storage/storage_repository.dart'; | ||
| import 'package:mewe_maps/services/http/loggly_service.dart'; | ||
| import 'package:mewe_maps/utils/logger.dart'; | ||
| import 'package:mewe_maps/utils/loggly_cache.dart'; | ||
|
|
||
| const _TAG = "LogglyLogger"; | ||
|
|
||
| void initializeLogglyLogger() { | ||
| LogglyLogger.instance.configure(token: dotenv.env["LOGGLY_TOKEN"], baseUrl: "http://logs-01.loggly.com/"); | ||
| } | ||
|
|
||
| class LogglyLogger { | ||
| static final LogglyLogger instance = LogglyLogger._internal(); | ||
|
|
||
| LogglyService? _logglyService; | ||
| String? _token; | ||
| final _logglyCache = LogglyCache(); | ||
|
|
||
| LogglyLogger._internal(); | ||
|
|
||
| void configure({required String? token, required String baseUrl, String? tag}) { | ||
| _token = token; | ||
| final dio = Dio(); | ||
| _logglyService = LogglyService(dio, baseUrl: baseUrl); | ||
| } | ||
|
|
||
| Future<void> logToCache(String message, {String? tag, String level = 'info', Map<String, dynamic>? params}) { | ||
| final log = LogglyLogEntry( | ||
| timestamp: DateTime.now().toUtc().toIso8601String(), | ||
| level: level, | ||
| message: message, | ||
| userId: StorageRepository.user?.userId, | ||
| tags: createTags(tag), | ||
| params: params, | ||
| ); | ||
|
|
||
| return _logglyCache.cacheLog(log); | ||
| } | ||
|
|
||
| Future<void> sendLogsToLoggly() async { | ||
| try { | ||
| final logs = await _logglyCache.readCachedLogs(); | ||
| if (logs.isNotEmpty) { | ||
| // send logs in bulk (for 100 logs) | ||
| for (int i = 0; i < logs.length; i += 100) { | ||
| final bulk = logs.sublist(i, i + 100 > logs.length ? logs.length : i + 100).join("\n"); | ||
| await _logglyService?.sendLogsInBulk(_token!, bulk); | ||
| } | ||
| await _logglyCache.clearCache(); | ||
| } | ||
| Logger.log(_TAG, "Sent ${logs.length} logs to Loggly"); | ||
| } catch (e) { | ||
| Logger.log(_TAG, "Error sending logs to Loggly: $e"); | ||
| } | ||
| } | ||
|
|
||
| List<String> createTags(String? tag) { | ||
|
dmytro-mewe marked this conversation as resolved.
Outdated
|
||
| List<String> tags = []; | ||
| if (tag != null) { | ||
| tags.add(tag); | ||
| } | ||
| if (Platform.isIOS) { | ||
| tags.add("ios"); | ||
| } else if (Platform.isAndroid) { | ||
| tags.add("android"); | ||
| } | ||
| if (kDebugMode) { | ||
| tags.add("debug"); | ||
| } else { | ||
| tags.add("release"); | ||
| } | ||
| return tags; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.