Improvements to logging (#457)

* Use `.info` logging in most places.
* Remove old objc logging support.
* Fix table view controller.
* Make sure timeline item content isn't logged.
* Add tests.
This commit is contained in:
Doug
2023-01-17 09:28:01 +00:00
committed by GitHub
parent 4b56e4af73
commit c8937505d7
57 changed files with 542 additions and 275 deletions
+11 -52
View File
@@ -18,25 +18,25 @@ import Foundation
import SwiftyBeaver
/// Various MXLog configuration options. Used in conjunction with `MXLog.configure()`
@objc public class MXLogConfiguration: NSObject {
public class MXLogConfiguration: NSObject {
/// the desired log level. `.verbose` by default.
@objc public var logLevel = MXLogLevel.verbose
public var logLevel = MXLogLevel.verbose
/// whether logs should be written directly to files. `false` by default.
@objc public var redirectLogsToFiles = false
public var redirectLogsToFiles = false
/// the maximum total space to use for log files in bytes. `100MB` by default.
@objc public var logFilesSizeLimit: UInt = 100 * 1024 * 1024 // 100MB
public var logFilesSizeLimit: UInt = 100 * 1024 * 1024 // 100MB
/// the maximum number of log files to use before rolling. `50` by default.
@objc public var maxLogFilesCount: UInt = 50
public var maxLogFilesCount: UInt = 50
/// the subname for log files. Files will be named as 'console-[subLogName].log'. `nil` by default
@objc public var subLogName: String?
public var subLogName: String?
}
/// MXLog logging levels. Use .none to disable logging entirely.
@objc public enum MXLogLevel: UInt {
public enum MXLogLevel: UInt {
case none
case verbose
case debug
@@ -56,13 +56,13 @@ private var logger: SwiftyBeaver.Type = {
Its purpose is to provide a common entry for customizing logging and should be used throughout the code.
Please see `MXLog.h` for Objective-C options.
*/
@objc public class MXLog: NSObject {
public class MXLog: NSObject {
/// Method used to customize MXLog's behavior.
/// Called automatically when first accessing the logger with the default values.
/// Please see `MXLogConfiguration` for all available options.
/// - Parameters:
/// - configuration: the `MXLogConfiguration` instance to use
@objc public static func configure(_ configuration: MXLogConfiguration) {
public static func configure(_ configuration: MXLogConfiguration) {
configureLogger(logger, withConfiguration: configuration)
}
@@ -74,11 +74,6 @@ private var logger: SwiftyBeaver.Type = {
logger.verbose(message(), file, function, line: line, context: context)
}
@available(swift, obsoleted: 5.4)
@objc public static func logVerbose(_ message: String, file: String, function: String, line: Int) {
logger.verbose(message, file, function, line: line)
}
public static func debug(_ message: @autoclosure () -> Any,
file: String = #file,
function: String = #function,
@@ -87,11 +82,6 @@ private var logger: SwiftyBeaver.Type = {
logger.debug(message(), file, function, line: line, context: context)
}
@available(swift, obsoleted: 5.4)
@objc public static func logDebug(_ message: String, file: String, function: String, line: Int) {
logger.debug(message, file, function, line: line)
}
public static func info(_ message: @autoclosure () -> Any,
file: String = #file,
function: String = #function,
@@ -100,11 +90,6 @@ private var logger: SwiftyBeaver.Type = {
logger.info(message(), file, function, line: line, context: context)
}
@available(swift, obsoleted: 5.4)
@objc public static func logInfo(_ message: String, file: String, function: String, line: Int) {
logger.info(message, file, function, line: line)
}
public static func warning(_ message: @autoclosure () -> Any,
file: String = #file,
function: String = #function,
@@ -113,31 +98,17 @@ private var logger: SwiftyBeaver.Type = {
logger.warning(message(), file, function, line: line, context: context)
}
@available(swift, obsoleted: 5.4)
@objc public static func logWarning(_ message: String, file: String, function: String, line: Int) {
logger.warning(message, file, function, line: line)
}
/// Log error with additional details
///
/// - Parameters:
/// - message: Description of the error without any variables (this is to improve error aggregations by type)
/// - context: Additional context-dependent details about the issue
public static func error(_ message: String,
public static func error(_ message: @autoclosure () -> Any,
file: String = #file,
function: String = #function,
line: Int = #line,
context: Any? = nil) {
logger.error(message, file, function, line: line, context: context)
}
@available(swift, obsoleted: 5.4)
@objc public static func logError(_ message: String,
file: String,
function: String,
line: Int,
context: Any? = nil) {
logger.error(message, file, function, line: line, context: context)
logger.error(message(), file, function, line: line, context: context)
}
/// Log failure with additional details
@@ -159,18 +130,6 @@ private var logger: SwiftyBeaver.Type = {
#endif
}
@available(swift, obsoleted: 5.4)
@objc public static func logFailure(_ message: String,
file: String,
function: String,
line: Int,
context: Any? = nil) {
logger.error(message, file, function, line: line, context: context)
#if DEBUG
assertionFailure("\(message)")
#endif
}
// MARK: - Private
fileprivate static func configureLogger(_ logger: SwiftyBeaver.Type, withConfiguration configuration: MXLogConfiguration) {
@@ -54,10 +54,10 @@ class MXLogger {
let nsLogURL = logURL(for: "console\(subLogName).log")
freopen((nsLogURL as NSURL).fileSystemRepresentation, "w+", stderr)
MXLog.debug("redirectNSLogToFiles: true")
MXLog.info("redirectNSLogToFiles: true")
if !tempLog.isEmpty {
// We can now log into files
MXLog.debug(tempLog)
MXLog.info(tempLog)
}
removeExtraFiles(from: numberOfFiles)
@@ -138,7 +138,7 @@ class MXLogger {
}
}
MXLog.debug("logFiles: \(logFiles)")
MXLog.info("logFiles: \(logFiles)")
return logFiles
}
@@ -282,7 +282,7 @@ class MXLogger {
if fileManager.fileExists(atPath: logFile.path()) {
try? fileManager.removeItem(at: logFile)
MXLog.debug("removeExtraFilesFromCount: \(count). removeItemAt: \(logFile)\n")
MXLog.info("removeExtraFilesFromCount: \(count). removeItemAt: \(logFile)\n")
} else {
break
}
@@ -318,10 +318,10 @@ class MXLogger {
let sizeLimitString = sizeLimit.formatted(.byteCount(style: .binary))
if let indexExceedingSizeLimit {
MXLog.debug("removeFilesAfterSizeLimit: Remove files from index \(indexExceedingSizeLimit) because logs are too large (\(logSizeString) for a limit of \(sizeLimitString)\n")
MXLog.info("removeFilesAfterSizeLimit: Remove files from index \(indexExceedingSizeLimit) because logs are too large (\(logSizeString) for a limit of \(sizeLimitString)\n")
removeExtraFiles(from: UInt(indexExceedingSizeLimit))
} else {
MXLog.debug("removeFilesAfterSizeLimit: No need: \(logSizeString) for a limit of \(sizeLimitString)\n")
MXLog.info("removeFilesAfterSizeLimit: No need: \(logSizeString) for a limit of \(sizeLimitString)\n")
}
}
}
@@ -36,13 +36,11 @@ struct TracingConfiguration {
])
enum Target: String {
case hyper, sled, matrix_sdk_sled, matrix_sdk_ffi
case hyper, sled, matrix_sdk_sled, matrix_sdk_ffi, matrix_sdk_crypto
case matrix_sdk_http_client = "matrix_sdk::http_client"
case matrix_sdk_ffi_uniffi_api = "matrix_sdk_ffi::uniffi_api"
case matrix_sdk_sliding_sync = "matrix_sdk::sliding_sync"
case matrix_sdk_base_sliding_sync = "matrix_sdk_base::sliding_sync"
case matrix_sdk_crypto
case matrix_sdk_crypto_sync = "matrix_sdk_crypto::machine[receive_sync_changes]"
}
enum LogLevel: String { case error, warn, info, debug, trace }
@@ -52,8 +50,7 @@ struct TracingConfiguration {
.hyper: .warn,
.sled: .warn,
.matrix_sdk_sled: .warn,
.matrix_sdk_crypto: .debug,
.matrix_sdk_crypto_sync: .trace
.matrix_sdk_crypto: .debug
]
var filter: String {