Mimir 0.0.5

Mimir 0.0.5

Amer Eid 维护。



Mimir 0.0.5

  • 作者
  • Amer Eid

CI Status Pods Version License Platform

适用于高使用量 iOS 应用程序的日志框架

概述

Mimir 是一个用于在高度使用的应用程序中记录的日志框架(Swift & Objective-C),旨在使用最少的磁盘空间记录尽可能多的日志记录。

与其他日志框架不同,Mimir 将日志记录到文本文件中,但尽量保持尽可能多的日志信息,同时使用最少的磁盘空间。

用例

此框架适用于以下应用程序:

  • 频繁记录日志
  • 记录大量空间的服务器响应
  • 将日志发送给远程的开发者
  • 非常关心带宽消耗

日志也会打印到控制台。

它是如何工作的

Mimir 通过创建 2 个文本文件来实现:

  • 截断文本文件
  • 扩展文本文件

Mimir 首先会填充扩展日志文本文件,直到文件大小达到一定值。一旦扩展日志文件满,最旧的日志条目将从文件末尾删除,并移动到截断文件中,然后相应地进行截断。

该机制可以确保最早的日志消息被完整记录,而旧日志消息将被截断。

日志级别

有多个不同的日志级别可供使用

  • 🟡追踪
  • 🟢详细
  • 🔵调试
  • 🟣信息
  • 🟠警告
  • 🔴错误

日志示例

日志记录自动为每条日志消息添加以下内容

  • 日志级别
  • 文件名
  • 函数名
  • 行号
  • 日志消息(如果不是跟踪级别)
🟡 18:30:15.697 TRACE [File->SwiftExampleViewController.swift] (Func->mimirTestButtonTapped(_:)) [Line->29]
🟢 18:30:15.699 VERBOSE [File->SwiftExampleViewController.swift] (Func->mimirTestButtonTapped(_:)) [Line->30]: "This is a verbose log"
🔵 18:30:15.699 DEBUG [File->SwiftExampleViewController.swift] (Func->mimirTestButtonTapped(_:)) [Line->31]: "This is a debug log"
🟣 18:30:15.699 INFO [File->SwiftExampleViewController.swift] (Func->mimirTestButtonTapped(_:)) [Line->32]: "This is a info log"
🟠 18:30:15.699 WARNING [File->SwiftExampleViewController.swift] (Func->mimirTestButtonTapped(_:)) [Line->33]: "This is a warning log"
🔴 18:30:15.699 ERROR [File->SwiftExampleViewController.swift] (Func->mimirTestButtonTapped(_:)) [Line->34]: "This is a error log"

用法 - Swift

设置

设置 Mimir 应在该日志开始之前进行,最好是在 AppDelegate 中的 didFinishLaunchingWithOptions 开头部分。

import Mimir // Do not forget this

let fileDestination = MMRFileDestination(nameOfFile: "sample")
Mimir.addDestination(fileDestination)

let consoleDestination = MMRConsoleDestination()
Mimir.addDestination(consoleDestination)

fileDestination 也可以自定义

  • maxExtendedSize -> 扩展日志文件的最大大小(默认 5MB)
  • maxTruncatedSize -> 截断日志文件的最大大小(默认 3MB)
  • maxTruncatedLogLength -> 移动到截断日志文件的日志消息的最大长度(默认 1024)
  • extraPercentageToStartDeletion -> 删除溢出日志时的缓冲(默认 0.2)

示例

let fileDestination = MMRFileDestination(nameOfFile: "sample")

fileDestination.maxExtendedSize = 6_000_000
fileDestination.maxTruncatedSize = 4_000_000
fileDestination.maxTruncatedLogLength = 1_000
fileDestination.extraPercentageToStartDeletion = 0.4

Mimir.addDestination(fileDestination)

基础日志

import Mimir // Do not forget this

Mimir.verbose("This is a verbose log")
Mimir.debug("This is a debug log")
Mimir.info("This is a info log")
Mimir.warning("This is a warning log")
Mimir.error("This is a error log")

获取日志

获取日志非常简单直接,有多种方式可以获取日志

import Mimir // Do not forget this

// Gets an array of URL objects for the logs text files
let _ = Mimir.getLogsPaths()

// Gets the path of the folder containing the logs as a String
let _ = Mimir.getLogsDirectoryPath()

// Gets logs as a string while limiting number of bytes
let _ = Mimir.getLogsAsString(limitInBytes: 500)

用途 - Objective-C

设置

设置 Mimir 应在该日志开始之前进行,最好是在 AppDelegate 中的 didFinishLaunchingWithOptions 开头部分。

#import <Mimir/Mimir-Swift.h> // Do not forget this

MMRFileDestination* fileDestination = [[MMRFileDestination alloc] initWithNameOfFile:@"sample"];
[Mimir addDestination:fileDestination];

MMRConsoleDestination *consoleDestintion = [[MMRConsoleDestination alloc] init];
[Mimir addDestination:consoleDestintion];

fileDestination 也可以自定义

  • maxExtendedSize -> 扩展日志文件的最大大小(默认 5MB)
  • maxTruncatedSize -> 截断日志文件的最大大小(默认 3MB)
  • maxTruncatedLogLength -> 移动到截断日志文件的日志消息的最大长度(默认 1024)
  • extraPercentageToStartDeletion -> 删除溢出日志时的缓冲(默认 0.2)

示例

MMRFileDestination* fileDestination = [[MMRFileDestination alloc] initWithNameOfFile:@"sample"];

fileDestination.maxExtendedSize = 6000000;
fileDestination.maxTruncatedSize = 4000000;
fileDestination.maxTruncatedLogLength = 1000;
fileDestination.extraPercentageToStartDeletion = 0.4;

[Mimir addDestination:fileDestination];

基本日志记录

Objective-C中的日志记录函数是宏,用法也是如此

#import <Mimir/MimirObjC.h>

MimirTrace();
MimirVerbose(@"This is a verbose log");
MimirDebug(@"This is a debug log");
MimirInfo(@"This is a info log");
MimirWarning(@"This is a warning log");
MimirError(@"This is a error log");

使用宏在Objective-C中对日志进行格式化相当简单

MimirVerbose(@"This is a %@'s verbose log", @"Amer's");

🟢 18:42:56.702 VERBOSE [File->ObjCExampleViewController.m] (Func->mimirTestButtonTapped:) [Line->36]: "This is a Amer's's verbose log"

获取日志

获取日志非常简单直接,有多种方式可以获取日志

#import <Mimir/MimirObjC.h> // Do not forget this

// Gets an array of URL objects for the logs text files
NSArray<NSURL*> *logsPaths = [Mimir getLogsPaths];

// Gets the path of the folder containing the logs as a String
NSString* logsDirectoryPath = [Mimir getLogsDirectoryPath];

// Gets logs as a string while limiting number of bytes
NSString* truncatedLogsString = [Mimir getLogsAsStringWithLimitInBytes:500];

备注

此框架仍处于早期阶段,并将持续进化。

如果您使用此框架并喜欢它,请随时告知我😊

安装

Mimir可以通过CocoaPods进行安装。只需将以下行添加到您的Podfile文件中:

pod 'Mimir'

作者

Amer Eid,[email protected]

许可证

Mimir遵循MIT许可证。具体信息请参阅LICENSE文件。