Skip to content

Commit b88fac0

Browse files
committed
#115 -- Remove logging(to:) method
1 parent c366a16 commit b88fac0

File tree

7 files changed

+34
-105
lines changed

7 files changed

+34
-105
lines changed

Sources/RediStack/RedisClient.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public protocol RedisClient {
3030
/// The client's configured default logger instance, if set.
3131
var defaultLogger: Logger? { get }
3232

33-
/// Overrides the default logger on the client with the provided instance for the duration of the returned object.
34-
/// - Parameter logger: The logger instance to use in commands on the returned client instance.
35-
/// - Returns: A client using the temporary default logger override for command logging.
36-
func logging(to logger: Logger) -> RedisClient
37-
3833
/// Sends the given command to Redis.
3934
/// - Parameters:
4035
/// - command: The command to send to Redis for execution.

Sources/RediStack/RedisConnection.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,6 @@ extension RedisConnection {
364364
// MARK: Logging
365365

366366
extension RedisConnection {
367-
public func logging(to logger: Logger) -> RedisClient {
368-
return CustomLoggerRedisClient(defaultLogger: self.prepareLoggerForUse(logger), client: self)
369-
}
370-
371367
private func prepareLoggerForUse(_ logger: Logger?) -> Logger {
372368
guard var logger = logger else { return self.defaultLogger }
373369
logger[metadataKey: RedisLogging.MetadataKeys.connectionID] = "\(self.id)"

Sources/RediStack/RedisConnectionPool.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ extension RedisConnectionPool {
174174
/// For example:
175175
/// ```swift
176176
/// let countFuture = pool.leaseConnection {
177-
/// let client = $0.logging(to: myLogger)
177+
/// client in
178+
///
178179
/// return client.authorize(with: userPassword)
179180
/// .flatMap { connection.select(database: userDatabase) }
180181
/// .flatMap { connection.increment(counterKey) }
@@ -324,10 +325,6 @@ extension RedisConnectionPool {
324325
// MARK: RedisClient
325326
extension RedisConnectionPool: RedisClient {
326327
public var eventLoop: EventLoop { self.loop }
327-
328-
public func logging(to logger: Logger) -> RedisClient {
329-
return CustomLoggerRedisClient(defaultLogger: logger, client: self)
330-
}
331328

332329
public func send<CommandResult>(
333330
_ command: RedisCommand<CommandResult>,

Sources/RediStack/RedisLogging.swift

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -65,57 +65,3 @@ extension Logger {
6565
/// The prototypical instance used for Redis connection pools.
6666
public static var redisBaseConnectionPoolLogger: Logger { RedisLogging.baseConnectionPoolLogger }
6767
}
68-
69-
// MARK: RedisClient Logger Overrides
70-
71-
/// This is an implementation detail of baseline RediStack RedisClients that stores a reference to an underlying
72-
/// RedisClient and a given logger instance, which is used as a new default logger on all commands.
73-
internal struct CustomLoggerRedisClient<Client: RedisClient>: RedisClient {
74-
internal var eventLoop: EventLoop { self.client.eventLoop }
75-
76-
internal let defaultLogger: Logger
77-
78-
private let client: Client
79-
80-
internal init(defaultLogger: Logger, client: Client) {
81-
self.defaultLogger = defaultLogger
82-
self.client = client
83-
}
84-
85-
// create a new instance by just reusing the same client and passing the new logger instance
86-
87-
internal func logging(to logger: Logger) -> RedisClient {
88-
return Self(defaultLogger: logger, client: client)
89-
}
90-
91-
// forward methods to the underlying client
92-
93-
// in each case we need to explicitly create a logger variable using the provided logger argument, defaulting to
94-
// the default logger if the argument is nil, because if we do it inline, the compiler will deduce the type
95-
// as optional, allowing the (possibly) nil argument to pass through without providing the default logger in nil cases
96-
97-
internal func send<CommandResult>(_ command: RedisCommand<CommandResult>, eventLoop: EventLoop?, logger: Logger?) -> EventLoopFuture<CommandResult> {
98-
let logger = logger ?? self.defaultLogger
99-
return self.client.send(command, eventLoop: eventLoop, logger: logger)
100-
}
101-
102-
internal func unsubscribe(from channels: [RedisChannelName], eventLoop: EventLoop?, logger: Logger?) -> EventLoopFuture<Void> {
103-
let logger = logger ?? self.defaultLogger
104-
return self.client.unsubscribe(from: channels, eventLoop: eventLoop, logger: logger)
105-
}
106-
107-
internal func punsubscribe(from patterns: [String], eventLoop: EventLoop?, logger: Logger?) -> EventLoopFuture<Void> {
108-
let logger = logger ?? self.defaultLogger
109-
return self.client.punsubscribe(from: patterns, eventLoop: eventLoop, logger: logger)
110-
}
111-
112-
internal func subscribe(to channels: [RedisChannelName], eventLoop: EventLoop?, logger: Logger?, _ receiver: @escaping RedisPubSubEventReceiver) -> EventLoopFuture<Void> {
113-
let logger = logger ?? self.defaultLogger
114-
return self.client.subscribe(to: channels, eventLoop: eventLoop, logger: logger, receiver)
115-
}
116-
117-
internal func psubscribe(to patterns: [String], eventLoop: EventLoop?, logger: Logger?, _ receiver: @escaping RedisPubSubEventReceiver) -> EventLoopFuture<Void> {
118-
let logger = logger ?? self.defaultLogger
119-
return self.client.psubscribe(to: patterns, eventLoop: eventLoop, logger: logger, receiver)
120-
}
121-
}

Sources/RediStackTestUtils/Extensions/RediStack.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Foundation
16+
import Logging
1617
import NIOCore
1718
import RediStack
1819

@@ -23,9 +24,10 @@ extension RedisConnection.Configuration {
2324
public init(
2425
host: String = RedisConnection.Configuration.defaultHostname,
2526
port: Int = RedisConnection.Configuration.defaultPort,
26-
password: String? = nil
27+
password: String? = nil,
28+
defaultLogger: Logger? = nil
2729
) throws {
28-
try self.init(hostname: host, port: port, password: password)
30+
try self.init(hostname: host, port: port, password: password, defaultLogger: defaultLogger)
2931
}
3032
}
3133

Tests/RediStackIntegrationTests/RedisLoggingTests.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ final class RedisLoggingTests: RediStackIntegrationTestCase {
2424
let handler = TestLogHandler()
2525
let logger = Logger(label: #function, factory: { _ in return handler })
2626
_ = try self.connection
27-
.logging(to: logger)
28-
.ping()
27+
.ping(logger: logger)
2928
.wait()
3029
XCTAssertFalse(handler.events.isEmpty)
3130
}
@@ -34,11 +33,21 @@ final class RedisLoggingTests: RediStackIntegrationTestCase {
3433
let defaultHandler = TestLogHandler()
3534
let defaultLogger = Logger(label: #function, factory: { _ in return defaultHandler })
3635

36+
let connection = try RedisConnection.make(
37+
configuration: .init(
38+
host: self.redisHostname,
39+
port: self.redisPort,
40+
password: self.redisPassword,
41+
defaultLogger: defaultLogger
42+
),
43+
boundEventLoop: self.connection.eventLoop
44+
).wait()
45+
defaultHandler.events = [] // empty out events from initialization
46+
3747
let expectedHandler = TestLogHandler()
3848
let expectedLogger = Logger(label: "something_else", factory: { _ in return expectedHandler })
3949

40-
_ = try self.connection
41-
.logging(to: defaultLogger)
50+
_ = try connection
4251
.ping(logger: expectedLogger)
4352
.wait()
4453

@@ -51,8 +60,7 @@ final class RedisLoggingTests: RediStackIntegrationTestCase {
5160
let logger = Logger(label: #function, factory: { _ in return handler })
5261

5362
_ = try self.connection
54-
.logging(to: logger)
55-
.ping()
63+
.ping(logger: logger)
5664
.wait()
5765
XCTAssertEqual(
5866
handler.metadata[RedisLogging.MetadataKeys.connectionID],
@@ -76,8 +84,7 @@ final class RedisLoggingTests: RediStackIntegrationTestCase {
7684
pool.activate()
7785

7886
_ = try pool
79-
.logging(to: logger)
80-
.ping()
87+
.ping(logger: logger)
8188
.wait()
8289
XCTAssertTrue(handler.metadata.keys.contains(RedisLogging.MetadataKeys.connectionID))
8390
XCTAssertEqual(
@@ -108,8 +115,7 @@ final class RedisLoggingTests: RediStackIntegrationTestCase {
108115
hosts.register("default.local", instances: [address])
109116

110117
_ = try client
111-
.logging(to: logger)
112-
.ping()
118+
.ping(logger: logger)
113119
.wait()
114120
XCTAssertTrue(handler.metadata.keys.contains(RedisLogging.MetadataKeys.connectionID))
115121
XCTAssertEqual(
@@ -132,6 +138,7 @@ final class TestLogHandler: LogHandler {
132138

133139
func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
134140
self.events.append((level, message, metadata, file, function, line))
141+
// print(message, dump(metadata), file, function, line)
135142
}
136143

137144
subscript(metadataKey key: String) -> Logger.Metadata.Value? {

docs/api-design/Logging.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,29 @@ to as [_Protocol-based Context Passing_](https://forums.swift.org/t/the-context-
2828
```swift
2929
// example code, may not reflect current implementation
3030

31-
private struct CustomLoggingRedisClient: RedisClient {
32-
// a client that this object will act as a context proxy for
33-
private let client: RedisClient
34-
private let logger: Logger
35-
/* conformance to RedisClient protocol */
36-
}
37-
38-
extension RedisClient {
39-
public func logging(to logger: Logger) -> RedisClient {
40-
return CustomLoggingRedisClient(client: self, logger: logger)
41-
}
42-
}
43-
4431
let myCustomLogger = ...
4532
let connection = ...
46-
connection
47-
.logging(to: myCustomLogger) // will use this logger for all 'user-space' logs for any requests made
48-
.ping()
33+
// will use this logger for all 'user-space' logs generated while serving this command
34+
connection.ping(logger: myCustomLogger)
4935
```
5036

5137
## Log Guidelines
5238

5339
1. Prefer logging at `trace` levels
5440
1. Prefer `debug` for any log that contains metadata, especially complex ones like structs or classes
5541
- exceptions to this guideline may include metadata such as object IDs that are triggering the logs
56-
1. Dynamic values should be attached as metadata rather than string interpolated
57-
1. All log metadata keys should be added to the `RedisLogging` namespace
58-
1. Log messages should be in all lowercase, with no punctuation preferred
59-
- if a Redis command keyword (such as `QUIT`) is in the log message, it should be in all caps
60-
1. `warning` logs should be reserved for situations that could lead to `error` or `critical` conditions
61-
- this may include leaks or bad state
42+
1. Dynamic values SHOULD be attached as metadata rather than string interpolated
43+
1. All log metadata keys SHOULD be added to the `RedisLogging` namespace
44+
1. Log messages SHOULD be in all lowercase, with no punctuation preferred
45+
- if a Redis command keyword (such as `QUIT`) is in the log message, it MUST be in all caps
46+
1. `warning` logs SHOULD be reserved for situations that could lead to `error` or `critical` conditions
47+
- this MAY include leaks or bad state
6248
1. Only use `error` in situations where the error cannot be expressed by the language, such as by throwing an error or failing `EventLoopFuture`s.
6349
- this is to avoid high severity logs that developers cannot control and must create filtering mechanisms if they want to ignore emitted logs from **RediStack**
6450
1. Log a `critical` message before any `preconditionFailure` or `fatalError`
6551

6652
### Metadata
6753

68-
1. All keys should have the `rdstk` prefix to avoid collisions
69-
1. Public metadata keys should be 16 characters or less to avoid as many String allocations as possible
70-
1. Keys should be computed properties to avoid memory costs
54+
1. All keys SHOULD have the `rdstk` prefix to avoid collisions
55+
1. Public metadata keys SHOULD be 16 characters or less to avoid as many String allocations as possible
56+
1. Keys SHOULD be computed properties to avoid memory costs

0 commit comments

Comments
 (0)