105 lines
4.6 KiB
Swift
105 lines
4.6 KiB
Swift
import Foundation
|
|
import HelderServices
|
|
import Testing
|
|
|
|
@Suite struct FileLoggerTests {
|
|
private let noon = Date(timeIntervalSince1970: 1_784_109_600)
|
|
|
|
private func read(_ dir: TempDirectory, _ name: String = "helder.log") throws -> String {
|
|
try dir.read(name)
|
|
}
|
|
|
|
@Test func aLineHoldsTimestampPaddedLevelPidScopeAndMessage() {
|
|
let line = FileLogger.formatLine(level: .info, scope: "git", message: "loaded", pid: 42, date: noon)
|
|
#expect(line == "2026-07-15T10:00:00.000Z INFO 42 git loaded\n")
|
|
}
|
|
|
|
@Test func continuationLinesAreIndentedSoTheyNeverLookLikeANewEntry() {
|
|
let line = FileLogger.formatLine(level: .warn, scope: "console", message: "line one\nline two\r\nline three", pid: 1, date: noon)
|
|
#expect(line == "2026-07-15T10:00:00.000Z WARN 1 console line one\n line two\n line three\n")
|
|
}
|
|
|
|
@Test func writingCreatesTheFolderAndAppendsLines() throws {
|
|
let dir = try TempDirectory()
|
|
let logger = FileLogger(directory: dir.url("a/b"))
|
|
logger.write(level: .info, scope: "boot", message: "hello")
|
|
logger.write(level: .warn, scope: "boot", message: "careful")
|
|
let lines = try dir.read("a/b/helder.log").split(separator: "\n")
|
|
#expect(lines.count == 2)
|
|
#expect(lines[0].contains("INFO") && lines[0].hasSuffix("boot hello"))
|
|
#expect(lines[1].contains("WARN") && lines[1].hasSuffix("careful"))
|
|
#expect(logger.fileURL == dir.url("a/b/helder.log"))
|
|
}
|
|
|
|
@Test func linesBelowTheFloorAreDropped() throws {
|
|
let dir = try TempDirectory()
|
|
let logger = FileLogger(directory: dir.root, minimumLevel: .warn)
|
|
logger.write(level: .debug, scope: "x", message: "debug line")
|
|
logger.write(level: .info, scope: "x", message: "info line")
|
|
logger.write(level: .error, scope: "x", message: "error line")
|
|
let out = try read(dir)
|
|
#expect(!out.contains("debug line"))
|
|
#expect(!out.contains("info line"))
|
|
#expect(out.contains("ERROR"))
|
|
}
|
|
|
|
@Test func anUnwritableFolderIsSilentlyIgnored() throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("wall", "x")
|
|
let logger = FileLogger(directory: dir.url("wall/sub"))
|
|
logger.write(level: .error, scope: "x", message: "still fine")
|
|
#expect(!dir.exists("wall/sub/helder.log"))
|
|
}
|
|
|
|
@Test func theFileRotatesPastTheCapAndKeepsWriting() throws {
|
|
let dir = try TempDirectory()
|
|
let logger = FileLogger(directory: dir.root)
|
|
let big = String(repeating: "x", count: 4000)
|
|
for _ in 0..<700 { logger.write(level: .info, scope: "bulk", message: big) }
|
|
#expect(dir.exists("helder.1.log"))
|
|
logger.write(level: .info, scope: "after", message: "still logging")
|
|
let live = try read(dir)
|
|
#expect(live.contains("still logging"))
|
|
#expect(live.utf8.count <= FileLogger.maxBytes)
|
|
}
|
|
|
|
@Test func rotationKeepsAtMostThreeOldFiles() throws {
|
|
let dir = try TempDirectory()
|
|
let logger = FileLogger(directory: dir.root)
|
|
let big = String(repeating: "y", count: 100_000)
|
|
for _ in 0..<(21 * 5) { logger.write(level: .info, scope: "bulk", message: big) }
|
|
#expect(dir.exists("helder.3.log"))
|
|
#expect(!dir.exists("helder.4.log"))
|
|
}
|
|
|
|
@Test func aLoggerPicksUpTheSizeOfAnExistingFile() throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write("helder.log", String(repeating: "z", count: FileLogger.maxBytes - 10))
|
|
FileLogger(directory: dir.root).write(level: .info, scope: "x", message: "tips it over")
|
|
#expect(dir.exists("helder.1.log"))
|
|
#expect(try read(dir).contains("tips it over"))
|
|
}
|
|
|
|
@Test func concurrentWritersNeverInterleaveWithinALine() async throws {
|
|
let dir = try TempDirectory()
|
|
let logger = FileLogger(directory: dir.root)
|
|
await withTaskGroup(of: Void.self) { group in
|
|
for i in 0..<8 {
|
|
group.addTask { for j in 0..<50 { logger.write(level: .debug, scope: "t\(i)", message: "message \(j)") } }
|
|
}
|
|
}
|
|
let lines = try read(dir).split(separator: "\n")
|
|
#expect(lines.count == 400)
|
|
#expect(lines.allSatisfy { $0.contains(" DEBUG ") && $0.contains("message ") })
|
|
}
|
|
|
|
@Test func logForwardsToTheInstalledSink() throws {
|
|
let dir = try TempDirectory()
|
|
let logger = FileLogger(directory: dir.root)
|
|
Log.setSink(logger)
|
|
Log.info("sink-test-\(dir.root.lastPathComponent)", "forwarded")
|
|
Log.setSink(nil)
|
|
#expect(try read(dir).contains("forwarded"))
|
|
}
|
|
}
|