Skip to content

Modernize swift log interface #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
targets: ["LoggingFormatAndPipe"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.3.0"),
],
targets: [
.target(
Expand Down
5 changes: 4 additions & 1 deletion Sources/LoggingFormatAndPipe/BasicFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ public struct BasicFormatter: Formatter {
/// Our main log formatting method
/// - Parameters:
/// - level: log level
/// - label: logger label
/// - message: actual message
/// - prettyMetadata: optional metadata that has already been "prettified"
/// - file: log's originating file
/// - function: log's originating function
/// - line: log's originating line
/// - Returns: Result of formatting the log
public func processLog(level: Logger.Level,
label: String,
message: Logger.Message,
prettyMetadata: String?,
source: String,
file: String, function: String, line: UInt) -> String {
let now = Date()

return self.format.map({ (component) -> String in
return self.processComponent(component, now: now, level: level, message: message, prettyMetadata: prettyMetadata, file: file, function: function, line: line)
return self.processComponent(component, now: now, level: level, label: label, message: message, prettyMetadata: prettyMetadata, source: source, file: file, function: function, line: line)
}).filter({ (string) -> Bool in
return string.count > 0
}).joined(separator: self.separator ?? "")
Expand Down
17 changes: 16 additions & 1 deletion Sources/LoggingFormatAndPipe/Formatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ public enum LogComponent {
/// Specifying your timestamp format can be done by providing a DateFormatter through `Formatter.timestampFormatter`
case timestamp

/// Logger label
case label
/// Log level
case level
/// The actual message
case message
/// Log metadata
case metadata
/// Log source
case source
/// The log's originating file
case file
/// The log's originating function
Expand All @@ -37,8 +41,10 @@ public enum LogComponent {
return [
.timestamp,
.level,
.label,
.message,
.metadata,
.source,
.file,
.function,
.line
Expand All @@ -53,15 +59,18 @@ public protocol Formatter {

/// Formatter's chance to format the log
/// - Parameter level: log level
/// - Parameter label: logger label
/// - Parameter message: actual message
/// - Parameter prettyMetadata: optional metadata that has already been "prettified"
/// - Parameter file: log's originating file
/// - Parameter function: log's originating function
/// - Parameter line: log's originating line
/// - Returns: Result of formatting the log
func processLog(level: Logger.Level,
label: String,
message: Logger.Message,
prettyMetadata: String?,
source: String,
file: String, function: String, line: UInt) -> String

}
Expand All @@ -78,18 +87,24 @@ extension Formatter {
/// - Parameter line: log's originating line
/// - Returns: Result of formatting the component
public func processComponent(_ component: LogComponent, now: Date, level: Logger.Level,
label: String,
message: Logger.Message,
prettyMetadata: String?,
source: String,
file: String, function: String, line: UInt) -> String {
switch component {
case .timestamp:
return self.timestampFormatter.string(from: now)
case .label:
return "\(label)"
case .level:
return "\(level)"
case .message:
return "\(message)"
case .metadata:
return "\(prettyMetadata.map { "\($0)" } ?? "")"
case .source:
return "\(source)"
case .file:
return "\(file)"
case .function:
Expand All @@ -100,7 +115,7 @@ extension Formatter {
return string
case .group(let logComponents):
return logComponents.map({ (component) -> String in
self.processComponent(component, now: now, level: level, message: message, prettyMetadata: prettyMetadata, file: file, function: function, line: line)
self.processComponent(component, now: now, level: level, label: label, message: message, prettyMetadata: prettyMetadata, source: source, file: file, function: function, line: line)
}).joined()
}
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/LoggingFormatAndPipe/Handler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public struct Handler: LogHandler {
/// - parameters:
/// - formatter: Formatter to format with
/// - pipe: PIpe to pipe to
public init(formatter: Formatter, pipe: Pipe) {
public init(label: String, formatter: Formatter, pipe: Pipe) {
self.label = label
self.formatter = formatter
self.pipe = pipe
}

/// Label of the logger
public let label: String

/// Formatter we're formatting with
public let formatter: Formatter

Expand All @@ -31,12 +35,18 @@ public struct Handler: LogHandler {
public func log(level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
source: String,
file: String, function: String, line: UInt) {
let prettyMetadata = metadata?.isEmpty ?? true
? self.prettyMetadata
: self.prettify(self.metadata.merging(metadata!, uniquingKeysWith: { _, new in new }))

let formattedMessage = self.formatter.processLog(level: level, message: message, prettyMetadata: prettyMetadata, file: file, function: function, line: line)
let formattedMessage = self.formatter.processLog(level: level,
label: self.label,
message: message,
prettyMetadata: prettyMetadata,
source: source,
file: file, function: function, line: line)
self.pipe.handle(formattedMessage)
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/LoggingFormatAndPipeTests/FormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ final class FormatterTests: XCTestCase {
func testFormatter(_ formatter: LoggingFormatAndPipe.Formatter) {
let pipe = HistoryPipe()

let logger = Logger(label: "test\(type(of: formatter))") { _ in
let logger = Logger(label: "test\(type(of: formatter))") { label in
return LoggingFormatAndPipe.Handler(
label: label,
formatter: formatter,
pipe: pipe
)
Expand Down
3 changes: 2 additions & 1 deletion Tests/LoggingFormatAndPipeTests/HandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import LoggingFormatAndPipe
final class HandlerTests: XCTestCase {
func testPiping() {
let pipe = HistoryPipe()
let logger = Logger(label: "testPiping") { _ in
let logger = Logger(label: "testPiping") { label in
return LoggingFormatAndPipe.Handler(
label: label,
formatter: BasicFormatter(),
pipe: pipe
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import LoggingFormatAndPipe

final class LoggerTextOutputStreamPipeTests: XCTestCase {
func testStdout() {
let logger = Logger(label: "testStdout") { _ in
let logger = Logger(label: "testStdout") { label in
return LoggingFormatAndPipe.Handler(
label: label,
formatter: BasicFormatter(),
pipe: LoggerTextOutputStreamPipe.standardOutput
)
Expand All @@ -26,8 +27,9 @@ final class LoggerTextOutputStreamPipeTests: XCTestCase {
}

func testStderr() {
let logger = Logger(label: "testStderr") { _ in
let logger = Logger(label: "testStderr") { label in
return LoggingFormatAndPipe.Handler(
label: label,
formatter: BasicFormatter(),
pipe: LoggerTextOutputStreamPipe.standardError
)
Expand Down