OSLog
extension OSLog: Logger
OSLogType Log Level Heirarchy:
- Debug - Use this level to capture information that may be useful during development or while troubleshooting a specific problem. Not for Release.
- Info - Use this level to capture information that may be helpful, but isn’t essential, for troubleshooting errors.
- Default - Use this level to capture information about things that might result in a failure. Not for Release.
- Error - Use this log level to capture process-level information to report errors in the process.
- Fault - Use this level to capture system-level or multi-process information to report system errors.
Each level includes the levels below itself, so setting ‘debug’ will include all logs, while ‘default’ would only include itself, error and fault levels
Usage
We suggest you to create a OSLog instance providing subsytem and category, as per apple naming convention.
static let logger = OSLog(subsystem: "com.example.logger", category: "HTTPClient")
It is advised to provide subsystem value in reverse domain format. Easy way would be to get the bundle identifier
of the framework or app in use. The category value must be a valid and thoughtful name of the module that is being logged.
The logs can be viewed outside Xcode using Console.app and these values are used for filtering logs messages; makes work
easier for devloper.
Implementation details
This extension on OSLog provides useful wrapper inline functions for each OSLogLevel levels in two flavours:
- Log function accepting
StaticStringand VariadicCVarArg. - Log function accepting
Anytyped value.
logger.logDebug(...)
logger.logInfo(...)
logger.log(...)
logger.logError(...)
logger.logFault(...)
It is advised to make your custom complex types conform to CustomDebugStringConvertible and CustomStringConvertible and
make use of the wrapper functions passing your complex type.
Privacy
The unified logging system considers dynamic strings and complex dynamic objects to be private, and does not collect them
automatically. Considering this, the messages needs to be marked with access modifiers. The wrapper functions accepting
Any type value as input with this extension marks log message arguments as public with DEBUG and private with RELEASE
configurations. It is up to the developer to mark arguments as public/private when using the other log function.
Please beware of what you log, you don’t wanna accidently expose confidential data i.e., secret key or anything of that sort.
Customize Log Output
If you wanna provide your own custom formatting while logging your custom type, extend it to
conform to CustomOSLogMessageConvertible.
-
Logs a developer-formatted message using the
.debugtype.The caller is responsible for including public/private formatting in the format string, as well as any source location info (line number, etc.).
Declaration
Swift
@inlinable public func logDebug(format: StaticString, args: CVarArg...)Parameters
formatA C-style format string.
argsA list of arguments to the format string (if any).
-
Logs a message using the
.defaulttype.Declaration
Swift
@inlinable public func logDebug(_ value: Any, file: String = #file, function: String = #function, line: UInt = #line)Parameters
valueThe value to be logged. If the value does not already conform to CustomLogRepresentable, a default implementation will used.
-
Logs the source location of the call site using the
.debugtype.Declaration
Swift
@inlinable public func trace(file: String = #file, function: String = #function, line: UInt = #line)
-
Logs a developer-formatted message using the
.infotype.The caller is responsible for including public/private formatting in the format string, as well as any source location info (line number, etc.).
Declaration
Swift
@inlinable public func logInfo(format: StaticString, args: CVarArg...)Parameters
formatA C-style format string.
argsA list of arguments to the format string (if any).
-
Logs a message using the
.infotype.Declaration
Swift
@inlinable public func logInfo(_ value: Any, file: String = #file, function: String = #function, line: UInt = #line)Parameters
valueThe value to be logged. If the value does not already conform to CustomLogRepresentable, a default implementation will used.
-
Logs a developer-formatted message using the
.defaulttype.The caller is responsible for including public/private formatting in the format string, as well as any source location info (line number, etc.).
Declaration
Swift
@inlinable public func log(format: StaticString, args: CVarArg...)Parameters
formatA C-style format string.
argsA list of arguments to the format string (if any).
-
Logs a message using the
.defaulttype.Declaration
Swift
@inlinable public func log(_ value: Any, file: String = #file, function: String = #function, line: UInt = #line)Parameters
valueThe value to be logged. If the value does not already conform to CustomLogRepresentable, a default implementation will used.
-
Logs a developer-formatted message using the
.errortype.The caller is responsible for including public/private formatting in the format string, as well as any source location info (line number, etc.).
Declaration
Swift
@inlinable public func logError(format: StaticString, args: CVarArg...)Parameters
formatA C-style format string.
argsA list of arguments to the format string (if any).
-
Logs a message using the
.errortype.Declaration
Swift
@inlinable public func logError(_ value: Any, file: String = #file, function: String = #function, line: UInt = #line)Parameters
valueThe value to be logged. If the value does not already conform to CustomLogRepresentable, a default implementation will used.
-
Logs a developer-formatted message using the
.faulttype.The caller is responsible for including public/private formatting in the format string, as well as any source location info (line number, etc.).
Declaration
Swift
@inlinable public func logFault(format: StaticString, args: CVarArg...)Parameters
formatA C-style format string.
argsA list of arguments to the format string (if any).
-
Logs a message using the
.faulttype.Declaration
Swift
@inlinable public func logFault(_ value: Any, file: String = #file, function: String = #function, line: UInt = #line)Parameters
valueThe value to be logged. If the value does not already conform to CustomLogRepresentable, a default implementation will used.