最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift日志方面的好用的库

Swift crifan 3095浏览 0评论

希望找个swift的log的库

除了实现print的效果还有可以输出到文件,便于调试设备,发现问题

swift log library

NSLog alternatives for better logging with shipping apps ⋅ ObjDev

Swift: Logging — Erica Sadun

Swift Log – Devil or Why print is dangerous — iOS App Development — Medium

CocoaLumberjack/CocoaLumberjack: A fast & simple, yet powerful & flexible logging framework for Mac and iOS

The First Essential Swift 3rd Party Library To Include In Your Project – Cerebral Gardens – Blog

Logging library for Swift – Stack Overflow

goktugyil/QorumLogs: Swift Logging Utility for Xcode & Google Docs

hubertr/Swell: Logging utility for Swift and Objective C

Wolg/awesome-swift: A curated list of awesome Swift frameworks, libraries and software.

  • QorumLogs — Swift Logging Utility for Xcode & Google Docs.
  • CleanroomLogger – A configurable and extensible pure Swift logging API that is simple, lightweight and performant.
  • XCGLogger – A debug log framework for use in Swift projects.
  • Swell – A logging utility for Swift and Objective C.
  • Log – A logging tool with built-in themes, formatters, and a nice API to define your owns.

swift 日志 库

XCGLogger

XCGLogger 入门教程 – Yenn的杂物间

DaveWoodCom/XCGLogger: A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog or println, but with additional information, such as the date, function name, filename and line number.

代码:

var gLog:XCGLogger!
let DocumentDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

/***************************************************************************
 * Logging
 ***************************************************************************/
let LoggingRootDir = DocumentDirectory

let LoggingFile = LoggingRootDir.URLByAppendingPathComponent("jiandao.log")
    func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
       
        gLog = XCGLogger.defaultInstance()
        gLog.setup(
            XCGLogger.LogLevel.Debug,
            showThreadName: true,
            showLogLevel: true,
            showFileNames: true,
            showLineNumbers: true,
            writeToFile: LoggingFile,
            fileLogLevel: XCGLogger.LogLevel.Debug)
       
        gLog.verbose("A verbose message, usually useful when working on a specific problem")
        gLog.debug("A debug message")
        gLog.info("An info message, probably useful to power users looking in console.app")
        gLog.warning("A warning message, may indicate a possible error")
        gLog.error("An error occurred, but it’s recoverable, just info about what happened")
        gLog.severe("A severe error occurred, we are likely about to crash now")

效果:

模拟器输出:

2016-03-30 17:45:38.064 简道[16024:901687] Unknown class _TtC6简道14ViewController in Interface Builder file.
2016-03-30 17:49:02.377 [Info] > 简道 Version: 1.0 Build: 1 PID: 16024
2016-03-30 17:49:02.377 [Info] > XCGLogger Version: 3.3 – LogLevel: Debug
2016-03-30 17:49:02.392 [Info] > XCGLogger writing to log to: file:///Users/crifan/Library/Developer/CoreSimulator/Devices/63F89987-3382-42A5-BC13-AE102BEF98DB/data/Containers/Data/Application/9410709F-01B6-4208-886F-CDB7F6587D76/Documents/jiandao.log
2016-03-30 17:49:03.822 [Debug] [main] [AppDelegate.swift:33] application(_:willFinishLaunchingWithOptions:) > A debug message
2016-03-30 17:49:04.479 [Info] [main] [AppDelegate.swift:34] application(_:willFinishLaunchingWithOptions:) > An info message, probably useful to power users looking in console.app
2016-03-30 17:49:05.683 [Warning] [main] [AppDelegate.swift:35] application(_:willFinishLaunchingWithOptions:) > A warning message, may indicate a possible error
2016-03-30 17:49:06.281 [Error] [main] [AppDelegate.swift:36] application(_:willFinishLaunchingWithOptions:) > An error occurred, but it’s recoverable, just info about what happened
2016-03-30 17:49:06.999 [Severe] [main] [AppDelegate.swift:37] application(_:willFinishLaunchingWithOptions:) > A severe error occurred, we are likely about to crash now
(lldb)

真机输出效果:

2016-03-30 17:51:53.899 简道[327:30380] Unknown class _TtC6简道14ViewController in Interface Builder file.
2016-03-30 17:52:00.085 [Info] > 简道 Version: 1.0 Build: 1 PID: 327
2016-03-30 17:52:00.085 [Info] > XCGLogger Version: 3.3 – LogLevel: Debug
2016-03-30 17:52:00.108 [Info] > XCGLogger writing to log to: file:///var/mobile/Containers/Data/Application/0D469B28-6209-4047-98D6-4D0CBCB85B4E/Documents/jiandao.log
2016-03-30 17:52:00.109 [Debug] [main] [AppDelegate.swift:33] application(_:willFinishLaunchingWithOptions:) > A debug message
2016-03-30 17:52:00.110 [Info] [main] [AppDelegate.swift:34] application(_:willFinishLaunchingWithOptions:) > An info message, probably useful to power users looking in console.app
2016-03-30 17:52:00.111 [Warning] [main] [AppDelegate.swift:35] application(_:willFinishLaunchingWithOptions:) > A warning message, may indicate a possible error
2016-03-30 17:52:00.111 [Error] [main] [AppDelegate.swift:36] application(_:willFinishLaunchingWithOptions:) > An error occurred, but it’s recoverable, just info about what happened
2016-03-30 17:52:00.112 [Severe] [main] [AppDelegate.swift:37] application(_:willFinishLaunchingWithOptions:) > A severe error occurred, we are likely about to crash now
(lldb)

效果不错。

[后记 2016-04-06]

后来看到:

Technical Q&A QA1669: Improved logging in Objective-C

看到了,关于想要log中记录文件名,函数名,用的是:

NSLog(@"%s:%d someObject=%@", __func__, __LINE__, someObject);

即:

__func__, __LINE__

和C语言,是一致的

-》也就能猜出来,估计XCGLogger内存,也是根据是否显示文件名,函数名这些参数,决定是否添加对应的__func__, __LINE__的

结果去看源码,竟然没有找到__func__, __LINE__

抽空再继续研究吧。

转载请注明:在路上 » [已解决]swift日志方面的好用的库

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.185 seconds, using 22.08MB memory