-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPRuntimeLogger.swift
More file actions
53 lines (45 loc) · 1.7 KB
/
MCPRuntimeLogger.swift
File metadata and controls
53 lines (45 loc) · 1.7 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
import Foundation
import System
public final class MCPRuntimeFileLogger {
private let timestampFormat = Date.ISO8601FormatStyle.iso8601
.year()
.month()
.day()
.timeZone(separator: .omitted).time(includingFractionalSeconds: true)
private static let implementation = MCPRuntimeFileLoggerImplementation()
/// Converts a timestamp in milliseconds since the Unix epoch to a formatted date string.
private func timestamp(timeStamp: Double) -> String {
return Date(timeIntervalSince1970: timeStamp/1000).formatted(timestampFormat)
}
public func log(
logFileName: String,
level: String,
message: String,
server: String,
tool: String? = nil,
time: Double
) {
let log = "[\(timestamp(timeStamp: time))] [\(level)] [\(server)\(tool == nil ? "" : "-\(tool!))")] \(message)\(message.hasSuffix("\n") ? "" : "\n")"
Task {
await MCPRuntimeFileLogger.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)
}
}
}