BufferedLogger 0.5.0

BufferedLogger 0.5.0

Yoheimuta 维护。



  • yohei yoshimuta

BufferedLogger

Build Status Carthage compatible Language Swift 5

BufferedLogger 是一个小巧但线程安全的 iOS 记录器,具有缓冲和重试机制。

  • 等待到输出时间,再输出日志条目。
  • 批量处理多个日志条目以同时使用。
  • 在某些错误发生后的一段时间过去后,重试输出日志条目。

您可以使用这个框架...

  • 将一组日志条目发送到您的服务器。
  • 在出现网络问题等错误时重新发送它们。

运行时要求

  • iOS 9.0 或更高版本
  • Swift5

安装

Carthage

github "yoheimuta/BufferedLogger"

用法

有关详细信息,请参阅示例项目

定义您自己的Writer

发布的日志条目将在常规时间表上缓冲并发送。

确保write方法按顺序调用,这意味着它在串行队列中运行。

class MyWriter: Writer {
    func write(_ chunk: Chunk, completion: @escaping (Bool) -> Void) {
        // You can implement something useful like uploading logs to your server.
        print("chunk is \(chunk)")

        chunk.entries.forEach {
            print("entry is \($0)")
        }

        completion(true)
    }
}

创建日志记录器并发布日志

您需要将您的writer注册到日志记录器中,并可以从中发布日志。

调用post方法不会阻塞。

import BufferedLogger

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

您还可以为缓冲和发送机制创建自己的配置。

如果您未定义您的配置,则使用以下默认设置。每个选项的意义包含在注释中。

/// Config represents a configuration for buffering and writing logs.
public struct Config {
    /// flushEntryCount is the maximum number of entries per one chunk.
    /// When the number of entries of buffer reaches this count, it starts to write a chunk.
    public let flushEntryCount: Int

    /// flushInterval is a interval to write a chunk.
    public let flushInterval: TimeInterval

    /// retryRule is a rule of retry.
    public let retryRule: RetryRule

    /// maxEntryCountInStorage is a max count of entry to be saved in the storage.
    /// When the number of entries in the storage reaches this count, it starts to
    /// delete the older entries.
    public let maxEntryCountInStorage: Int

    /// storagePath is a path to the entries.
    /// When you uses multiple BFLogger, you must set an unique path.
    public let storagePath: String

    public init(flushEntryCount: Int = 5,
                flushInterval: TimeInterval = 10,
                retryRule: RetryRule = DefaultRetryRule(retryLimit: 3),
                maxEntryCountInStorage: Int = 1000,
                storagePath: String = defaultStoragePath) {
        self.flushEntryCount = flushEntryCount
        self.flushInterval = flushInterval
        self.retryRule = retryRule
        self.maxEntryCountInStorage = maxEntryCountInStorage
        self.storagePath = storagePath
    }

    /// default is a default configuration.
    public static let `default` = Config()
}

持久性

当应用程序无法发送日志条目时,BufferedLogger会存储未发送的条目到本地存储。

默认情况下,它们存储在Library/Caches目录中的本地文件中。

您还可以定义自己的自定义日志条目存储,后端为任何存储系统。

有关更多信息,请参阅EntryStroage协议。

贡献

  • 分支
  • 创建您的功能分支:git checkout -b your-new-feature
  • 提交更改:git commit -m '添加您的功能'
  • 将更改推送到分支:git push origin your-new-feature
  • 提交pull请求

许可证

MIT许可证(MIT)

致谢

感谢Puree-Swift:[https://github.com/cookpad/Puree-Swift]

我从中受到启发,设计了界面和实现了一些功能。