diff --git a/example/fetch_tray_cache_example.dart b/example/fetch_tray_cache_example.dart index 9a39561..a8eed6b 100644 --- a/example/fetch_tray_cache_example.dart +++ b/example/fetch_tray_cache_example.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:fetch_tray/fetch_tray.dart'; import 'package:fetch_tray_cache_plugin/fetch_tray_cache.dart'; +import 'package:logger/logger.dart'; import 'models/post.dart'; import 'requests/get_all_posts_request.dart'; @@ -15,6 +16,8 @@ void main() async { TrayCachePlugin( cacheStoreType: TrayCacheStoreType.memory, cacheDuration: const Duration(seconds: 2), + logLevel: Level.debug, + // cacheDirectory: (await getTemporaryDirectory()).path, ), ], ); diff --git a/lib/src/fetch_tray_cache_base.dart b/lib/src/fetch_tray_cache_base.dart index 0a7f1e5..847cdc3 100644 --- a/lib/src/fetch_tray_cache_base.dart +++ b/lib/src/fetch_tray_cache_base.dart @@ -2,9 +2,9 @@ import 'package:dio/dio.dart'; import 'package:dio_cache_interceptor/dio_cache_interceptor.dart'; import 'package:dio_cache_interceptor_hive_store/dio_cache_interceptor_hive_store.dart'; import 'package:fetch_tray/fetch_tray.dart'; +import 'package:logger/logger.dart'; import '../fetch_tray_cache.dart'; -import 'interceptors/cache_interceptor.dart'; /// Type of cache store enum TrayCacheStoreType { @@ -29,11 +29,14 @@ class TrayCachePluginKeys { class TrayCachePlugin implements TrayPlugin { final TrayCacheStoreType cacheStoreType; final Duration cacheDuration; + + final Level logLevel; late final CacheStore store; TrayCachePlugin({ this.cacheStoreType = TrayCacheStoreType.memory, this.cacheDuration = const Duration(days: 7), + this.logLevel = Level.error, String? cacheDirectory, }) { switch (cacheStoreType) { @@ -51,6 +54,7 @@ class TrayCachePlugin implements TrayPlugin { TrayCacheInterceptor( cacheOptions: cacheOptions, maxAge: cacheDuration, + logLevel: logLevel, ), DioCacheInterceptor( options: cacheOptions, diff --git a/lib/src/interceptors/cache_interceptor.dart b/lib/src/interceptors/cache_interceptor.dart index ac25d98..ada3269 100644 --- a/lib/src/interceptors/cache_interceptor.dart +++ b/lib/src/interceptors/cache_interceptor.dart @@ -20,23 +20,24 @@ import 'package:logger/logger.dart'; class TrayCacheInterceptor extends Interceptor { final Duration maxAge; final CacheOptions cacheOptions; - final Logger logger = Logger( - printer: PrettyPrinter( - methodCount: 0, - printTime: true, - ), - ); + final Level logLevel; + + get logger => Logger( + printer: PrettyPrinter( + methodCount: 0, + printTime: true, + ), + level: logLevel); TrayCacheInterceptor({ required this.cacheOptions, required this.maxAge, + this.logLevel = Level.error, }); @override void onRequest( - RequestOptions options, - RequestInterceptorHandler handler, - ) async { + RequestOptions options, RequestInterceptorHandler handler) async { // TODO: implement filter for logger /* final requestShouldLog = options.extra[TrayCachePluginKeys.requestShouldLog] as bool? ?? false; */ @@ -61,7 +62,9 @@ class TrayCacheInterceptor extends Interceptor { final cacheDuration = requestCacheDuration ?? maxAge; if (difference <= cacheDuration) { - logger.i('Cache hit, returning cached response', logPrefix); + logger.i( + '$logPrefix Cache hit, returning cached response', + ); return handler.resolve( cache.toResponse( options, @@ -69,13 +72,17 @@ class TrayCacheInterceptor extends Interceptor { ), ); } else { - logger.i('Cache too old, deleting...', logPrefix); + logger.i( + '$logPrefix Cache too old, deleting...', + ); await store.delete(key); } } } - logger.i('Sending request over network', logPrefix); + logger.i( + '$logPrefix Sending request over network', + ); handler.next(options); } diff --git a/pubspec.yaml b/pubspec.yaml index 762da87..0c13cf7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,22 +1,20 @@ name: fetch_tray_cache_plugin description: A caching plugin for fetch_tray. -version: 0.0.2 +version: 0.0.4 +publish_to: none # repository: https://github.com/my_org/my_repo environment: - sdk: ^3.0.2 + sdk: ">=3.0.0 <4.0.0" # Add regular dependencies here. dependencies: - fetch_tray: - git: - url: https://github.com/marqably/fetch_tray - ref: next + fetch_tray: ^0.3.3 dio: ^5.2.1+1 dio_cache_interceptor: ^3.4.2 dio_cache_interceptor_hive_store: ^3.2.1 - logger: ^1.4.0 + logger: ^2.0.2+1 dev_dependencies: - lints: ^2.0.0 + lints: ^3.0.0 test: ^1.21.0