Skip to content
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

Add string log formatting witnesses and builder 👷 #252

Merged

Conversation

p4checo
Copy link
Member

@p4checo p4checo commented May 25, 2022

Checklist

Motivation and Context

Until now, we relied on a custom string-based log format to convert log items into log messages. However, it was quite limited because it only allowed a predefined set of components/formats, evaluated the format string for every log item.

By modeling formatting as witnesses wrapping a closure that can be composed, more flexibility is achieved and items are formatted efficiently by performing a single (composed) closure.

To improve ergonomics, a new result builder was created to allow defining log formatting witnesses with a flexible DSL.

Description

  • Create LogItemFormatComponent protocol to model first class types for multiple composable format operations.

  • Create Log.ItemFormat namespace, to nest:

    • New LogItemFormatComponent implementations, operators and static helpers for most common operations
    • New Formatting witness and static helpers
    • New Builder result builder
  • Update Log.StringLogItemFormatter to only require a Formatting and be instantiated via a Log.ItemFormat.Builder.

  • Remove LogLevelFormatters.

  • Deprecate Log.Item.testItem in favor of Log.Item.dummy().

  • Fix and add relevant UTs.

  • Add @inlinable annotations for improved optimization possibilities.

Log item formatting:

"<timestamp> <module>? | <level emoji> <level> | [<thread>/<queue>] <function> | <file>:<line> | <msg>"

Old

let string = "$DHH:mm:ss.SSS$d $O | $C $L | [$T/$Q] $F | $n:$l | $M"

New

@Builder var string: Log.ItemFormat.Formatting<String> {
    Group(separator: " ") {
        Group(separator: " ") {
            Timestamp()
            Module()
        }
        .trimmed() // remove empty space if `nil` module
        "|"
        EmojiLevel()
        Level()
            .uppercased()
            .rightPadded(7) // pad with spaces up to max level name length
        "|"
        Group(prefix: "[", separator: "/", suffix: "]") {
            Thread()
            Queue()
        }
        Function()
        "|"
        Group(separator: ":") {
            File()
            Line()
        }
        "|"
        \.message
    }
}

@p4checo p4checo requested a review from a team May 25, 2022 23:56
@codecov
Copy link

codecov bot commented May 26, 2022

Codecov Report

Merging #252 (27cbac0) into master (d40bdce) will increase coverage by 0.22%.
The diff coverage is 98.85%.

❗ Current head 27cbac0 differs from pull request most recent head 673fac2. Consider uploading reports for the commit 673fac2 to get more accurate results

@@            Coverage Diff             @@
##           master     #252      +/-   ##
==========================================
+ Coverage   92.02%   92.24%   +0.22%     
==========================================
  Files          99      100       +1     
  Lines        3284     3367      +83     
==========================================
+ Hits         3022     3106      +84     
+ Misses        262      261       -1     
Impacted Files Coverage Δ
.../ItemFormat/Log+ItemFormat+GenericComponents.swift 96.15% <96.15%> (ø)
...ing/ItemFormat/Log+ItemFormat+ItemComponents.swift 100.00% <100.00%> (ø)
...es/Logging/ItemFormat/Log+ItemFormat.Builder.swift 100.00% <100.00%> (ø)
...Logging/ItemFormat/Log+ItemFormat.Formatting.swift 100.00% <100.00%> (ø)
...ng/ItemFormatters/Log+StringLogItemFormatter.swift 100.00% <100.00%> (+5.55%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d40bdce...673fac2. Read the comment docs.

@p4checo p4checo force-pushed the enhancement/add-string-log-format-witnesses-and-builder branch from 6510f54 to 72131b4 Compare May 31, 2022 19:05
@p4checo p4checo force-pushed the enhancement/add-string-log-format-witnesses-and-builder branch from f9d1591 to 2236296 Compare June 8, 2022 16:16
Until now, we relied on a custom string-based log format to convert log
items into log messages. However, it was quite limited because it only
allowed a predefined set of components/formats, evaluated the format
string for *every* log item.

By modeling formatting as witnesses wrapping a closure that can be
composed, more flexibility is achieved and items are formatted
efficiently by performing a single (composed) closure.

To improve ergonomics, a new result builder was created to allow
defining log formatting witnesses with a flexible DSL.

## Changes

- Create `LogItemFormatComponent` protocol to model first class types
for multiple composable format operations.

- Create `Log.ItemFormat` namespace, nesting:
  + Multiple `LogItemFormatComponent` implementations and static
  helpers for most common use cases.
  + New `Formatting` witnesses
  + New `Builder` result builder to build formatting witnesses.

- Update `Log.StringLogItemFormatter` to only require a `Formatting` and
be instantiated via a `FormattingBuilder`.

- Remove `LogLevelFormatter`s.

- Add `@inlinable` annotations for improved optimization possibilities.

- Deprecate `Log.Item.testItem` in favor of `Log.Item.dummy()`.

- Fix and add relevant UTs.
@p4checo p4checo force-pushed the enhancement/add-string-log-format-witnesses-and-builder branch from 27cbac0 to 673fac2 Compare June 15, 2022 18:30
@p4checo p4checo merged commit acfe7d4 into master Jun 15, 2022
@p4checo p4checo deleted the enhancement/add-string-log-format-witnesses-and-builder branch June 15, 2022 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants