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
DefaultLogReporter
protocol:Extending the
DefaultLogReporter
protocol 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 theLogReporter
protocol and it’sDefaultLoggerType
associated 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
LogReceiver
protocol:(The
onReceive
method 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
format
method:Composing loggers
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:
Filtering loggers
You can filter loggers by adding a
LogFilter
to theLogFilters.global
cache. The following example excludes all instances of aLogger
based class calledMyLogger
:NOTE:
included
andexcluded
are mutually exclusive when usingClassBasedLogFilter
. If both contain types,included
will be used andexcluded
will be ignored