iOS Logger Kit

Karthik
2 min readDec 10, 2021

--

The article is about a customized logging package.

We are into migrating our monolith project into modules. where we wanted to build a logger package that can be given to other iOS teams for their consumption. enforcing every other team to use the same logger can make the console clean once after integrating various modules.

Let’s start with an interface

Logger Interface

An interface can help us to get the necessary inputs from the consumer once they opted to use the logger kit.

  • LogTag refers to the unique reference of the module/pod/package.
  • enable a bool that can help in toggling the logger.
  • Severity filters the logs which need to be printed in the console.
  • LogWriter & LogFormatter can help in formatting the logs.
  • debug, info, warning, error function helps in logging with proper priority.

Logger Severity

Severity helps in filtering the logs which the consumer wants to see in the console. The list of severity is shown here.

Verbose shows all the logs by default. The type debug can show the logs from debug and above priorities. Error type writes only the error since there are no other higher priorities than that.

Logger Writers

Not everyone wants to write their logs in the console. some may want to write in a file or push it to the server etc. They can make use of the logWriter interface to create their destination.

Logger Formatter

The format helps in customizing the log which gets printed in the console. we have created our default format as shown below.

Format: [Severity] [LogTag] [File] [line]: —  Log String Payload. OutPut:  💙💙 [LoggerKitExample]: ViewController.swift: 18  — viewController loaded 

Consumers can create their own formate by adopting the LogFormatter protocol

Wrapping it up!

Finally, consumers can simply adopt the LoggerInterface to make use of the logger functionality as shown below. Logger typealias removes the clutter and provides the Logger point to initiate the log.

I hope you enjoyed this article 😀. Please feel free to share, comment, and give some claps 👏👏. Stay tuned at this pace for more updates. Thanks for reading this.

--

--

Karthik
Karthik

No responses yet