A log reporting framework written in Swift

What's New



Upgrade BlueSocket to 2.x


Build Status codecov

Cosmic is a log reporting framework written in Swift.


Cosmic provides a simple interface to rich logging functionality, including:

  • Pre-configured loggers for many use-cases
  • HTTP and Socket-based loggers for a number of services including, Loggly and Papertrail.
  • Formatters for various logging standards including Syslog and JSON.
  • Filtering of logs by class or module



pod 'Cosmic', '~> x.y.z'


.Package(url: "", majorVersion: <MAJORVERSION>)


The simplest way to support a logger in your class is to extend the DefaultLogReporter protocol:

import Cosmic

class MyClass: DefaultLogReporter {
	// ...

Extending the DefaultLogReporter protocol adds a logger property to your class that can be called to report log messages:

func logSomething() {
	// Debug level
	self.logger.debug("Logging something")
	// Info level"Logging something")
	// Warn level
	self.logger.warn("Logging something")
	// Error level
	self.logger.error("Logging something")

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:

class MyClass: LogReporter {

	typealias DefaultLoggerType: Logger = PrintLogger

	// ...

Alternatively, if you want to manage loggers yourself, you can simply instantiate them as needed:

let myLogger: Logger = PrintLogger()


You can create your own loggers by implementing the LogReceiver protocol:

	class MyLogger: LogReceiver {

		func onReceive(_ messages: [String], logLevel: LogLevel) {
			// Do something with the log


(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:

init() {

And you can format your messages using the format method:

func onReceive(_ messages: [String], logLevel: LogLevel) {
	messages.forEach { print(format($0)) }

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:

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 cache. The following example excludes all instances of a Logger based class called MyLogger:

let filter = ClassBasedLogFilter()
filter.excluded.append(MyLogger.self) filter)

NOTE: included and excluded are mutually exclusive when using ClassBasedLogFilter. If both contain types, included will be used and excluded will be ignored


  • Swift Tools 5.0.0
View More Packages from this Author


Last updated: Tue Jun 11 2024 23:30:26 GMT-0900 (Hawaii-Aleutian Daylight Time)