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 arbitraryAnyvalue:
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
LogChannelthat can be used to perform logging at the.errorlog severity level. Will benilif logging hasn’t yet been enabled, or if logging for the.errorseverity has not been configured.Declaration
Swift
public private(set) static var error: LogChannel? -
The
LogChannelthat can be used to perform logging at the.warninglog severity level. Will benilif logging hasn’t yet been enabled, or if logging for the.warningseverity has not been configured.Declaration
Swift
public private(set) static var warning: LogChannel? -
The
LogChannelthat can be used to perform logging at the.infolog severity level. Will benilif logging hasn’t yet been enabled, or if logging for the.infoseverity has not been configured.Declaration
Swift
public private(set) static var info: LogChannel? -
The
LogChannelthat can be used to perform logging at the.debuglog severity level. Will benilif logging hasn’t yet been enabled, or if logging for the.debugseverity has not been configured.Declaration
Swift
public private(set) static var debug: LogChannel? -
The
LogChannelthat can be used to perform logging at the.verboselog severity level. Will benilif logging hasn’t yet been enabled, or if logging for the.verboseseverity 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
stderroutput stream of the running process. In Xcode, log entries will appear in the console.Warning
Setting either
debugModeorverboseDebugModetotruewill result insynchronousModebeing 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
minimumSeverityThe minimum supported
LogSeverity. AnyLogEntryhaving aseverityless thanminimumSeveritywill be silently ignored.debugModeIf
true, the value ofminimumSeveritywill be lowered (if necessary) to.debugandsynchronousModewill be used when recording log entries.verboseDebugModeIf
true, the value ofminimumSeveritywill be lowered (if necessary) to.verboseandsynchronousModewill be used when recording log entries.stdStreamsModeA
StandardStreamsModevalue that governs when standard console streams (i.e.,stdoutandstderr) should be used for recording log output.mimicOSLogOutputIf
true, any output sent tostdoutwill be formatted in such a way as to mimic the output seen whenos_log()is used.showCallSiteIf
true, the source file and line indicating the call site of the log request will be added to formatted log messages.filtersThe
LogFilters to use when deciding whether a givenLogEntryshould be passed along for recording. -
Enables logging using the specified
LogConfiguration.Declaration
Swift
public static func enable(configuration: LogConfiguration)Parameters
configurationThe
LogConfigurationto use for controlling the behavior of logging. -
Enables logging using the specified
LogConfigurations.Declaration
Swift
public static func enable(configuration: [LogConfiguration])Parameters
configurationAn array of
LogConfigurations specifying the behavior of logging. -
Enables logging using the specified
LogReceptacle.Individual
LogChannels forerror,warning,info,debug, andverbosemay or may not be constructed depending on the receptacle’sminimumSeverity.Declaration
Swift
public static func enable(receptacle: LogReceptacle)Parameters
receptacleThe
LogReceptacleto use when creating theLogChannels for the five severity levels. -
Enables logging using the specified
LogChannels.The static
error,warning,info,debug, andverboseproperties ofLogwill be set using the specified values.If you know that the configuration of a given
LogChannelguarantees that it will never perform logging, it is best to passnilinstead. 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
errorChannelThe
LogChannelto use for logging messages with aseverityof.error.warningChannelThe
LogChannelto use for logging messages with aseverityof.warning.infoChannelThe
LogChannelto use for logging messages with aseverityof.info.debugChannelThe
LogChannelto use for logging messages with aseverityof.debug.verboseChannelThe
LogChannelto use for logging messages with aseverityof.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
LogChannelresponsible for logging at the given severity.Declaration
Swift
public static func channel(severity: LogSeverity) -> LogChannel?Parameters
severityThe
LogSeveritylevel of theLogChannelto return.Return Value
The
LogChannelused byLogto perform logging at the given severity; will benilifLogis 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
severityThe
LogSeverityfor the message being recorded.functionThe default value provided for this parameter captures the signature of the calling function. You should not provide a value for this parameter.
filePathThe 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.
fileLineThe 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
severityThe
LogSeverityfor the log entry.msgThe message to log.
functionThe default value provided for this parameter captures the signature of the calling function. You should not provide a value for this parameter.
filePathThe 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.
fileLineThe 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
severityThe
LogSeverityfor the log entry.valueThe value to send to the log. Determining how (and whether) arbitrary values are captured and represented will be handled by the
LogRecorderimplementation(s) that are ultimately called upon to record the log entry.functionThe default value provided for this parameter captures the signature of the calling function. You should not provide a value for this parameter.
filePathThe 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.
fileLineThe 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.
View on GitHub
Log Struct Reference