Skip to content

Remove [Lifecycle] prefix from log messages #95

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

Merged
merged 1 commit into from
May 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions Sources/Lifecycle/Lifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public struct ServiceLifecycle {
}

private func log(_ message: String) {
self.underlying.log(message)
self.underlying.logger.info("\(message)")
}
}

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

private var state = State.idle([])
Expand Down Expand Up @@ -435,7 +435,7 @@ public class ComponentLifecycle: LifecycleTask {
callback(nil)
case .shutdown:
self.stateLock.unlock()
self.log(level: .warning, "already shutdown")
self.logger.warning("already shutdown")
callback(nil)
case .starting(let queue):
self.state = .shuttingDown(queue)
Expand Down Expand Up @@ -476,11 +476,11 @@ public class ComponentLifecycle: LifecycleTask {
self.state = .starting(queue)
}

self.log("starting")
self.logger.info("starting")
Counter(label: "\(self.label).lifecycle.start").increment()

if tasks.count == 0 {
self.log(level: .notice, "no tasks provided")
self.logger.notice("no tasks provided")
}
self.startTask(on: queue, tasks: tasks, index: 0) { started, error in
self.stateLock.lock()
Expand Down Expand Up @@ -519,12 +519,12 @@ public class ComponentLifecycle: LifecycleTask {
if index >= tasks.count {
return callback(index, nil)
}
self.log("starting tasks [\(tasks[index].label)]")
self.logger.info("starting tasks [\(tasks[index].label)]")
let startTime = DispatchTime.now()
start { error in
Timer(label: "\(self.label).\(tasks[index].label).lifecycle.start").recordNanoseconds(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds)
if let error = error {
self.log(level: .error, "failed to start [\(tasks[index].label)]: \(error)")
self.logger.error("failed to start [\(tasks[index].label)]: \(error)")
return callback(index, error)
}
// shutdown called while starting
Expand All @@ -540,7 +540,7 @@ public class ComponentLifecycle: LifecycleTask {
self.state = .shuttingDown(queue)
}

self.log("shutting down")
self.logger.info("shutting down")
Counter(label: "\(self.label).lifecycle.shutdown").increment()

self.shutdownTask(on: queue, tasks: tasks.reversed(), index: 0, errors: nil) { errors in
Expand All @@ -550,7 +550,7 @@ public class ComponentLifecycle: LifecycleTask {
}
self.state = .shutdown(errors)
}
self.log("bye")
self.logger.info("bye")
callback()
}
}
Expand All @@ -564,7 +564,7 @@ public class ComponentLifecycle: LifecycleTask {
return callback(errors)
}

self.log("stopping tasks [\(tasks[index].label)]")
self.logger.info("stopping tasks [\(tasks[index].label)]")
let startTime = DispatchTime.now()
shutdown { error in
Timer(label: "\(self.label).\(tasks[index].label).lifecycle.shutdown").recordNanoseconds(DispatchTime.now().uptimeNanoseconds - startTime.uptimeNanoseconds)
Expand All @@ -574,16 +574,12 @@ public class ComponentLifecycle: LifecycleTask {
errors = [:]
}
errors![tasks[index].label] = error
self.log(level: .error, "failed to stop [\(tasks[index].label)]: \(error)")
self.logger.error("failed to stop [\(tasks[index].label)]: \(error)")
}
self.shutdownTask(on: queue, tasks: tasks, index: index + 1, errors: errors, callback: callback)
}
}

internal func log(level: Logger.Level = .info, _ message: String) {
self.logger.log(level: level, "[\(self.label)] \(message)")
}

private enum State {
case idle([LifecycleTask])
case starting(DispatchQueue)
Expand Down