Log
public struct Log
Log
is the primary public API for CleanroomLogger.
If you wish to send a message to the log, you do so by calling the appropriae
function provided by the appropriate LogChannel
given the importance of your
message.
There are five levels of severity at which log messages can be recorded. Each
level is represented by a read-only static variable maintained by the Log
:
Log.error
— The highest severity; something has gone wrong and a fatal error may be imminentLog.warning
— Something appears amiss and might bear looking into before a larger problem arisesLog.info
— Something notable happened, but it isn’t anything to worry aboutLog.debug
— Used for debugging and diagnostic informationLog.verbose
- The lowest severity; used for detailed or frequently occurring debugging and diagnostic information
Each LogChannel
can be used in one of three ways:
- The
trace()
function records a short log message detailing the source file, source line, and function name of the caller. It is intended to be called with no arguments, as follows:
Log.debug?.trace()
- The
message()
function records a message specified by the caller:
Log.info?.message("The application has finished launching.")
message()
is intended to be called with a single parameter, the message
string, as shown above. Unlike NSLog()
, no printf
-like functionality
is provided; instead, use Swift string interpolation to construct parameterized
messages.
- Finally, the
value()
function records a string representation of an arbitraryAny
value:
Log.verbose?.value(delegate)
The value()
function is intended to be called with a single parameter, of
type Any?
.
The underlying logging implementation is responsible for converting this value into a string representation.
Note that some implementations may not be able to convert certain values into strings; in those cases, log requests may be silently ignored.
Enabling logging
By default, logging is disabled, meaning that none of the Log
‘s log channels
have been populated. As a result, attempts to perform any logging will silently
fail.
It is the responsibility of the application developer to enable logging, which
is done by calling the appropriate Log.enable()
function.
-
The
LogChannel
that can be used to perform logging at the.error
log severity level. Will benil
if logging hasn’t yet been enabled, or if logging for the.error
severity has not been configured.Declaration
Swift
public private(set) static var error: LogChannel?
-
The
LogChannel
that can be used to perform logging at the.warning
log severity level. Will benil
if logging hasn’t yet been enabled, or if logging for the.warning
severity has not been configured.Declaration
Swift
public private(set) static var warning: LogChannel?
-
The
LogChannel
that can be used to perform logging at the.info
log severity level. Will benil
if logging hasn’t yet been enabled, or if logging for the.info
severity has not been configured.Declaration
Swift
public private(set) static var info: LogChannel?
-
The
LogChannel
that can be used to perform logging at the.debug
log severity level. Will benil
if logging hasn’t yet been enabled, or if logging for the.debug
severity has not been configured.Declaration
Swift
public private(set) static var debug: LogChannel?
-
The
LogChannel
that can be used to perform logging at the.verbose
log severity level. Will benil
if logging hasn’t yet been enabled, or if logging for the.verbose
severity has not been configured.Declaration
Swift
public private(set) static var verbose: LogChannel?
-
enable(minimumSeverity:debugMode:verboseDebugMode:stdStreamsMode:mimicOSLogOutput:showCallSite:filters:)
Enables logging using an
XcodeLogConfiguration
.Log entries are recorded by being written to the Apple System Log and to the
stderr
output stream of the running process. In Xcode, log entries will appear in the console.Warning
Setting either
debugMode
orverboseDebugMode
totrue
will result insynchronousMode
being used when recording log entries. Synchronous mode is helpful while debugging, as it ensures that logs are always up-to-date when debug breakpoints are hit. However, synchronous mode can have a negative influence on performance and is therefore not recommended for use in production code.Declaration
Swift
public static func enable(minimumSeverity: LogSeverity = .info, debugMode: Bool = false, verboseDebugMode: Bool = false, stdStreamsMode: ConsoleLogConfiguration.StandardStreamsMode = .useAsFallback, mimicOSLogOutput: Bool = true, showCallSite: Bool = true, filters: [LogFilter] = [])
Parameters
minimumSeverity
The minimum supported
LogSeverity
. AnyLogEntry
having aseverity
less thanminimumSeverity
will be silently ignored.debugMode
If
true
, the value ofminimumSeverity
will be lowered (if necessary) to.debug
andsynchronousMode
will be used when recording log entries.verboseDebugMode
If
true
, the value ofminimumSeverity
will be lowered (if necessary) to.verbose
andsynchronousMode
will be used when recording log entries.stdStreamsMode
A
StandardStreamsMode
value that governs when standard console streams (i.e.,stdout
andstderr
) should be used for recording log output.mimicOSLogOutput
If
true
, any output sent tostdout
will be formatted in such a way as to mimic the output seen whenos_log()
is used.showCallSite
If
true
, the source file and line indicating the call site of the log request will be added to formatted log messages.filters
The
LogFilter
s to use when deciding whether a givenLogEntry
should be passed along for recording. -
Enables logging using the specified
LogConfiguration
.Declaration
Swift
public static func enable(configuration: LogConfiguration)
Parameters
configuration
The
LogConfiguration
to use for controlling the behavior of logging. -
Enables logging using the specified
LogConfiguration
s.Declaration
Swift
public static func enable(configuration: [LogConfiguration])
Parameters
configuration
An array of
LogConfiguration
s specifying the behavior of logging. -
Enables logging using the specified
LogReceptacle
.Individual
LogChannel
s forerror
,warning
,info
,debug
, andverbose
may or may not be constructed depending on the receptacle’sminimumSeverity
.Declaration
Swift
public static func enable(receptacle: LogReceptacle)
Parameters
receptacle
The
LogReceptacle
to use when creating theLogChannel
s for the five severity levels. -
Enables logging using the specified
LogChannel
s.The static
error
,warning
,info
,debug
, andverbose
properties ofLog
will be set using the specified values.If you know that the configuration of a given
LogChannel
guarantees that it will never perform logging, it is best to passnil
instead. Otherwise, needless overhead will be added to the application.Declaration
Swift
public static func enable(errorChannel: LogChannel?, warningChannel: LogChannel?, infoChannel: LogChannel?, debugChannel: LogChannel?, verboseChannel: LogChannel?)
Parameters
errorChannel
The
LogChannel
to use for logging messages with aseverity
of.error
.warningChannel
The
LogChannel
to use for logging messages with aseverity
of.warning
.infoChannel
The
LogChannel
to use for logging messages with aseverity
of.info
.debugChannel
The
LogChannel
to use for logging messages with aseverity
of.debug
.verboseChannel
The
LogChannel
to use for logging messages with aseverity
of.verbose
. -
Assuming CleanroomLogger has not yet been enabled, calling this function prevents any other caller from enabling CleanroomLogger for the remainder of the lifetime of the running executable.
The ability to prevent CleanroomLogger from being enabled may be useful in applications that link against libraries requiring CleanroomLogger. Application developers who did not choose to use CleanroomLogger can ensure that embedded third-party libraries don’t use it, either.
Important
IfLog.enable()
has already been called, callingLog.neverEnable()
will have no effect.Declaration
Swift
public static func neverEnable()
-
Returns the
LogChannel
responsible for logging at the given severity.Declaration
Swift
public static func channel(severity: LogSeverity) -> LogChannel?
Parameters
severity
The
LogSeverity
level of theLogChannel
to return.Return Value
The
LogChannel
used byLog
to perform logging at the given severity; will benil
ifLog
is not configured to perform logging at that severity. -
Sends program execution trace information to the log using the specified severity. This information includes source-level call site information as well as the stack frame signature of the caller.
Declaration
Swift
public static func trace(_ severity: LogSeverity, function: String = #function, filePath: String = #file, fileLine: Int = #line)
Parameters
severity
The
LogSeverity
for the message being recorded.function
The default value provided for this parameter captures the signature of the calling function. You should not provide a value for this parameter.
filePath
The default value provided for this parameter captures the file path of the code issuing the call to this function. You should not provide a value for this parameter.
fileLine
The default value provided for this parameter captures the line number issuing the call to this function. You should not provide a value for this parameter.
-
Sends a message string to the log using the specified severity.
Declaration
Swift
public static func message(_ severity: LogSeverity, message: String, function: String = #function, filePath: String = #file, fileLine: Int = #line)
Parameters
severity
The
LogSeverity
for the log entry.msg
The message to log.
function
The default value provided for this parameter captures the signature of the calling function. You should not provide a value for this parameter.
filePath
The default value provided for this parameter captures the file path of the code issuing the call to this function. You should not provide a value for this parameter.
fileLine
The default value provided for this parameter captures the line number issuing the call to this function. You should not provide a value for this parameter.
-
Sends an arbitrary value to the log using the specified severity.
Declaration
Swift
public static func value(_ severity: LogSeverity, value: Any?, function: String = #function, filePath: String = #file, fileLine: Int = #line)
Parameters
severity
The
LogSeverity
for the log entry.value
The value to send to the log. Determining how (and whether) arbitrary values are captured and represented will be handled by the
LogRecorder
implementation(s) that are ultimately called upon to record the log entry.function
The default value provided for this parameter captures the signature of the calling function. You should not provide a value for this parameter.
filePath
The default value provided for this parameter captures the file path of the code issuing the call to this function. You should not provide a value for this parameter.
fileLine
The default value provided for this parameter captures the line number issuing the call to this function. You should not provide a value for this parameter.