最近項(xiàng)目需要做個(gè)日志系統(tǒng),并且壓縮上傳。雖然使用的都是第三方庫,但是還是在這里寫下吧。
使用的第三方
日志使用: https://github.com/DaveWoodCom/XCGLogger
導(dǎo)入項(xiàng)目
使用的pod 導(dǎo)入
pod "XCGLogger" #log日志
日志XCGLogger 配置
import XCGLogger
let log = XCGLogger.default 使用全局常量聲明為默認(rèn)的XCGLogger實(shí)例
在AppDelegate中進(jìn)行一些初始化操作
func application(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:Any]?) ->Bool
高級(jí)使用
控制臺(tái)輸出:
let systemDestination = AppleSystemLogDestination(identifier:"advancedLogger.systemDestination")
//設(shè)置控制臺(tái)輸出的各個(gè)配置項(xiàng)
systemDestination.outputLevel= .debug
systemDestination.showLogIdentifier=false
systemDestination.showFunctionName=true
systemDestination.showThreadName=true
systemDestination.showLevel=true
systemDestination.showFileName=true
systemDestination.showLineNumber=true
systemDestination.showDate=true
//logger對(duì)象中添加控制臺(tái)輸出
log.add(destination: systemDestination)
日志文件地址:
letlogURL =self.createLogFile()
letfileDestination =FileDestination(writeToFile: logURL,
identifier:"advancedLogger.fileDestination",
shouldAppend:true, appendMarker:"-- Relauched App --")
//設(shè)置文件輸出的各個(gè)配置項(xiàng)
fileDestination.outputLevel= .debug
fileDestination.showLogIdentifier=false
fileDestination.showFunctionName=true
fileDestination.showThreadName=true
fileDestination.showLevel=true
fileDestination.showFileName=true
fileDestination.showLineNumber=true
fileDestination.showDate=true
//文件輸出在后臺(tái)處理
fileDestination.logQueue=XCGLogger.logQueue
//logger對(duì)象中添加控制臺(tái)輸出
log.add(destination: fileDestination)
//開始啟用
log.logAppDetails()
自定義時(shí)間格式:
letdateFormatter =DateFormatter()
dateFormatter.dateFormat="yyyy-MM-dd HH:mm:ss"
dateFormatter.locale=Locale.current
log.dateFormatter= dateFormatter
最后:
log.setup(level: .debug, showThreadName:true, showLevel:true, showFileNames:true, showLineNumbers:true)
調(diào)試與發(fā)布 配置
#if DEBUG
log.setup(level: .debug, showThreadName:true, showLevel:true, showFileNames:true, showLineNumbers:true)
#else
log.setup(level: .severe, showThreadName:true, showLevel:true, showFileNames:true, showLineNumbers:true)
#end if
基本使用
log.verbose(“一個(gè)詳細(xì)的消息,通常在處理特定問題時(shí)很有用”)
log.debug(“ A debug message ”)
log.info(“一個(gè)信息消息,可能有用的為用戶在console.app中查找”)
log.warning(“警告信息,可能表示可能的錯(cuò)誤”)
log.error(“發(fā)生錯(cuò)誤,但它可以恢復(fù),只是關(guān)于發(fā)生了什么的信息”)
log.severe(“發(fā)生嚴(yán)重錯(cuò)誤,我們現(xiàn)在可能會(huì)崩潰”)
之前也沒加過日志,所以就在項(xiàng)目 登錄(超時(shí)、重連)、數(shù)據(jù)庫操作catch、 文件操作catch、所有接口返回失敗。這些地方加了log.error(".......") 。 希望有人可以留言注意要打日志地方,謝謝。