By default the logger provided by LogReporter will be an instance of PrintLogger - this can be changed by instead implementing the LogReporter protocol and it’s DefaultLoggerType associated type:
Cosmic provides CompositeLogger for more complex use cases. CompositeLogger routes logs to multiple loggers.
The log level of the composite logger is used transitively for all its component loggers.
The example below describes a logger that logs to console, file, and a TCP socket:
let printLogger = PrintLogger()
let memoryLogger = MemoryLogger()
let socketLogger = SocketLogger(...)
let logger = CompositeLogger(printLogger, fileLogger, socketLogger)
Filtering loggers
You can filter loggers by adding a LogFilter to the LogFilters.global cache. The following example excludes all instances of a Logger based class called MyLogger:
let filter = ClassBasedLogFilter()
filter.excluded.append(MyLogger.self)
LogFilters.global.addFilter(filter: filter)
NOTE: included and excluded are mutually exclusive when using ClassBasedLogFilter. If both contain types, included will be used
and excluded will be ignored
Cosmic
Cosmic is a log reporting framework written in Swift.
About
Cosmic provides a simple interface to rich logging functionality, including:
Installation
Cocoapods:
SPM:
Usage
The simplest way to support a logger in your class is to extend the
DefaultLogReporterprotocol:Extending the
DefaultLogReporterprotocol adds a logger property to your class that can be called to report log messages:By default the logger provided by LogReporter will be an instance of
PrintLogger- this can be changed by instead implementing theLogReporterprotocol and it’sDefaultLoggerTypeassociated type:Alternatively, if you want to manage loggers yourself, you can simply instantiate them as needed:
Extension
You can create your own loggers by implementing the
LogReceiverprotocol:(The
onReceivemethod will only be called for valid log levels so you don’t need to filter based on log level here.)You can add formatters in your initialiser:
And you can format your messages using the
formatmethod:Composing loggers
Cosmic provides
CompositeLoggerfor more complex use cases.CompositeLoggerroutes logs to multiple loggers.The log level of the composite logger is used transitively for all its component loggers.
The example below describes a logger that logs to console, file, and a TCP socket:
Filtering loggers
You can filter loggers by adding a
LogFilterto theLogFilters.globalcache. The following example excludes all instances of aLoggerbased class calledMyLogger:NOTE:
includedandexcludedare mutually exclusive when usingClassBasedLogFilter. If both contain types,includedwill be used andexcludedwill be ignored