自从开始做iOS开发, 才知道以前的Android中的Log是多么的灵活好用. 如下图我的Android Studio的Logcat窗口:
- 不同level的日志用不同颜色显示: info-绿色, debug-蓝色, warning-黄色, error-红色;
- 每条日志显示该条日志所在的Class, 文件名,行数, 点击文件名可以轻松的打开日志所在的程序行;
- 每条日志都带时间戳;
反观iOS用print或者NSLog打印出来的日志:
Sending `RECEIVE_LAST_RESTART` with no listeners registered.
Warning: PropTypes has been moved to a separate package. Accessing React.PropTypes is no longer supported and will be removed completely in React 16. Use the prop-types package on npm instead. (https://fb.me/migrating-from-react-proptypes)
Warning: React.createClass is no longer supported. Use a plain JavaScript class instead. If you're not yet ready to migrate, create-react-class is available on npm as a drop-in replacement. (https://fb.me/migrating-from-react-create-class)
DDC succeed
Running application "application" with appParams: {"rootTag":1,"initialProps":{"tenantId":"telia-dev","lang":"en","uri":"/dashboard","env":"development"}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
fetching deviceIDSurrogateKey: 7AA86AF4-40F8-48D0-9DF3-C6F8556E5ADD, <NSThread: 0x608000c78780>{number = 8, name = (null)}
deviceIDSurrogateKey: ---> 7AA86AF4-40F8-48D0-9DF3-C6F8556E5ADD
Failed to load previous state
Failed to register for remote push remote notifications are not supported in the simulator
print不带时间戳, 不区分日志级别, 无颜色区分, 不知道日志所在文件/行数......
SwiftyLog登场
这种日志在日常开发中查看起来非常的不方便. 基于此, 我将print做了一个简单的封装, 基本功能包括:
- 使iOS的日志基本达到与Android的Logcat相当的效果, 可以显示时间戳, 所在文件类名,行数,颜色区分
2018-04-16T14:01:34.528Z 💚d-[AppDelegate.swift#application(_:didFinishLaunchingWithOptions:)#24]: App Started....
2018-04-16T14:01:34.543Z 💚d-[ViewController.swift#viewDidLoad()#17]: ViewController loaded
2018-04-16T14:01:35.657Z 💙i-[ViewController.swift#buttonTapped#21]: buttton tapped
2018-04-16T14:01:35.658Z 💛w-[ViewController.swift#buttonTapped#22]: this is a warning message
2018-04-16T14:01:35.658Z ❤️e-[ViewController.swift#buttonTapped#23]: ooops, something is wrong.
- 日志可以输出到debug console, device console, 或者在摇晃手机上直接显示, 并可以将日志发送到指定的email:
SwiftyLog使用姿势:
SwiftyLog在Github开源, 查看源码请用力狂戳这里: https://github.com/zhihuitang/SwiftyLog
SwiftyLog支持Cocoapods和Carthage, 根据个人喜好选择合适的.
Cocoapods:
pod 'SwiftyLog'
Carthage:
github "zhihuitang/SwiftyLog.git"
Cocoapods或者Carthage设置好后, 执行pod install
或者carthage update
下载更新库即可使用了. 在AppDelegate.swift
中导入SwiftyLog并初始化:
import UIKit
import SwiftyLog
// 1
let logger = Logger.shared
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 2
#if DEBUG
// 3
logger.level = .info
logger.ouput = .debugerConsoleAndFile
#else
//4
logger.level = .none
#endif
logger.d("App Started....")
return true
}
}
上面的代码中:
- 全局初始化SwiftyLog, 这样在本工程内其他任何地方直接引用logger即可, 无需导入SwiftyLog;
-
#if DEBUG
表示日志只在DEBUG模式下有效, 在非DEBUG模式下不记录日志, 摇晃手机也不会弹出日志查看窗口; - 设置日志输出的级别以及输出位置.
日志分为5个级别:.info
->.debug
->.warning
->.error
->.none
, 低于logLevel的日志将不会记录. 如果设置logLevel为.none
表示关闭日志,所有日志均不记录 - 非DEBUG模式关闭日志输出
logger.level = .none
logger.ouput = .debugerConsoleAndFile
设置日志输出到Xcode的console并输出到device文件.只有输出到.debugerConsoleAndFile
和.deviceConsoleAndFile
的日志才可以通过摇晃手机查看日志.
欢迎提意见
由于本人才疏学浅, 这个framework还是很简单粗糙, 希望大家使用的同时多包涵, 也欢迎大家的意见, 我第一时间回复修改.