项目作者: yoheimuta

项目描述 :
Tiny but thread-safe logger with a buffering and retrying mechanism for iOS
高级语言: Swift
项目地址: git://github.com/yoheimuta/BufferedLogger.git
创建时间: 2018-07-25T03:01:49Z
项目社区:https://github.com/yoheimuta/BufferedLogger

开源协议:MIT License

下载


BufferedLogger

Build Status
Carthage compatible
Language Swift 5
Version
License

BufferedLogger is a tiny but thread-safe logger with a buffering and retrying mechanism for iOS.

  • Buffer log entries until it’s time to output them.
  • Batch multiple log entries to use them at the same time.
  • Retry outputing log entries when some backoff time elapsed after some errors occurred.

You can use this framework…

  • To send a group of log entries to your server.
  • To resend them when some errors like a networking trouble were occured.

Runtime Requirements

  • iOS 9.0 or later
  • Swift5

Installation

Carthage

  1. github "yoheimuta/BufferedLogger"

CocoaPods

  1. pod "BufferedLogger"

Usage

For details, refer to a Demo project.

Define your own Writer

Posted log entries are buffered and emitted as a chunk on a routine schedule.

write method is ensured to call serially, which means it is run by a serial queue.

  1. class MyWriter: Writer {
  2. func write(_ chunk: Chunk, completion: @escaping (Bool) -> Void) {
  3. // You can implement something useful like uploading logs to your server.
  4. print("chunk is \(chunk)")
  5. chunk.entries.forEach {
  6. print("entry is \($0)")
  7. }
  8. completion(true)
  9. }
  10. }

Make the logger and post a log

You have to register your writer to the logger and can post your log with it from any thread.

A call to post method is no blocking.

  1. import BufferedLogger
  2. logger = BFLogger(writer: MyWriter())
  3. logger.post("1".data(using: .utf8)!)
  4. logger.post("2".data(using: .utf8)!)
  5. logger.post("3".data(using: .utf8)!)

You can also create your configuration for buffering and emitting mechanism.

If you omit to define your configuration, default below is used. Each meaning is in a comment.

  1. /// Config represents a configuration for buffering and writing logs.
  2. public struct Config {
  3. /// flushEntryCount is the maximum number of entries per one chunk.
  4. /// When the number of entries of buffer reaches this count, it starts to write a chunk.
  5. public let flushEntryCount: Int
  6. /// flushInterval is a interval to write a chunk.
  7. public let flushInterval: TimeInterval
  8. /// retryRule is a rule of retry.
  9. public let retryRule: RetryRule
  10. /// maxEntryCountInStorage is a max count of entry to be saved in the storage.
  11. /// When the number of entries in the storage reaches this count, it starts to
  12. /// delete the older entries.
  13. public let maxEntryCountInStorage: Int
  14. /// storagePath is a path to the entries.
  15. /// When you uses multiple BFLogger, you must set an unique path.
  16. public let storagePath: String
  17. public init(flushEntryCount: Int = 5,
  18. flushInterval: TimeInterval = 10,
  19. retryRule: RetryRule = DefaultRetryRule(retryLimit: 3),
  20. maxEntryCountInStorage: Int = 1000,
  21. storagePath: String = defaultStoragePath) {
  22. self.flushEntryCount = flushEntryCount
  23. self.flushInterval = flushInterval
  24. self.retryRule = retryRule
  25. self.maxEntryCountInStorage = maxEntryCountInStorage
  26. self.storagePath = storagePath
  27. }
  28. /// default is a default configuration.
  29. public static let `default` = Config()
  30. }

Persistence

BufferedLogger stores the unsent entries in the local storage when the application couldn’t send log entries.

By default, it stores them in local files in the Library/Caches directory.

You can also define your own custom log entry storage backed by any storage system.

See the EntryStroage protocol for more details.

Contributing

  • Fork it
  • Create your feature branch: git checkout -b your-new-feature
  • Commit changes: git commit -m ‘Add your feature’
  • Push to the branch: git push origin your-new-feature
  • Submit a pull request

License

The MIT License (MIT)

Acknowledgement

Thank you to the Puree-Swift: https://github.com/cookpad/Puree-Swift

I inspired by this library for the elaborate design, interface and some implementation.