-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSinkTests.swift
92 lines (79 loc) · 3.5 KB
/
FileSinkTests.swift
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by Mengyu Li on 2021/10/28.
//
@testable import Senna
import XCTest
final class FileSinkTests: XCTestCase {
func testFile() throws {
let fileURL = URL(fileURLWithPath: "file.log")
try Data().write(to: fileURL, options: .atomic)
let fileSink = FileSink(fileURL.path)
let logger = Logger(label: "File") {
Handler(name: $0, sink: fileSink, formation: Formation.file, logLevel: .trace)
}
var count = 10
while count > 0 {
logger.trace("\(UInt8.random(in: .min ..< .max))")
logger.debug("\(UInt8.random(in: .min ..< .max))")
logger.info("\(UInt8.random(in: .min ..< .max))")
logger.notice("\(UInt8.random(in: .min ..< .max))")
logger.warning("\(UInt8.random(in: .min ..< .max))")
logger.error("\(UInt8.random(in: .min ..< .max))")
logger.critical("\(UInt8.random(in: .min ..< .max))")
count -= 1
}
fileSink.flush().close()
guard let fileContents = try String(data: Data(contentsOf: fileURL), encoding: .utf8) else {
return XCTFail("logging to file failed")
}
XCTAssertEqual(fileContents.split(separator: "\n").count, 70)
}
func testFileFlushAlways() throws {
let fileURL = URL(fileURLWithPath: "file.log")
try Data().write(to: fileURL, options: .atomic)
let fileSink = FileSink(fileURL.path, flushMode: .always)
let logger = Logger(label: "File") {
Handler(name: $0, sink: fileSink, formation: Formation.file, logLevel: .trace)
}
var count = 10
while count > 0 {
logger.trace("\(UInt8.random(in: .min ..< .max))")
logger.debug("\(UInt8.random(in: .min ..< .max))")
logger.info("\(UInt8.random(in: .min ..< .max))")
logger.notice("\(UInt8.random(in: .min ..< .max))")
logger.warning("\(UInt8.random(in: .min ..< .max))")
logger.error("\(UInt8.random(in: .min ..< .max))")
logger.critical("\(UInt8.random(in: .min ..< .max))")
count -= 1
}
fileSink.close()
guard let fileContents = try String(data: Data(contentsOf: fileURL), encoding: .utf8) else {
return XCTFail("logging to file failed")
}
XCTAssertEqual(fileContents.split(separator: "\n").count, 70)
}
func testFileFlushWhen() throws {
let fileURL = URL(fileURLWithPath: "file.log")
try Data().write(to: fileURL, options: .atomic)
let fileSink = FileSink(fileURL.path, flushMode: .when(.warning))
let logger = Logger(label: "File") {
Handler(name: $0, sink: fileSink, formation: Formation.file, logLevel: .trace)
}
var count = 10
while count > 0 {
logger.trace("\(UInt8.random(in: .min ..< .max))")
logger.debug("\(UInt8.random(in: .min ..< .max))")
logger.info("\(UInt8.random(in: .min ..< .max))")
logger.notice("\(UInt8.random(in: .min ..< .max))")
logger.warning("\(UInt8.random(in: .min ..< .max))")
logger.error("\(UInt8.random(in: .min ..< .max))")
logger.critical("\(UInt8.random(in: .min ..< .max))")
count -= 1
}
fileSink.close()
guard let fileContents = try String(data: Data(contentsOf: fileURL), encoding: .utf8) else {
return XCTFail("logging to file failed")
}
XCTAssertEqual(fileContents.split(separator: "\n").count, 70)
}
}