-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPRuntimeLogger.swift
More file actions
61 lines (51 loc) · 1.89 KB
/
MCPRuntimeLogger.swift
File metadata and controls
61 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Foundation
import System
public final class MCPRuntimeFileLogger {
private lazy var dateFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter
}()
private let implementation = MCPRuntimeFileLoggerImplementation()
/// Converts a timestamp in milliseconds since the Unix epoch to a formatted date string.
private func timestamp(timeStamp: Double) -> String {
let date = Date(timeIntervalSince1970: timeStamp/1000)
return dateFormatter.string(from: date)
}
public func log(
logFileName: String,
level: String,
message: String,
server: String,
tool: String? = nil,
time: Double
) {
guard time.isFinite, time >= 0 else {
return
}
let toolSuffix = tool.map { "-\($0)" } ?? ""
let timestampStr = timestamp(timeStamp: time)
let log = "[\(timestampStr)] [\(level)] [\(server)\(toolSuffix)] \(message)\(message.hasSuffix("\n") ? "" : "\n")"
Task { [implementation] in
await implementation.logToFile(logFileName: logFileName, log: log)
}
}
}
actor MCPRuntimeFileLoggerImplementation {
private let logDir: FilePath
private var workspaceLoggers: [String: BaseFileLoggerImplementation] = [:]
public init() {
logDir = FileLoggingLocation.mcpRuntimeLogsPath
}
public func logToFile(logFileName: String, log: String) async {
if workspaceLoggers[logFileName] == nil {
workspaceLoggers[logFileName] = BaseFileLoggerImplementation(
logDir: logDir,
logFileName: logFileName
)
}
if let logger = workspaceLoggers[logFileName] {
await logger.logToFile(log)
}
}
}