Skip to content

Commit 094ee2e

Browse files
committed
=logging remove LoggerWithSource, keep allowing config via passing in
logger
1 parent 29271c6 commit 094ee2e

File tree

13 files changed

+48
-53
lines changed

13 files changed

+48
-53
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ var dependencies: [Package.Dependency] = [
258258

259259
// ~~~ SSWG APIs ~~~
260260

261-
// .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
262-
.package(url: "https://github.com/weissi/swift-log.git", .branch("jw-source")),
261+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
263262
// swift-metrics 1.x and 2.x are almost API compatible, so most clients should use
264263
.package(url: "https://github.com/apple/swift-metrics.git", "1.0.0" ..< "3.0.0"),
265264

Sources/DistributedActors/ActorLogging.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal final class LoggingContext {
7474
/// The preferred way of obtaining a logger for an actor or system is `context.log` or `system.log`, rather than creating new ones.
7575
public struct ActorLogger {
7676
public static func make<T>(context: ActorContext<T>) -> Logger {
77-
var log = context.system.log.logger
77+
var log = context.system.log
7878
log[metadataKey: "actor/path"] = Logger.MetadataValue.stringConvertible(context.path)
7979
return log
8080
}
@@ -116,7 +116,7 @@ public struct ActorOriginLogHandler: LogHandler {
116116
public init(_ system: ActorSystem, identifier: String? = nil) {
117117
self.init(
118118
LoggingContext(
119-
logger: system.log.logger,
119+
logger: system.log,
120120
identifier: identifier ?? system.name,
121121
useBuiltInFormatter: system.settings.logging.useBuiltInFormatter,
122122
dispatcher: { () in _hackyPThreadThreadId() }

Sources/DistributedActors/ActorSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public final class ActorSystem {
109109
// MARK: Logging
110110

111111
/// Root logger of this actor system, as configured in `LoggingSettings`.
112-
public let log: LoggerWithSource
112+
public let log: Logger
113113

114114
// ==== ----------------------------------------------------------------------------------------------------------------
115115
// MARK: Shutdown
@@ -190,7 +190,7 @@ public final class ActorSystem {
190190
rootLogger[metadataKey: "actor/node"] = "\(settings.cluster.uniqueBindNode)"
191191
}
192192
rootLogger[metadataKey: "actor/nodeName"] = "\(self.name)"
193-
self.log = rootLogger.withSource("ActorSystem(\(self.name))")
193+
self.log = rootLogger
194194

195195
// vvv~~~~~~~~~~~~~~~~~~~ all properties initialized, self can be shared ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~vvv //
196196

Sources/DistributedActors/ActorSystemSettings.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ extension Array where Element == ActorTransport {
6464
public struct LoggingSettings {
6565
public static let `default` = LoggingSettings()
6666

67-
/// At what level should the library actually log and print logs // TODO: We'd want to replace this by proper log handlers which allow config by labels
68-
public var logLevel: Logger.Level { // TODO: deprecate
67+
/// Customize the default log level of the `system.log` (and `context.log`) loggers.
68+
///
69+
/// This this modifies the current "base" logger which is `LoggingSettings.logger`,
70+
/// it is also possible to change the logger itself, e.g. if you care about reusing a specific logger
71+
/// or need to pass metadata through all loggers in the actor system.
72+
public var logLevel: Logger.Level {
6973
get {
7074
self.logger.logLevel
7175
}
@@ -77,13 +81,9 @@ public struct LoggingSettings {
7781
public var logger: Logger = LoggingSettings.makeDefaultLogger()
7882

7983
static func makeDefaultLogger() -> Logger {
80-
Logger(label: "<<ActorSystem>>")
84+
Logger(label: "<<ActorSystem>>") // replaced by specific system name during startup
8185
}
8286

83-
// /// Optionally override Logger that shall be offered to actors and the system.
84-
// /// This is used instead of globally configured `Logging.Logger()` factories by the actor system.
85-
// public var overrideLoggerFactory: ((String) -> Logger)?
86-
8787
// TODO: hope to remove this once a StdOutLogHandler lands that has formatting support;
8888
// logs are hard to follow with not consistent order of metadata etc (like system address etc).
8989
public var useBuiltInFormatter: Bool = true

Sources/DistributedActors/Cluster/Transport/TransportPipelines.swift

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ private final class InitiatingHandshakeHandler: ChannelInboundHandler, Removable
3535
typealias InboundOut = ByteBuffer
3636
typealias OutboundOut = ByteBuffer
3737

38-
private let log: LoggerWithSource
38+
private let log: Logger
3939
private let handshakeOffer: Wire.HandshakeOffer
4040
private let cluster: ClusterShell.Ref
4141

42-
init(log: LoggerWithSource, handshakeOffer: Wire.HandshakeOffer, cluster: ClusterShell.Ref) {
42+
init(log: Logger, handshakeOffer: Wire.HandshakeOffer, cluster: ClusterShell.Ref) {
4343
self.log = log
4444
self.handshakeOffer = handshakeOffer
4545
self.cluster = cluster
@@ -112,11 +112,11 @@ final class ReceivingHandshakeHandler: ChannelInboundHandler, RemovableChannelHa
112112
typealias InboundIn = ByteBuffer
113113
typealias InboundOut = Never
114114

115-
private let log: LoggerWithSource
115+
private let log: Logger
116116
private let cluster: ClusterShell.Ref
117117
private let localNode: UniqueNode
118118

119-
init(log: LoggerWithSource, cluster: ClusterShell.Ref, localNode: UniqueNode) {
119+
init(log: Logger, cluster: ClusterShell.Ref, localNode: UniqueNode) {
120120
self.log = log
121121
self.cluster = cluster
122122
self.localNode = localNode
@@ -251,9 +251,9 @@ private final class WireEnvelopeHandler: ChannelDuplexHandler {
251251
typealias InboundIn = ByteBuffer
252252
typealias InboundOut = Wire.Envelope
253253

254-
let log: LoggerWithSource
254+
let log: Logger
255255

256-
init(log: LoggerWithSource) {
256+
init(log: Logger) {
257257
self.log = log
258258
}
259259

@@ -290,10 +290,10 @@ final class OutboundSerializationHandler: ChannelOutboundHandler {
290290
typealias OutboundIn = TransportEnvelope
291291
typealias OutboundOut = Wire.Envelope
292292

293-
let log: LoggerWithSource
293+
let log: Logger
294294
let serializationPool: SerializationPool
295295

296-
init(log: LoggerWithSource, serializationPool: SerializationPool) {
296+
init(log: Logger, serializationPool: SerializationPool) {
297297
self.log = log
298298
self.serializationPool = serializationPool
299299
}
@@ -353,7 +353,7 @@ internal final class SystemMessageRedeliveryHandler: ChannelDuplexHandler {
353353
typealias InboundIn = Wire.Envelope // we handle deserialization in case it is about a system message (since message == system message envelope)
354354
typealias InboundOut = Wire.Envelope // we pass along user messages; all system messages are handled in-place here (ack, nack), or delivered directly to the recipient from this handler
355355

356-
private let log: LoggerWithSource
356+
private let log: Logger
357357
private let system: ActorSystem
358358
private let clusterShell: ClusterShell.Ref
359359

@@ -366,7 +366,7 @@ internal final class SystemMessageRedeliveryHandler: ChannelDuplexHandler {
366366
private var redeliveryScheduled: Scheduled<RedeliveryTick>?
367367

368368
init(
369-
log: LoggerWithSource,
369+
log: Logger,
370370
system: ActorSystem,
371371
cluster: ClusterShell.Ref,
372372
serializationPool: SerializationPool,
@@ -602,12 +602,12 @@ private final class UserMessageHandler: ChannelInboundHandler {
602602
typealias InboundIn = Wire.Envelope
603603
typealias InboundOut = Never // we terminate here, by sending messages off to local actors
604604

605-
let log: LoggerWithSource
605+
let log: Logger
606606

607607
let system: ActorSystem
608608
let serializationPool: SerializationPool
609609

610-
init(log: LoggerWithSource, system: ActorSystem, serializationPool: SerializationPool) {
610+
init(log: Logger, system: ActorSystem, serializationPool: SerializationPool) {
611611
self.log = log
612612
self.system = system
613613
self.serializationPool = serializationPool
@@ -649,9 +649,9 @@ private final class DumpRawBytesDebugHandler: ChannelInboundHandler {
649649
typealias InboundIn = ByteBuffer
650650

651651
let role: HandlerRole
652-
var log: LoggerWithSource
652+
var log: Logger
653653

654-
init(role: HandlerRole, log: LoggerWithSource) {
654+
init(role: HandlerRole, log: Logger) {
655655
self.role = role
656656
self.log = log
657657
}
@@ -708,7 +708,8 @@ extension ClusterShell {
708708
}
709709
}
710710

711-
let log = LoggerWithSource(system.log.logger, source: "/system/transport.server")
711+
var log = system.log
712+
log[metadataKey: "actor/path"] = "/system/transport.server" // TODO: this is a fake path, we could use log source: here if it gets merged
712713

713714
// FIXME: PASS IN FROM ASSOCIATION SINCE MUST SURVIVE CONNECTIONS! // TODO: tests about killing connections the hard way
714715
let outboundSysMsgs = OutboundSystemMessageRedelivery(settings: .default)
@@ -771,7 +772,8 @@ extension ClusterShell {
771772
}
772773
}
773774

774-
let log = LoggerWithSource(system.log.logger, source: "/system/transport.client")
775+
var log = system.log
776+
log[metadataKey: "actor/path"] = "/system/transport.client" // TODO: this is a fake path, we could use log source: here if it gets merged
775777

776778
// FIXME: PASS IN FROM ASSOCIATION SINCE MUST SURVIVE CONNECTIONS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
777779
let outboundSysMsgs = OutboundSystemMessageRedelivery(settings: .default)

Sources/DistributedActors/DeadLetters.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension ActorSystem {
7171
///
7272
/// We don't apply the special /dead path, as to not complicate diagnosing who actually terminated or if we were accidentally sent
7373
/// a remote actor ref that was dead(!)
74-
return ActorRef(.deadLetters(.init(self.log.logger, address: recipient, system: self))).adapt(from: Message.self)
74+
return ActorRef(.deadLetters(.init(self.log, address: recipient, system: self))).adapt(from: Message.self)
7575
}
7676

7777
let localRecipient: ActorAddress
@@ -82,7 +82,7 @@ extension ActorSystem {
8282
// drop the node from the address; and prepend it as known-to-be-dead
8383
localRecipient = ActorAddress(path: ActorPath._dead.appending(segments: recipient.segments), incarnation: recipient.incarnation)
8484
}
85-
return ActorRef(.deadLetters(.init(self.log.logger, address: localRecipient, system: self))).adapt(from: Message.self)
85+
return ActorRef(.deadLetters(.init(self.log, address: localRecipient, system: self))).adapt(from: Message.self)
8686
}
8787

8888
/// Anonymous `/dead/letters` reference, which may be used for messages which have no logical recipient.
@@ -140,11 +140,11 @@ extension ActorSystem {
140140
/// but not for anything more -- users shall assume that their communication is correct and only debug why a dead reference appeared if it indeed does happen.
141141
public final class DeadLetterOffice {
142142
let _address: ActorAddress
143-
let log: LoggerWithSource
143+
let log: Logger
144144
weak var system: ActorSystem?
145145

146146
init(_ log: Logger, address: ActorAddress, system: ActorSystem?) {
147-
self.log = log.withSource("DeadLetters")
147+
self.log = log
148148
self._address = address
149149
self.system = system
150150
}

Sources/DistributedActors/Serialization/Serialization+Context.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension Serialization {
2525
///
2626
/// Context MAY be accessed concurrently be encoders/decoders.
2727
public struct Context {
28-
public let log: LoggerWithSource
28+
public let log: Logger
2929
public let system: ActorSystem
3030

3131
public var serialization: Serialization {
@@ -40,7 +40,7 @@ extension Serialization {
4040
self.system.cluster.node
4141
}
4242

43-
internal init(log: LoggerWithSource, system: ActorSystem, allocator: NIO.ByteBufferAllocator) {
43+
internal init(log: Logger, system: ActorSystem, allocator: NIO.ByteBufferAllocator) {
4444
self.log = log
4545
self.system = system
4646
self.allocator = allocator

Sources/DistributedActors/Serialization/Serialization.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Foundation // for Codable
3535
/// for any kind of Message type that is possible to be received by any spawned `Actor`, sub-receive, `Gossip` instance etc.
3636
///
3737
public class Serialization {
38-
private let log: LoggerWithSource
38+
private let log: Logger
3939
internal let settings: Serialization.Settings
4040
@usableFromInline
4141
internal let metrics: ActorSystemMetrics // TODO: rather, do this via instrumentation
@@ -180,9 +180,10 @@ public class Serialization {
180180

181181
self.allocator = self.settings.allocator
182182

183-
var log = system.log.logger.withSource("serialization")
183+
var log = system.log
184184
// TODO: Dry up setting this metadata
185185
log[metadataKey: "node"] = .stringConvertible(systemSettings.cluster.uniqueBindNode)
186+
log[metadataKey: "actor/path"] = "/system/serialization" // TODO: this is a fake path, we could use log source: here if it gets merged
186187
log.logLevel = systemSettings.logging.logLevel
187188
self.log = log
188189

Sources/DistributedActorsTestKit/LogCapture.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ extension LogCapture {
109109
actorPath = "[\(path)]"
110110
}
111111

112+
metadata.removeValue(forKey: "label")
112113
if !metadata.isEmpty {
113114
metadataString = "\n// metadata:\n"
114-
for key in metadata.keys.sorted() where key != "label" {
115-
let valueDescription = self.prettyPrint(metadata: metadata[key]!)
115+
for key in metadata.keys.sorted() {
116+
let value: Logger.MetadataValue = metadata[key]!
117+
let valueDescription = self.prettyPrint(metadata: value)
116118

117119
var allString = "\n// \"\(key)\": \(valueDescription)"
118120
if allString.contains("\n") {
@@ -167,7 +169,6 @@ public struct CapturedLogMessage {
167169
let level: Logger.Level
168170
var message: Logger.Message
169171
var metadata: Logger.Metadata?
170-
let source: String
171172
let file: String
172173
let function: String
173174
let line: UInt
@@ -185,7 +186,7 @@ struct LogCaptureLogHandler: LogHandler {
185186
self.capture = capture
186187
}
187188

188-
public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, source: String, file: String, function: String, line: UInt) {
189+
public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
189190
guard self.capture.settings.filterActorPaths.contains(where: { path in self.label.starts(with: path) }) else {
190191
return // ignore this actor's logs, it was filtered out
191192
}
@@ -203,7 +204,7 @@ struct LogCaptureLogHandler: LogHandler {
203204
var _metadata: Logger.Metadata = metadata ?? [:]
204205
_metadata["label"] = "\(self.label)"
205206

206-
self.capture.append(CapturedLogMessage(date: date, level: level, message: message, metadata: _metadata, source: source, file: file, function: function, line: line))
207+
self.capture.append(CapturedLogMessage(date: date, level: level, message: message, metadata: _metadata, file: file, function: function, line: line))
207208
}
208209

209210
public subscript(metadataKey _: String) -> Logger.Metadata.Value? {

Sources/DistributedActorsXPC/ActorCell/XPCServiceCellDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal final class XPCServiceCellDelegate<Message: ActorMessage>: CellDelegate
7777
master.tell(.xpcRegisterService(self.peer, myself.asAddressable())) // TODO: do we really need it?
7878

7979
xpc_connection_set_event_handler(self.peer) { (xdict: xpc_object_t) in
80-
var log = system.log.logger.withSource("XPCService:\(myself.address.name)")
80+
var log = system.log
8181
log[metadataKey: "actor/path"] = "\(address)"
8282
// TODO: connection id?
8383

0 commit comments

Comments
 (0)