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

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(": %@", 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(": %@", 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(": %@", 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(": %@", string)
            }
        }
    }
    
    /// just output in console.
    ///
    /// - Parameters:
    ///   - message: output message.
    ///   - fileName: file name.
    ///   - methodName: method name.
    ///   - lineNumber: line number.
    static func out(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,提升调试效率)