diff --git a/Sources/Scribe/Plugins/FilePlugin.swift b/Sources/Scribe/Plugins/FilePlugin.swift deleted file mode 100644 index 12dc814..0000000 --- a/Sources/Scribe/Plugins/FilePlugin.swift +++ /dev/null @@ -1,43 +0,0 @@ -import o -import Plugin - -/// A Scribe Plugin to save logs to a file -public struct FilePlugin: ScribePlugin { - /// The path of the directory to write the file to - public var path: String - - /// The filename of the file to write to - public var filename: String - - /// A closure that formats the log output. If the closure returns nil, then the log data won't be written to the file. - public var outputFormatter: (Scribe.PluginPayload) async throws -> String? - - /// Initializes a `FilePlugin` - /// - /// - Parameters: - /// - path: The path of the directory to write the file to. - /// - filename: The filename of the file to write to. - /// - outputFormatter: A closure that formats the log output. If the closure returns nil, then the log data won't be written to the file. - public init( - path: String = ".", - filename: String, - outputFormatter: @escaping (Scribe.PluginPayload) async throws -> String? - ) { - self.path = path - self.filename = filename - self.outputFormatter = outputFormatter - } - - /// Handles the logging event by writing it to a file. - public func handle(value: Scribe.PluginPayload) async throws { - guard let formattedLog = try await outputFormatter(value) else { - return - } - - var fileContents: [String] = (try? o.file.in(path: path, filename: filename)) ?? [] - - fileContents.append(formattedLog) - - try o.file.out(fileContents, path: path, filename: filename) - } -} diff --git a/Sources/Scribe/Plugins/URLPlugin.swift b/Sources/Scribe/Plugins/URLPlugin.swift deleted file mode 100644 index ef83655..0000000 --- a/Sources/Scribe/Plugins/URLPlugin.swift +++ /dev/null @@ -1,54 +0,0 @@ -import Foundation -import o -import Plugin - -/// A Scribe Plugin to send logs to a remote URL -public struct URLPlugin: ScribePlugin { - /// The type alias DataResponse is defined as o.url.DataResponse. - public typealias DataResponse = o.url.DataResponse - - /// The URL of the endpoint the plugin will send data to. - public var url: URL - - /// The header fields to include in the HTTP request. - public var headerFields: [String: String] - - /// A closure that formats the `Scribe.PluginPayload` into a JSON (`Data`) object. - public var outputFormatter: (Scribe.PluginPayload) async throws -> Data - - /// A closure that handles the response from the remote URL. - public var responseHandler: (DataResponse) async throws -> Void - - /// Initializes a new `URLPlugin`. - /// - /// - Parameters: - /// - url: The URL of the endpoint to which the plugin will send log data. - /// - headerFields: The header fields to include in the HTTP request. Defaults to `["Content-Type": "application/json; charset=utf-8", "Accept": "application/json"]`. - /// - outputFormatter: A closure that formats the `Scribe.PluginPayload` into a JSON (`Data`) object. - /// - responseHandler: A closure that handles the response from the remote URL. Defaults to an empty closure. - public init( - url: URL, - headerFields: [String : String] = [ - "Content-Type": "application/json; charset=utf-8", - "Accept": "application/json" - ], - outputFormatter: @escaping (Scribe.PluginPayload) async throws -> Data, - responseHandler: @escaping (DataResponse) async throws -> Void = { _ in } - ) { - self.url = url - self.headerFields = headerFields - self.outputFormatter = outputFormatter - self.responseHandler = responseHandler - } - - /// Handles the given `Scribe.PluginPayload` by sending it to the remote URL. - public func handle(value: Scribe.PluginPayload) async throws { - let dataResponse = try await o.url.post( - url: url, - body: try await outputFormatter(value), - headerFields: headerFields - ) - - try await responseHandler(dataResponse) - } -} diff --git a/Sources/Scribe/Plugins/ScribePlugin.swift b/Sources/Scribe/ScribePlugin.swift similarity index 100% rename from Sources/Scribe/Plugins/ScribePlugin.swift rename to Sources/Scribe/ScribePlugin.swift diff --git a/Tests/ScribeTests/ScribeTests.swift b/Tests/ScribeTests/ScribeTests.swift index c8807d1..8a87119 100644 --- a/Tests/ScribeTests/ScribeTests.swift +++ b/Tests/ScribeTests/ScribeTests.swift @@ -4,35 +4,6 @@ import XCTest @testable import Scribe final class ScribeTests: XCTestCase { - func testExample() async throws { - let filename = "scribe.test" - - let scribe = Scribe( - label: "Scribe.Tests", - plugins: [ - FilePlugin(filename: filename) { payload in - "\(payload.level.rawValue.uppercased()): \(payload.message)" - } - ] - ) - - let pluginTask = scribe.info("Test") - - try await pluginTask.value - - let fileContents: [String] = (try? o.file.in(filename: filename)) ?? [] - - XCTAssertEqual(fileContents.first, "INFO: Test") - - // Clean up and delete test file - - XCTAssertNoThrow(try o.file.string(filename: filename)) - - try o.file.delete(filename: filename) - - XCTAssertThrowsError(try o.file.string(filename: filename)) - } - func testPeristablePlugin() async throws { class CountPlugin: ScribePlugin { static let shared = CountPlugin()