Skip to content

Commit

Permalink
[DOC] : update string interpolation README
Browse files Browse the repository at this point in the history
  • Loading branch information
L1MeN9Yu committed Mar 2, 2022
1 parent 973a088 commit fd34f6a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
Binary file modified Documentations/README/Resources/stdout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentations/README/Resources/stdout_message_with_metadata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentations/README/Resources/stdout_xcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Builtin output target : `[stdout/stderr,file,oslog,syslog]`
* ANSI escape code
* Complete customizable for text out
* Support string interpolation the same as [Apple's Unified Logging System](https://developer.apple.com/documentation/os/logging/generating_log_messages_from_your_code)
* 100% Code Coverage

## Examples
Expand Down Expand Up @@ -143,6 +144,33 @@ See [Printer](Sources/Implements/Printer.swift) and [Printable](Sources/Protocol
The `Separator` string is used for split log message's component.
The `Formation.standard`'s separator is `" ▶ "`.

### Apple's Unified Logging System

Senna reimplement the string interpolation behavior of [Apple's Unified Logging System](https://developer.apple.com/documentation/os/logging/generating_log_messages_from_your_code)

```swift
let sink = StandardSink.out()
let formation = Formation.standard
var logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
logger[metadataKey: "UserID"] = .stringConvertible(9527)
#if DEBUG
let privacy = Privacy.public
#else
let privacy = Privacy.private
#endif
// default is private
logger.senna.notice("the user name is \("L1MeN9Yu")")
logger.senna.notice("the user name is \("L1MeN9Yu", privacy: privacy)")
```

Terminal out :

![](Documentations/README/Resources/stdout.string.interpolation.png)

see [LoggerSennaTests.swift](Tests/LoggerSennaTests.swift) for more usages.

## Installation

### Swift Package Manager
Expand All @@ -151,7 +179,7 @@ Add the following to your `Package.swift` file:

```swift
dependencies: [
.package(url: "https://github.com/L1MeN9Yu/Senna.git", from: "2.5.0")
.package(url: "https://github.com/L1MeN9Yu/Senna.git", from: "3.0.0")
]
```

Expand All @@ -161,6 +189,7 @@ dependencies: [
* [swift-log-format-and-pipe](https://github.com/Adorkable/swift-log-format-and-pipe)
* [spdlog](https://github.com/gabime/spdlog)
* [Rainbow](https://github.com/onevcat/Rainbow)
* [Logging](https://github.com/shaps80/Logging)

## License

Expand Down
81 changes: 81 additions & 0 deletions Tests/ReadmeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Created by Mengyu Li on 2022/3/2.
//

@testable import Senna
import XCTest

final class ReadmeTests: XCTestCase {
func testSimpleOut() {
// create sink
let sink = StandardSink.out()
// create formation
let formation = Formation.standard
// create log
let logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
// do some logs
logger.trace("This is a trace message")
logger.debug("This is a debug message")
logger.info("This is a info message")
logger.notice("This is a notice message")
logger.warning("This is a warning message")
logger.error("This is a error message")
logger.critical("This is a critical message")
}

func testMetaData() {
// create sink
let sink = StandardSink.out()
// create formation
let formation = Formation.standard
// create log
var logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
logger[metadataKey: "UserID"] = .stringConvertible(9527)
logger.info("message with logger meta data")
logger.info("message with both logger and message meta data", metadata: ["UserName": .string("L1MeN9Yu")])
}

func testStandardXcode() {
// create sink
let sink = StandardSink.out()
// create formation
let formation = Formation.standardXcode
// create log
var logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}

logger.trace("This is a trace message")
logger.debug("This is a debug message")
logger.info("This is a info message")
logger.notice("This is a notice message")
logger.warning("This is a warning message")
logger.error("This is a error message")
logger.critical("This is a critical message")

logger[metadataKey: "UserID"] = .stringConvertible(9527)
logger.info("message with logger meta data")
logger.info("message with both logger and message meta data", metadata: ["UserName": .string("L1MeN9Yu")])
}

func testStringInterpolation() {
let sink = StandardSink.out()
let formation = Formation.standard
var logger = Logger(label: "LogName") {
Handler(name: $0, sink: sink, formation: formation, logLevel: .trace)
}
logger[metadataKey: "UserID"] = .stringConvertible(9527)
#if DEBUG
let privacy = Privacy.public
#else
let privacy = Privacy.private
#endif
// default is private
logger.senna.notice("the user name is \("L1MeN9Yu")")
logger.senna.notice("the user name is \("L1MeN9Yu", privacy: privacy)")
}
}

0 comments on commit fd34f6a

Please sign in to comment.