Part 3: Installing Custom Log Manager

Welcome to Part 3: Installing Custom Log Manager

If you need check part 2 here you go.

First and foremost, what is SwiftyBeaver?

SwiftyBeaver is Swift-based logging framework for iOS and macOS. SwiftyBeaver has different types of log messages where also we can filter logs to make bug checking even easier. It has a free license plan.

That’s not all. Out log information is kept in cloud. SwiftyBeaver comes with a macOS App that lets us view our logs in real time.

The SwiftyBeaver Logging Framework can be installed on every Apple device and Swift 2 & Swift 3 using the installation tool of your choice.

  • For Swift 3 install the latest SwiftyBeaver version
  • For Swift 2 install SwiftyBeaver 0.7
In Part 3 we will configure SwiftyBeaver for our project.

Cloud Setup

After completed signup SwiftyBeaver Preferences menu, you will need to generate new credentials.

File > New SwiftyBeaver saves local log file. But I don`t have information on what is necessary. The location where you save is not important but don`t forget this location.

When you click on the Generate New App Credentials button you should see App ID, App Secret and Encryption Key for our app. Let’s define these information with AppDelegate.

After having installed SwiftyBeaver Logging Framework just add these 3 lines at the top of your AppDelegate.swift:

import SwiftyBeaver
let log = SwiftyBeaver.self

AppDelegate:didFinishLaunchingWithOptions() add the SwiftyBeaver log destinations (console, file, etc.) you want to use in your whole app:

// add log destinations. at least one is needed!
let console = ConsoleDestination() // log to Xcode Console
let file = FileDestination() // log to default swiftybeaver.log file
log.addDestination(console)
log.addDestination(file)

To start logging to the platform you just need to initiate SBPlatformDestination() with your credentials, optionally adjust the properties and then add the instance to SwiftyBeaver itself.

let platform = SBPlatformDestination(appID: "o8QXgd", appSecret: "dclfQ5mzuewrrdvighTxcqreb2arql0g", encryptionKey: "R3ocOuk6Sb3jxekwhnPr4j5d5Yq8Qdhr")
log.addDestination(platform)

👍 We made it!

Log Levels

Xcode console includes color, customizable formatting & so much more with SwiftyBeaver. But better than this: we can set different log levels.

// log with different importance
log.verbose("not so important") // prio 1, VERBOSE in silver
log.debug("something to debug") // prio 2, DEBUG in blue
log.info("a nice information") // prio 3, INFO in green
log.warning("oh no, that won’t be good") // prio 4, WARNING in yellow
log.error("ouch, an error did occur!") // prio 5, ERROR in red

// log strings, ints, dates, etc.
log.verbose(123)
log.info(-123.45678)
log.warning(NSDate())
log.error(["I", "like", "logs!"])
log.error(["name": "Mr Beaver", "address": "7 Beaver Lodge"])

I will show you more examples for each step with different levels. Before ending this article, we will start to use warning message for didReceiveMemoryWarning.

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
self.log.warning("MemoryWarning")
}
Now we have cloud logging function. CMD + R

Done!

Source code: https://github.com/durul/combat

Next Part 4: Design Pattern and MVC

That’s it. 😃😃😃 Thanks for reading.

If you want to follow me in social media, here are some links: github, Twitter, Linkedin

You can check my previous articles here.