Files
teleport/mobile/TeleportKit/Sources/LogBackends/Rotating File/RotatingFileLogHandler.swift
T
Gustavo Medori 6d906dc57b Migrating PR from gravitational/teleport repo (#232)
Export-Source-Commit: ead22275e58458e55a2316e849ff5fed138eea27
2026-08-25 11:35:10 -05:00

51 lines
1.5 KiB
Swift

// Teleport
// Copyright (C) 2026 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
import Dependencies
import Foundation
import Logging
/// A log handler that writes to a rotating file on disk.
public struct RotatingFileLogHandler: LogHandler {
public let label: String
@Dependency(\.date.now)
private var now
private let writer: RotatingFileWriter
public init(label: String, writer: RotatingFileWriter) {
self.label = label
self.writer = writer
}
// MARK: LogHandler Conformance
public var logLevel: Logger.Level = .info
public var metadata: Logger.Metadata = [:]
public subscript(metadataKey key: String) -> Logger.Metadata.Value? {
get { metadata[key] }
set { metadata[key] = newValue }
}
public func log(event: LogEvent) {
let logMessage = LogFormatter.format(label: label, event: event, handlerMetadata: metadata, timestamp: now)
writer.enqueue(logMessage: "\(logMessage)\n")
}
}