-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from appunite/feature_decorated_levels
Feature decorated levels
- Loading branch information
Showing
21 changed files
with
544 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// AnyLevelDecorator.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
public final class AnyLevelDecorator<Level: PSpyLevel>: PSpyLevelDecorator { | ||
private let decorator: _AnyLevelDecoratorBase<Level> | ||
|
||
public init<Decorator: PSpyLevelDecorator>(_ decorator: Decorator) where Decorator.Level == Level { | ||
self.decorator = _AnyLevelDecoratorBox(decorator) | ||
} | ||
|
||
public func decorate(level: Level, value: String) -> String { | ||
return decorator.decorate(level: level, value: value) | ||
} | ||
} | ||
|
||
private final class _AnyLevelDecoratorBox<Decorator: PSpyLevelDecorator>: _AnyLevelDecoratorBase<Decorator.Level> { | ||
let decorator: Decorator | ||
|
||
init(_ decorator: Decorator) { | ||
self.decorator = decorator | ||
super.init() | ||
} | ||
|
||
override func decorate(level: Level, value: String) -> String { | ||
return decorator.decorate(level: level, value: value) | ||
} | ||
} | ||
|
||
private class _AnyLevelDecoratorBase<Level: PSpyLevel>: PSpyLevelDecorator { | ||
func decorate(level: Level, value: String) -> String { | ||
fatalError("Must be overriden") | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Spy/Common/Formatters/Decorated/ColoredSpyLevelNameDecorator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ColoredSpyLevelNameDecorator.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
public final class ColoredSpyLevelNameDecorator: PSpyLevelDecorator { | ||
public typealias Level = SpyLevel | ||
|
||
public init() { | ||
|
||
} | ||
|
||
public func decorate(level: SpyLevel, value: String) -> String { | ||
return value.colored(with: level.color) | ||
} | ||
} | ||
|
||
private extension SpyLevel { | ||
var color: SpyColor { | ||
switch self { | ||
case .finest: return .white | ||
case .finer: return .cyan | ||
case .fine: return .blue | ||
case .config: return .magenta | ||
case .info: return .green | ||
case .warning: return .yellow | ||
case .severe: return .red | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Spy/Common/Formatters/Decorated/DecoratedLevelBuilder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// DecoratedLevelNameBuilder.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
public final class DecoratedLevelNameBuilder<Level: PSpyLevel> { | ||
private var decorators: [AnyLevelDecorator<Level>] = [] | ||
|
||
public init() { | ||
add(decorator: PlainLevelNameDecorator().toAnyDecorator()) | ||
} | ||
|
||
@discardableResult public func add(decorator: AnyLevelDecorator<Level>) -> DecoratedLevelNameBuilder<Level> { | ||
decorators.append(decorator) | ||
return self | ||
} | ||
|
||
public func build(withLevel level: Level) -> String { | ||
var decoratedLevelName = "" | ||
for decorator in decorators { | ||
decoratedLevelName = decorator.decorate(level: level, value: decoratedLevelName) | ||
} | ||
return decoratedLevelName | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Spy/Common/Formatters/Decorated/DecoratedSpyFormatter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// DecoratedSpyFormatter.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class DecoratedSpyFormatter<Level: PSpyLevel, Channel: PSpyChannel>: PSpyFormatter { | ||
let levelNameBuilder: DecoratedLevelNameBuilder<Level> | ||
|
||
public init(levelNameBuilder: DecoratedLevelNameBuilder<Level>) { | ||
self.levelNameBuilder = levelNameBuilder | ||
} | ||
|
||
public func format(timestamp: TimeInterval, level: Level, channel: Channel, message: PSpyable) -> String { | ||
return "\(Date(timeIntervalSince1970: timestamp)) \(levelNameBuilder.build(withLevel: level))::\(channel.channelName)::\(message.spyMessage)" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Spy/Common/Formatters/Decorated/EmojiPrefixedSpyLevelDecorator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// EmojiPrefixedSpyLevelNameDecorator.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
public final class EmojiPrefixedSpyLevelNameDecorator: PSpyLevelDecorator { | ||
public init() { | ||
|
||
} | ||
|
||
public func decorate(level: SpyLevel, value: String) -> String { | ||
return level.prefix + " " + value | ||
} | ||
|
||
public typealias Level = SpyLevel | ||
} | ||
|
||
private extension SpyLevel { | ||
var prefix: String { | ||
switch self { | ||
case .finest: return "💬" | ||
case .finer: return "🔊" | ||
case .fine: return "📣" | ||
case .config: return "✅" | ||
case .info: return "ℹ️" | ||
case .warning: return "⚠️" | ||
case .severe: return "⛔" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// PSpyLevelDecorator.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
public protocol PSpyLevelDecorator { | ||
associatedtype Level: PSpyLevel | ||
func decorate(level: Level, value: String) -> String | ||
} | ||
|
||
public extension PSpyLevelDecorator { | ||
func toAnyDecorator() -> AnyLevelDecorator<Level> { | ||
return AnyLevelDecorator(self) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Spy/Common/Formatters/Decorated/PlainLevelNameDecorator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// PlainLevelNameDecorator.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
public final class PlainLevelNameDecorator<Level: PSpyLevel>: PSpyLevelDecorator { | ||
public init() { | ||
|
||
} | ||
public func decorate(level: Level, value: String) -> String { | ||
return level.levelName | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// DecoratedSpyFormatterTests.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Spy | ||
|
||
final class DecoratedSpyFormatterTests: XCTestCase { | ||
func testFormat_WhenCreatedWithBuilderWithColorDecorator_ShouldReturnStringInCorrectFormat() { | ||
let sut = DecoratedSpyFormatter<SpyLevel, SpyChannel>(levelNameBuilder: DecoratedLevelNameBuilder<SpyLevel>().add(decorator: ColoredSpyLevelNameDecorator().toAnyDecorator())) | ||
let formattedLog = sut.format(timestamp: 0, level: .info, channel: .defaultChannel, message: "message") | ||
XCTAssertEqual("1970-01-01 00:00:00 +0000 \u{001B}[0;32minfo\u{001B}[0;0m::default_channel::message", formattedLog) | ||
} | ||
|
||
func testFormat_WhenCreatedWithBuilderWithEmijiDecorator_ShouldReturnStringInCorrectFormat() { | ||
let sut = DecoratedSpyFormatter<SpyLevel, SpyChannel>(levelNameBuilder: DecoratedLevelNameBuilder<SpyLevel>().add(decorator: EmojiPrefixedSpyLevelNameDecorator().toAnyDecorator())) | ||
let formattedLog = sut.format(timestamp: 0, level: .info, channel: .defaultChannel, message: "message") | ||
XCTAssertEqual("1970-01-01 00:00:00 +0000 ℹ️ info::default_channel::message", formattedLog) | ||
} | ||
|
||
func testFormat_WhenCreatedWithBuilderWithTwoDecorators_ShouldReturnStringInCorrectFormat() { | ||
let sut = DecoratedSpyFormatter<SpyLevel, SpyChannel>(levelNameBuilder: DecoratedLevelNameBuilder<SpyLevel>() | ||
.add(decorator: ColoredSpyLevelNameDecorator().toAnyDecorator()) | ||
.add(decorator: EmojiPrefixedSpyLevelNameDecorator().toAnyDecorator()) | ||
) | ||
let formattedLog = sut.format(timestamp: 0, level: .info, channel: .defaultChannel, message: "message") | ||
XCTAssertEqual("1970-01-01 00:00:00 +0000 ℹ️ \u{001B}[0;32minfo\u{001B}[0;0m::default_channel::message", formattedLog) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// EmojiPrefixedSpyLevelNameDecoratorTests.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 24/01/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Spy | ||
|
||
public final class EmojiPrefixedSpyLevelNameDecoratorTests: XCTestCase { | ||
func testDecorate_WhenCalledWithLevel_ShouldReturnCorrectString() { | ||
// given | ||
let sut = EmojiPrefixedSpyLevelNameDecorator() | ||
// then | ||
XCTAssertEqual("💬 name", sut.decorate(level: .finest, value: "name")) | ||
XCTAssertEqual("🔊 name", sut.decorate(level: .finer, value: "name")) | ||
XCTAssertEqual("📣 name", sut.decorate(level: .fine, value: "name")) | ||
XCTAssertEqual("✅ name", sut.decorate(level: .config, value: "name")) | ||
XCTAssertEqual("ℹ️ name", sut.decorate(level: .info, value: "name")) | ||
XCTAssertEqual("⚠️ name", sut.decorate(level: .warning, value: "name")) | ||
XCTAssertEqual("⛔ name", sut.decorate(level: .severe, value: "name")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.