Skip to content

Commit

Permalink
Merge pull request #350 from Baseflow/feature/logger
Browse files Browse the repository at this point in the history
Improve logging options
  • Loading branch information
renefloor authored Nov 29, 2021
2 parents 101c6a1 + d14bfa3 commit d124fe0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions flutter_cache_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [3.3.0] - 2021-11-29
* Added option to manage the log level. Doesn't print failed downloads by default anymore. You can set it like this:
```dart
CacheManager.logLevel = CacheManagerLogLevel.verbose;
```

## [3.2.0] - 2021-11-27
* [Bugfix] getSingleFile now downloads a new file before completing as the outdated file might have been deleted.

Expand Down
1 change: 1 addition & 0 deletions flutter_cache_manager/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void main() {
pubDevURL: 'https://pub.dev/packages/flutter_cache_manager',
pages: [CacheManagerPage.createPage()],
));
CacheManager.logLevel = CacheManagerLogLevel.verbose;
}

const url = 'https://blurha.sh/assets/images/img1.jpg';
Expand Down
1 change: 1 addition & 0 deletions flutter_cache_manager/lib/flutter_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export 'src/cache_manager.dart';
export 'src/cache_managers/cache_managers.dart';
export 'src/compat/file_fetcher.dart';
export 'src/config/config.dart';
export 'src/logger.dart';
export 'src/result/result.dart';
export 'src/storage/cache_info_repositories/cache_info_repositories.dart';
export 'src/web/file_service.dart';
Expand Down
16 changes: 9 additions & 7 deletions flutter_cache_manager/lib/src/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_cache_manager/src/cache_managers/base_cache_manager.dart';
import 'package:flutter_cache_manager/src/cache_store.dart';
import 'package:flutter_cache_manager/src/logger.dart';
import 'package:flutter_cache_manager/src/result/download_progress.dart';
import 'package:flutter_cache_manager/src/result/file_info.dart';
import 'package:flutter_cache_manager/src/result/file_response.dart';
Expand All @@ -23,6 +24,8 @@ import 'config/config.dart';
/// Basic cache manager implementation, which should be used as a single
/// instance.
class CacheManager implements BaseCacheManager {
static CacheManagerLogLevel logLevel = CacheManagerLogLevel.none;

/// Creates a new instance of a cache manager. This can be used to retrieve
/// files from the cache or download them online. The http headers are used
/// for the maximum age of the files. The BaseCacheManager should only be
Expand Down Expand Up @@ -130,8 +133,9 @@ class CacheManager implements BaseCacheManager {
withProgress = false;
}
} catch (e) {
print(
'CacheManager: Failed to load cached file for $url with error:\n$e');
cacheLogger.log(
'CacheManager: Failed to load cached file for $url with error:\n$e',
CacheManagerLogLevel.debug);
}
if (cacheFile == null || cacheFile.validTill.isBefore(DateTime.now())) {
try {
Expand All @@ -145,11 +149,9 @@ class CacheManager implements BaseCacheManager {
}
}
} catch (e) {
assert(() {
print(
'CacheManager: Failed to download file from $url with error:\n$e');
return true;
}());
cacheLogger.log(
'CacheManager: Failed to download file from $url with error:\n$e',
CacheManagerLogLevel.debug);
if (cacheFile == null && streamController.hasListener) {
streamController.addError(e);
}
Expand Down
5 changes: 5 additions & 0 deletions flutter_cache_manager/lib/src/cache_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:flutter_cache_manager/src/config/config.dart';
import 'package:flutter_cache_manager/src/storage/file_system/file_system.dart';
import 'package:pedantic/pedantic.dart';

import '../flutter_cache_manager.dart';
import 'logger.dart';
import 'result/file_info.dart';
import 'storage/cache_info_repositories/cache_info_repository.dart';
import 'storage/cache_object.dart';
Expand Down Expand Up @@ -41,6 +43,9 @@ class CacheStore {
return null;
}
final file = await fileSystem.createFile(cacheObject.relativePath);
cacheLogger.log(
'CacheManager: Loaded $key from cache', CacheManagerLogLevel.verbose);

return FileInfo(
file,
FileSource.Cache,
Expand Down
23 changes: 23 additions & 0 deletions flutter_cache_manager/lib/src/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import '../flutter_cache_manager.dart';

/// Instance of the cache manager. Can be set to a custom one if preferred.
CacheLogger cacheLogger = CacheLogger();

/// Log levels of the cache manager. Debug shows failed downloads and verbose
/// also shows successful downloads and cache retrievals.
enum CacheManagerLogLevel {
none,
warning,
debug,
verbose,
}

/// [CacheLogger] which is used by the cache manager to log useful information
class CacheLogger {
/// Function to log a message on a certain loglevel
void log(String message, CacheManagerLogLevel level) {
if (CacheManager.logLevel.index >= level.index) {
print(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:flutter_cache_manager/src/logger.dart';
import 'package:flutter_cache_manager/src/storage/cache_object.dart';

/// Base class for cache info repositories
Expand Down Expand Up @@ -58,7 +59,10 @@ extension MigrationExtension on CacheInfoRepository {
var cacheObjects = await previousRepository.getAllObjects();
await _putAll(cacheObjects);
var isClosed = await previousRepository.close();
if (!isClosed) print('Deleting an open repository while migrating.');
if (!isClosed) {
cacheLogger.log('Deleting an open repository while migrating.',
CacheManagerLogLevel.warning);
}
await previousRepository.deleteDataFile();
}

Expand Down
4 changes: 4 additions & 0 deletions flutter_cache_manager/lib/src/web/web_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'package:pedantic/pedantic.dart';
import 'package:rxdart/rxdart.dart';
import 'package:uuid/uuid.dart';

import '../logger.dart';

///Flutter Cache Manager
///Copyright (c) 2019 Rene Floor
///Released under MIT License.
Expand Down Expand Up @@ -60,6 +62,8 @@ class WebHelper {
_queue.add(QueueItem(url, key, authHeaders));
return;
}
cacheLogger.log(
'CacheManager: Downloading $url', CacheManagerLogLevel.verbose);

concurrentCalls++;
var subject = _memCache[key]!;
Expand Down
2 changes: 1 addition & 1 deletion flutter_cache_manager/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_cache_manager
description: Generic cache manager for flutter. Saves web files on the storages of the device and saves the cache info using sqflite.
version: 3.2.0
version: 3.3.0
homepage: https://github.com/Baseflow/flutter_cache_manager/tree/master/flutter_cache_manager

environment:
Expand Down

0 comments on commit d124fe0

Please sign in to comment.