Skip to content

Commit

Permalink
Improving FileLogger performance by applying a flushable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
RockfordWei committed Jan 19, 2018
1 parent 6b2de9e commit 9988504
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
26 changes: 19 additions & 7 deletions Sources/PerfectJWTAuth/DataworkUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public class FileLogger: LogManager {
internal let _gmt: Bool
internal let encoder: JSONEncoder

internal var cache = Data()

/// size to cache the log before flushing
public static let cacheSize = 65536

/// constructor
/// - parameters:
/// - path: a local folder to store the log file. **NOTE** the log files will be access.`\(date)`.log under the folder.
Expand All @@ -141,7 +146,6 @@ public class FileLogger: LogManager {
/// - message: an extra text message for this event, could be nil
public func report(_ userId: String, level: LogLevel = .event, event: LoginManagementEvent, message: String? = nil) {
let t = StdLogger.timestamp(self._gmt)
let fileName = "\(_path)/access.\(t.0).log"
var r = LogRecord()
r.id = UUID().string
r.userId = userId
Expand All @@ -151,15 +155,23 @@ public class FileLogger: LogManager {
if let msg = message {
r.message = msg
}
guard let data = try? encoder.encode(r) else {
return
}
self.cache.append(data)
self.cache.append(contentsOf: ",\n".utf8)
guard self.cache.count > FileLogger.cacheSize else {
return
}
let fileName = "\(_path)/access.\(t.0).log"
_lock.doWithLock {
do {
let f = File(fileName)
try f.open(.append)
let data = try encoder.encode(r)
if let content = String(data: data, encoding: .utf8) {
try f.write(string: content + ",\n")
f.close()
}
let bytes:[UInt8] = self.cache.map { $0 }
try f.write(bytes: bytes)
f.close()
self.cache.removeAll()
} catch {
print("unable to log \(r)")
}
Expand Down Expand Up @@ -206,7 +218,7 @@ public final class DataworkUtility {
/// - throws: Exception
public static func explainProperties<Profile: Codable>(of: Profile) throws -> [Field] {
let data = try encoder.encode(of)
guard let json = String.init(bytes: data, encoding: .utf8),
guard let json = String(bytes: data, encoding: .utf8),
let payload = try json.jsonDecode() as? [String:Any]
else {
throw Exception.json
Expand Down
2 changes: 1 addition & 1 deletion Sources/PerfectJWTAuth/PerfectJWTAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ public class HTTPAccessControl<Profile>: HTTPRequestFilter where Profile:Codable

internal func profile(of: String) throws -> Profile {
let bytes: [UInt8] = of.utf8.map { $0 }
let data = Data.init(bytes: bytes)
let data = Data(bytes: bytes)
return try _decoder.decode(Profile.self, from: data)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/UDBMariaDB/UDBMariaDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public class UDBMariaDB<Profile>: UserDatabase {
}
let properties:[String] = fields.map { $0.name }
let columns = ["id", "salt", "shadow"] + properties
let qmarks:[String] = Array.init(repeating: "?", count: columns.count)
let qmarks:[String] = Array(repeating: "?", count: columns.count)
let col = columns.joined(separator: ",")
let que = qmarks.joined(separator: ",")
let sql = "INSERT INTO users (\(col)) VALUES(\(que))"
Expand Down
2 changes: 1 addition & 1 deletion Sources/UDBMySQL/UDBMySQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public class UDBMySQL<Profile>: UserDatabase {
}
let properties:[String] = fields.map { $0.name }
let columns = ["id", "salt", "shadow"] + properties
let qmarks:[String] = Array.init(repeating: "?", count: columns.count)
let qmarks:[String] = Array(repeating: "?", count: columns.count)
let col = columns.joined(separator: ",")
let que = qmarks.joined(separator: ",")
let sql = "INSERT INTO users (\(col)) VALUES(\(que))"
Expand Down
2 changes: 1 addition & 1 deletion Sources/UDBSQLite/UDBSQLite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class UDBSQLite<Profile>: UserDatabase {
}
let properties:[String] = fields.map { $0.name }
let columns = ["id", "salt", "shadow"] + properties
let qmarks:[String] = Array.init(repeating: "?", count: columns.count)
let qmarks:[String] = Array(repeating: "?", count: columns.count)
let col = columns.joined(separator: ",")
let que = qmarks.joined(separator: ",")
let sql = "INSERT INTO users (\(col)) VALUES(\(que))"
Expand Down

0 comments on commit 9988504

Please sign in to comment.