美文网首页
swift 自定义Log,提升调试效率

swift 自定义Log,提升调试效率

作者: 莫寂岚 | 来源:发表于2019-04-11 18:17 被阅读0次

mac自带的控制台,在收集程序log方面非常有用,在开发过程中,利用好mac的控制台非常有意义。
面向github开源项目,我完善编写了如下log类,用以日常开发。

import Foundation
import os


/// subsystem for current project.
///  - setup it when start a new project.
private let subsystem = "com.aby.htmlsize"


/// Custom log system.
public class Log {
    public struct Output: OptionSet {
        public let rawValue: Int

        public init(rawValue: Int) {
            self.rawValue = rawValue
        }

        public static let info = Output(rawValue: 1 << 0)
        public static let debug = Output(rawValue: 1 << 1)
        public static let warning = Output(rawValue: 1 << 2)
        public static let error = Output(rawValue: 1 << 3)

        public static let all: Output = [.info, .debug, .warning, .error]
    }
    
    /// setup log level and when does the log work.
    public static var output: Output = [.debug, .warning, .error]
    
    @available(iOS 10.0, *)
    /// level info
    static let infoLog = OSLog(subsystem: subsystem, category: "INFO")
    /// info level
    ///
    /// - Parameters:
    ///   - string: log info description
    ///   - fileName: file name
    ///   - methodName: method name
    ///   - lineNumber: line number
    public static func info(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
        #if DEBUG
        let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
        let log = "\(file):line \(lineNumber) method:\(methodName):\(string)"
        if output.contains(.info) {
            if #available(iOS 10.0, *) {
                os_log("%@", log: infoLog, type: .info, log)
            } else {
                print("<INFO>: %@", log)
            }
        }
        #endif
    }

    @available(iOS 10.0, *)
    /// level debug
    static let debugLog = OSLog(subsystem: subsystem, category: "DEBUG")
    /// debug level
    ///
    /// - Parameters:
    ///   - string: log info description
    ///   - fileName: file name
    ///   - methodName: method name
    ///   - lineNumber: line number
    public static func debug(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
        #if DEBUG
            let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
            let log = "\(file):line \(lineNumber) method:\(methodName):\(string)"
            if output.contains(.debug) {
                if #available(iOS 10.0, *) {
                    os_log("%@", log: debugLog, type: .debug, log)
                } else {
                    print("<DEBUG>: %@", log)
                }
            }
        #endif
    }

    @available(iOS 10.0, *)
    /// level warning
    static let warningLog = OSLog(subsystem: subsystem, category: "WARNING")
    public static func warning(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
        if output.contains(.warning) {
            let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
            let log = "\(file):line \(lineNumber) method:\(methodName):\(string)"
            if #available(iOS 10.0, *) {
                os_log("%@", log: warningLog, type: .fault, log)
            } else {
                print("<WARNING>: %@", string)
            }
        }
    }

    @available(iOS 10.0, *)
    /// level error
    static let errorLog = OSLog(subsystem: subsystem, category: "ERROR")
    public static func error(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
        if output.contains(.error) {
            let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
            let log = "\(file):line \(lineNumber) method:\(methodName):\n\(string)"
            if #available(iOS 10.0, *) {
                os_log("%@", log: errorLog, type: .error, log)
            } else {
                print("<ERROR>: %@", string)
            }
        }
    }
    
    /// just output in console.
    ///
    /// - Parameters:
    ///   - message: output message.
    ///   - fileName: file name.
    ///   - methodName: method name.
    ///   - lineNumber: line number.
    static func out<N>(message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line){
        #if DEBUG
            let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "")
            print("\(file):\(lineNumber) \(methodName):\(message)");
        #endif
    }
}

log分为4个级别,均可在macOS的日志台记录调试。output为了满足单纯的print console。
在程序开始启动时,可以通过

// 设置日志输出级别
Log.output = [.error, .warning]

通过

Log.debug("完成加载, 当前ScrollView的长度为:\(webView.scrollView.contentSize.height)")
Log.error("完成加载,当前ScrollView的长度为:\(webView.scrollView.contentSize.height)")
Log.warning("警告,程序有问题")

如上方式来调试程序。

相关文章

  • swift 自定义Log,提升调试效率

    mac自带的控制台,在收集程序log方面非常有用,在开发过程中,利用好mac的控制台非常有意义。面向github开...

  • Swift中自定义Log

    Swift中自定义Log Swift中自定义Log:依次是类名.方法名.行号.内容. func LTLog(fun...

  • Swift 小技巧 -- 自定义调试信息打印

    今天想记录和分享一个 Swift 中的自定义调试信息打印小技巧. 之前写过一个 OC 自定义 log 的文章, 有...

  • iOS 自定义Log

    swift自定义log一般写在AppDelegate里:AppDelegate.swift:application...

  • Swift自定义log以及其它

    一、自定义Log 在Swift中,我们也需要如OC一样,对log进行自定义。下面是一个我自定义的log,示例如下:...

  • Swift调试log写法

    1.新建Common.swift2.内容如下 3.配置Build Settings -> 搜索Swift -> O...

  • SWIFT 的一些小知识点

    1、NSLog 在 oc 中使用NSLog,在 swift 中为 print 1.1、自定义 Log 函数 1.2...

  • iOS宏关闭NSLog输出

    Release版本关闭Log输出 输出更详细的调试信息 参考文章:iOS开发-使用宏自定义输出(NSLog)

  • swift中自定义Log

    swift中自定义log输出日志,swift支持全局函数,不写在任何类中的方法可以当做全局方法使用。这里写在了Ap...

  • Swift 基础(1) -- 自定义LOG

    Swift 3.0 自定义LOG. 知识点一 :系统自带常量(1) #line : 当前行号.(2) #file...

网友评论

      本文标题:swift 自定义Log,提升调试效率

      本文链接:https://www.haomeiwen.com/subject/buyxwqtx.html