O2Logger.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // O2Logger.swift
  3. //
  4. //
  5. // Created by 刘振兴 on 2017/6/2.
  6. //
  7. //
  8. import Foundation
  9. import CocoaLumberjack
  10. //设置日志级别
  11. public let ddLogLevel:DDLogLevel = DDLogLevel.debug;
  12. class O2Logger {
  13. private static var fileLogger:DDFileLogger {
  14. var logFilePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
  15. logFilePath?.append("/logs")
  16. let fileManager = DDLogFileManagerDefault(logsDirectory: logFilePath)
  17. let ddFileLogger = DDFileLogger(logFileManager: fileManager)
  18. ddFileLogger.logFormatter = O2LoggerFormatter()
  19. return ddFileLogger
  20. }
  21. public static func startLogManager() {
  22. DDTTYLogger.sharedInstance.colorsEnabled = true
  23. DDTTYLogger.sharedInstance.logFormatter = O2LoggerFormatter()
  24. //加入到console
  25. DDLog.add(DDTTYLogger.sharedInstance)
  26. //加入到Apple Log System
  27. //DDLog.add(DDASLLogger.sharedInstance)
  28. //加入到自定义文件
  29. //let fileLogger:DDFileLogger = DDFileLogger()
  30. fileLogger.rollingFrequency = TimeInterval(60*60*24)
  31. fileLogger.logFileManager.maximumNumberOfLogFiles = 7
  32. DDLog.add(fileLogger)
  33. DDLogInfo("DDLog is configuration")
  34. }
  35. public static func getLogFiles() -> [O2LogFileInfo] {
  36. //fileLogger.logFileManager.sortedLogFileInfos
  37. var o2LogFiles:[O2LogFileInfo] = []
  38. let fileInfos = fileLogger.logFileManager.sortedLogFileInfos
  39. for fileInfo in fileInfos {
  40. DDLogDebug(fileInfo.filePath)
  41. let logFile = O2LogFileInfo(filePath: fileInfo.filePath, fileName: fileInfo.fileName, creationDate: fileInfo.creationDate, modificationDate: fileInfo.modificationDate, fileSize: fileInfo.fileSize, age: fileInfo.age, isArchived: fileInfo.isArchived)
  42. o2LogFiles.append(logFile)
  43. }
  44. return o2LogFiles
  45. }
  46. public static func debug(_ message:String){
  47. DDLogDebug(message)
  48. }
  49. public static func info(_ message:String){
  50. DDLogInfo(message)
  51. }
  52. public static func warn(_ message:String){
  53. DDLogWarn(message)
  54. }
  55. public static func error(_ message:String){
  56. DDLogError(message)
  57. }
  58. }
  59. class O2LoggerFormatter: NSObject,DDLogFormatter {
  60. func format(message logMessage: DDLogMessage) -> String? {
  61. //Level
  62. var level:String = "U"
  63. switch logMessage.flag {
  64. case DDLogFlag.error:
  65. level = "E"
  66. break
  67. case DDLogFlag.warning:
  68. level = "W"
  69. break
  70. case DDLogFlag.info:
  71. level = "I"
  72. break
  73. case DDLogFlag.debug:
  74. level = "D"
  75. break
  76. case DDLogFlag.verbose:
  77. level = "V"
  78. break
  79. default:
  80. level = "U"
  81. }
  82. //fileName
  83. let fileName = URL(fileURLWithPath: logMessage.file).lastPathComponent
  84. // function
  85. let fc = logMessage.function ?? ""
  86. //line
  87. let lineNumber = String(logMessage.line)
  88. //time
  89. let dateAndTime = logMessage.timestamp.toString("yyyy-MM-dd HH:mm:ss")
  90. //msg
  91. let msg = logMessage.message
  92. return "\(dateAndTime) \(fileName) \(fc) \(lineNumber) \(level) \(msg)"
  93. }
  94. }
  95. struct O2LogFileInfo {
  96. var filePath: String!
  97. var fileName: String!
  98. var creationDate: Date!
  99. var modificationDate: Date!
  100. var fileSize: UInt64!
  101. var age: TimeInterval!
  102. var isArchived: Bool!
  103. var friendFileName:String {
  104. let comps = fileName.split(" ")
  105. if comps.count > 2 {
  106. return "\(comps[1])_\(comps[2])"
  107. }else{
  108. return fileName
  109. }
  110. }
  111. // init(_ filePath:String,_ fileName:String,creationDate:Date,_ modificationDate:Date,_ fileSize:UInt64,_ age:TimeInterval,_ isArchived:Bool) {
  112. // self.filePath = filePath
  113. // self.fileName = fileName
  114. // self.creationDate = creationDate
  115. // self.modificationDate = modificationDate
  116. // self.fileSize = fileSize
  117. // self.age = age
  118. // self.isArchived = isArchived
  119. // }
  120. }