Skip to content
A log reporting framework written in Swift
Swift Ruby Objective-C
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
Cosmic.xcodeproj Feature/jn/swift 5.0 (#8) Apr 5, 2019
Sources/Cosmic Add log level field to formatters May 10, 2019
.gitignore Add .DS_Store to .gitignore Apr 12, 2018
.travis.yml Feature/jn/swift 5.0 (#8) Apr 5, 2019
Cosmic.podspec Bump podspec to 6.0.0 May 10, 2019
LICENSE Add Apache 2.0 license May 10, 2019
Package.pins Add Package.pins Jul 12, 2017
Package.resolved Update BlueSocket to recent version (1.0.x) Feb 3, 2019
Package.swift Allow automatic selection of library type instead of forcing dynamic … Jun 9, 2019
Podfile Feature/jn/swift 5.0 (#8) Apr 5, 2019
Podfile.lock Feature/jn/swift 5.0 (#8) Apr 5, 2019 Add code coverage badge Jul 25, 2017
codecov.yml Feature/jn/swift 5.0 (#8) Apr 5, 2019


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

You can’t perform that action at this time.