Skip to content

Commit 103c143

Browse files
authored
Remove [Lifecycle] prefix from log messages (#95)
Motivation: Since Swift Log 1.3.0, log handlers can include the source of a particular message, rendering the [Lifecycle] prefix unnecessary. Modifications: Removed the [Lifecycle] prefix from all log messages by removing the underlying log helper and calling out to the Logger itself. Result: Log messages written by a log handler which includes the location wont log "Lifecycle" twice.
1 parent 4714c3c commit 103c143

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

Sources/Lifecycle/Lifecycle.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public struct ServiceLifecycle {
258258
}
259259

260260
private func log(_ message: String) {
261-
self.underlying.log(message)
261+
self.underlying.logger.info("\(message)")
262262
}
263263
}
264264

@@ -357,7 +357,7 @@ struct ShutdownError: Error {
357357
/// `ComponentLifecycle` provides a basic mechanism to cleanly startup and shutdown a subsystem in a larger application, freeing resources in order before exiting.
358358
public class ComponentLifecycle: LifecycleTask {
359359
public let label: String
360-
private let logger: Logger
360+
fileprivate let logger: Logger
361361
internal let shutdownGroup = DispatchGroup()
362362

363363
private var state = State.idle([])
@@ -435,7 +435,7 @@ public class ComponentLifecycle: LifecycleTask {
435435
callback(nil)
436436
case .shutdown:
437437
self.stateLock.unlock()
438-
self.log(level: .warning, "already shutdown")
438+
self.logger.warning("already shutdown")
439439
callback(nil)
440440
case .starting(let queue):
441441
self.state = .shuttingDown(queue)
@@ -476,11 +476,11 @@ public class ComponentLifecycle: LifecycleTask {
476476
self.state = .starting(queue)
477477
}
478478

479-
self.log("starting")
479+
self.logger.info("starting")
480480
Counter(label: "\(self.label).lifecycle.start").increment()
481481

482482
if tasks.count == 0 {
483-
self.log(level: .notice, "no tasks provided")
483+
self.logger.notice("no tasks provided")
484484
}
485485
self.startTask(on: queue, tasks: tasks, index: 0) { started, error in
486486
self.stateLock.lock()
@@ -519,12 +519,12 @@ public class ComponentLifecycle: LifecycleTask {
519519
if index >= tasks.count {
520520
return callback(index, nil)
521521
}
522-
self.log("starting tasks [\(tasks[index].label)]")
522+
self.logger.info("starting tasks [\(tasks[index].label)]")
523523
let startTime = DispatchTime.now()
524524
start { error in
525525
Timer(label: "\(self.label).\(tasks[index].label).lifecycle.start").recordNanoseconds(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds)
526526
if let error = error {
527-
self.log(level: .error, "failed to start [\(tasks[index].label)]: \(error)")
527+
self.logger.error("failed to start [\(tasks[index].label)]: \(error)")
528528
return callback(index, error)
529529
}
530530
// shutdown called while starting
@@ -540,7 +540,7 @@ public class ComponentLifecycle: LifecycleTask {
540540
self.state = .shuttingDown(queue)
541541
}
542542

543-
self.log("shutting down")
543+
self.logger.info("shutting down")
544544
Counter(label: "\(self.label).lifecycle.shutdown").increment()
545545

546546
self.shutdownTask(on: queue, tasks: tasks.reversed(), index: 0, errors: nil) { errors in
@@ -550,7 +550,7 @@ public class ComponentLifecycle: LifecycleTask {
550550
}
551551
self.state = .shutdown(errors)
552552
}
553-
self.log("bye")
553+
self.logger.info("bye")
554554
callback()
555555
}
556556
}
@@ -564,7 +564,7 @@ public class ComponentLifecycle: LifecycleTask {
564564
return callback(errors)
565565
}
566566

567-
self.log("stopping tasks [\(tasks[index].label)]")
567+
self.logger.info("stopping tasks [\(tasks[index].label)]")
568568
let startTime = DispatchTime.now()
569569
shutdown { error in
570570
Timer(label: "\(self.label).\(tasks[index].label).lifecycle.shutdown").recordNanoseconds(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds)
@@ -574,16 +574,12 @@ public class ComponentLifecycle: LifecycleTask {
574574
errors = [:]
575575
}
576576
errors![tasks[index].label] = error
577-
self.log(level: .error, "failed to stop [\(tasks[index].label)]: \(error)")
577+
self.logger.error("failed to stop [\(tasks[index].label)]: \(error)")
578578
}
579579
self.shutdownTask(on: queue, tasks: tasks, index: index + 1, errors: errors, callback: callback)
580580
}
581581
}
582582

583-
internal func log(level: Logger.Level = .info, _ message: String) {
584-
self.logger.log(level: level, "[\(self.label)] \(message)")
585-
}
586-
587583
private enum State {
588584
case idle([LifecycleTask])
589585
case starting(DispatchQueue)

0 commit comments

Comments
 (0)