swift日志系统

最近项目需要做个日志系统,并且压缩Zip上传。虽然使用的都是第三方库,但是还是在这里写下吧。 使用的第三方

日志使用:  XCGLogger

压缩使用 Zip   导入项目 使用的pod 导入

pod "XCGLogger" #log日志
pod "Zip"
复制代码

日志XCGLogger 全局

这里对全局log进行配置、以及使用

import XCGLogger
let log = XCGLogger.default    使用全局常量声明为默认的XCGLogger实例
复制代码

在AppDelegate中进行一些初始化操作

我的项目中是在登录执行完成之后执行的初始化、用于记录每个账号的日志

func application(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:Any]?) ->Bool
复制代码

配置如下:

这里设置的id参数为账号。

func logInstall(id: String){
        //控制台输出
        let systemDestination = AppleSystemLogDestination(identifier: "advancedLogger.systemDestination")
        
        //设置控制台输出的各个配置项
        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对象中添加控制台输出
        log.add(destination: systemDestination)
        
        //日志文件地址(这里不详细写了,大概是创建.txt文件返回url地址)
        let logURL = self.createLogFile(id: id)
        
        let fileDestination = FileDestination(writeToFile: logURL,
                                              identifier: "advancedLogger.fileDestination",
                                              shouldAppend: true, appendMarker: "-- Relauched App --")
        //设置文件输出的各个配置项
        fileDestination.outputLevel = .debug
        fileDestination.showLogIdentifier = false
        fileDestination.showFunctionName = true
        fileDestination.showThreadName = true
        fileDestination.showLevel = true
        fileDestination.showFileName = true
        fileDestination.showLineNumber = true
        fileDestination.showDate = true
        
        //文件输出在后台处理
        fileDestination.logQueue = XCGLogger.logQueue
        
        //logger对象中添加控制台输出
        log.add(destination: fileDestination)
        
        //开始启用
        log.logAppDetails()
        
        let dateFormatter = 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)
        
        NSSetUncaughtExceptionHandler { (exception) in
            let arr:NSArray = exception.callStackSymbols as NSArray//得到当前调用栈信息
            let reason = exception.reason//非常重要,就是崩溃的原因
            let name = exception.name//异常类型
            let content =  String("===异常错误报告===:name:\(name)===\n==reson:\(reason!)==\n==ncallStackSymbols:\n\(arr.componentsJoined(by: "\n"))")
            log.error("exception type : \(name) \n crash reason : \(reason!) \n call stack info : \(arr)====content-->\(content ?? "")")
        }
    }
复制代码

调试与发布 配置

#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(“一个详细的消息,通常在处理特定问题时很有用”)
log.debug(“ A debug message ”)
log.info(“一个信息消息,可能有用的为用户在console.app中查找”)
log.warning(“警告信息,可能表示可能的错误”)
log.error(“发生错误,但它可以恢复,只是关于发生了什么的信息”)
log.severe(“发生严重错误,我们现在可能会崩溃”)
复制代码

之前也没加过日志,所以就在项目 登录(超时、重连)、数据库操作catch、 文件操作catch、所有接口返回失败。这些地方加了log.error(".......")  。

后面就是压缩日志


### 这里是根据id 时间创建的zip文件名称
```swift
    //压缩文件
       func zipLogFil(userId: String)-> String?{
        let cachePath = FileManager.default.urls(for: .cachesDirectory,
                                                 in: .userDomainMask)[0]
        let date: Date = Date()
        let fileFormatter: DateFormatter = DateFormatter()
        fileFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        let time = fileFormatter.string(from: date)
        //日志文件夹地址(压缩文件前需要先创建压缩文件.zip文件)
        self.logPath = cachePath.appendingPathComponent(userId)
        // 创建一个zip文件
        self.zipPath = cachePath.appendingPathComponent("\(userId) \(time)_i.zip")
        
        do {
            try Zip.zipFiles(paths: [self.logPath!], zipFilePath: self.zipPath!, password: nil, progress: nil)
            return self.zipPath!.path
        }catch {
            log.error("压缩日志文件失败")
        }
        return nil
    }
复制代码
    //解压文件
    func unZip(paths: URL,toPath: URL,overwrite: Bool,password: String? = nil)-> Bool{
        do{
            try Zip.unzipFile(paths, destination: toPath, overwrite: overwrite, password: password, progress: nil)
            return true
        }catch{
            return false
        }
    }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值