SwiftLoggly
Swift中具有丰富功能框架的简单日志记录方式。
特性
- 支持Swift 5和最新的Xcode
- 方法可以记录String、NSDictionary、Dictionary、NSError、Error。
- 为控制台日志添加了表情符号。
- 为日志追踪类型添加了丰富功能。(信息、详细、警告、调试、错误)
- 可以根据每种日志类型获取日志数量,并导出为CSV文件。
- 在mac OS的Terminal中彩色输出日志。
- 支持CocoaPods、mac OS和Vapor框架(Swift包管理器)。
- 可以启用正常、JSON格式的日志格式。
- 可以根据日志编码类型对日志进行编码(utf8、ascii、unicode、utf16等)。参考:String.Encoding。
截图
iOS/MacOS头文件导入
首先需要导入框架。请参阅安装指南了解如何将框架添加到项目中。
//iOS - Objective-C
@import SwiftLoggly;
//iOS-Swift
import SwiftLoggly
//macOS
import SwiftLogglyOSX
// Enumaration for log format type
public enum LogFormatType {
case Normal
case JSON
}
// Enumaration for log type
public enum LogType {
case Info
case Verbose
case Warnings
case Debug
case Error
}
示例
SwiftLoggly无需配置即可使用,只需调用日志函数即可。
示例 1 - Objective-C
// Log Dictionary
NSMutableDictionary *user1 = [NSMutableDictionary new];
[user1 setValue:@"vinoth" forKey:@"name" ];
[user1 setValue:@"[email protected]" forKey: @"email"];
[[Loggly logger] logglyWarningsWithDictionary:user1];
// Log string
NSString *jsonString = @"[{\"name\":\"vignesh\",\"email\":\"[email protected]\"},{\"name\":\"vinoth\",\"email\":\"[email protected]\"}]";
[[Loggly logger] logglyInfoWithText:jsonString];
示例 2 - Swift
// String
loggly(LogType.Info, text: "Write to the log!")
logglyInfo(LogType.Info, text: "Write to the log!")
// NSDictionary
loggly(LogType.Verbose, dictionary: nsDictionary)
logglyVerbose(dictionary: nsDictionary)
// Dictionary
loggly(LogType.Warnings, dictionary: dictionary)
logglyWarnings(dictionary: nsDictionary)
// Just for fun!!
loggly(LogType.Info, text: "Welcome to Swift Loggly")
loggly(LogType.Verbose, text: "Fun")
loggly(LogType.Debug, text: "is")
loggly(LogType.Warnings, text: "Matter")
loggly(LogType.Error, text: "here!!")
// Normal Type
[💙 Info - Jan 31, 2017, 1:52:38 PM]: Welcome to Swift Loggly
[💜 Warn - Jan 31, 2017, 1:52:38 PM]: Fun
[💚 Error - Jan 31, 2017, 1:52:38 PM]: is
[💛 Error - Jan 31, 2017, 1:52:38 PM]: Matter
[❤️ Error - Jan 31, 2017, 1:52:38 PM]: here!!
// JSON Type
{ "LogType" : "Info", "LogDate" : "Mar 10, 2017, 2:53:15 PM", "LogMessage" : "Welcome to Swift Loggly"}
{ "LogType" : "Verbose", "LogDate" : "Mar 10, 2017, 2:53:15 PM", "LogMessage" : "Fun"}
{ "LogType" : "Debug", "LogDate" : "Mar 10, 2017, 2:53:15 PM", "LogMessage" : "is"}
{ "LogType" : "Error", "LogDate" : "Mar 10, 2017, 2:53:15 PM", "LogMessage" : "here!!"}
{ "LogType" : "Info", "LogDate" : "Mar 10, 2017, 2:53:15 PM", "LogMessage" : "{ \"name\" : \"Vignesh\", \"Position\" : \"Senior Engineer\"}"}
这将在OS X/Ubuntu和iOS系统上创建一个日志文件。
OS X/Ubuntu的日志文件将创建在日志目录中(位于:/Library/Logs)。iOS的日志文件将创建在您的应用文档目录中的Logs文件夹下。
配置
SwiftLoggly中有几个可配置的选项。
// Enable Emojis
Loggly.logger.enableEmojis = false
//Set the log save format type
Loggly.logger.logFormateType = LogFormateType.JSON //default is "Normal"
// Set the log encoding format
Loggly.logger.logEncodingType = String.Encoding.utf8; //default is "utf8"
//Set the name of the log files
Loggly.logger.name = "Sample" //default is "logglyfile"
//Set the max size of each log file. Value is in KB
Loggly.logger.maxFileSize = 2048 //default is 1024
//Set the max number of logs files that will be kept
Loggly.logger.maxFileCount = 8 //default is 4
//Set the directory in which the logs files will be written
Loggly.logger.directory = "/Library/XXX-folder-name-XXX" //default is the standard logging directory for each platform.
//This writes to the log
logglyInfo("Write to the log!")
示例项目
示例项目位于/Examples文件夹中。
安装
CocoaPods
请访问 入门 标签页,了解如何在 cocoapods.org 上开始使用。
要在您的项目中使用 SwiftLoggly,请将以下 'Podfile' 添加到项目中
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'SwiftLoggly'
然后运行以下命令
pod install || pod update
为 Vapor 的 Swift 包管理器
您需要将以下依赖关系添加到 'Package.swift'(使用终端注释指令)
// Vapor
dependencies: [
.Package(url: "https://github.com/vigneshuvi/SwiftLoggly.git", majorVersion: 1, minor: 0)
],
Then run:
vapor build || vapor xcode
// Importing header
import SwiftLoggly
许可
SwiftLoggly 采用 MIT 许可协议。