Skip to main content

Logger

Entity used for logging messages.

public class Logger 

Initializers

init(identifier:destinations:)

Init a logger with a given identifier and destinations.

public init(identifier: String = "", destinations: [LogDestination] = []) 

Properties

identifier

Identifier of the Logger. Will be visible if a destination has showIdentifiers enabled.

public let identifier: String

destinations

Destinations for this logger. See LogDestination protocol for details.

public var destinations: [LogDestination]

Methods

callAsFunction(_:functionName:fileName:lineNumber:message:)

Allows logger to be called as function. Transforms, given that let log = Logger(), log.log(.info, "Hello") to log(.info, "Hello") for ease of use.

public func callAsFunction(
_ level: LogLevel,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line,
message: @autoclosure () -> Any
)

Parameters

  • level: Log level for this message
  • functionName: Function of the caller
  • fileName: File of the caller
  • lineNumber: Line number of the caller
  • message: Message to be logged

log(_:functionName:fileName:lineNumber:message:)

Log a message to all enabled destinations. See Logger.destinations for customizing the output.

public func log(
_ level: LogLevel,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line,
message: @autoclosure () -> Any
)

Parameters

  • level: Log level for this message
  • functionName: Function of the caller
  • fileName: File of the caller
  • lineNumber: Line number of the caller
  • message: Message to be logged

info(_:functionName:fileName:lineNumber:)

Log an info message.

public func info(
_ message: @autoclosure () -> Any,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line
)

Parameters

  • message: Message to be logged
  • functionName: Function of the caller
  • fileName: File of the caller
  • lineNumber: Line number of the caller

debug(_:functionName:fileName:lineNumber:)

Log a debug message.

public func debug(
_ message: @autoclosure () -> Any,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line
)

Parameters

  • message: Message to be logged
  • functionName: Function of the caller
  • fileName: File of the caller
  • lineNumber: Line number of the caller

warning(_:functionName:fileName:lineNumber:)

Log a warning message.

public func warning(
_ message: @autoclosure () -> Any,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line
)

Parameters

  • message: Message to be logged
  • functionName: Function of the caller
  • fileName: File of the caller
  • lineNumber: Line number of the caller

error(_:functionName:fileName:lineNumber:)

Log an error message.

public func error(
_ message: @autoclosure () -> Any,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line
)

Parameters

  • message: Message to be logged
  • functionName: Function of the caller
  • fileName: File of the caller
  • lineNumber: Line number of the caller

assert(_:_:functionName:fileName:lineNumber:)

Performs Swift.assert and stops program execution if condition evaluated to false. In RELEASE builds only logs the failure.

public func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> Any,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line
)

Parameters

  • condition: The condition to test.
  • message: A custom message to log if condition is evaluated to false.

assertionFailure(_:functionName:fileName:lineNumber:)

Stops program execution with Swift.assertionFailure. In RELEASE builds only logs the failure.

public func assertionFailure(
_ message: @autoclosure () -> Any,
functionName: StaticString = #function,
fileName: StaticString = #file,
lineNumber: UInt = #line
)

Parameters

  • message: A custom message to log if condition is evaluated to false.

Did you find this page helpful?