Skip to content

Commit

Permalink
refactor(logging): Logging logic (#248)
Browse files Browse the repository at this point in the history
Simplifies the logging functionality in the library.
- Each target has an internal Log structure with its own instance of Logger
- Logs is a public structure exposing the static logSeverityLevel property to a library user. It emits the notification with log severity level to notifiy all internal Log structures.
  • Loading branch information
VladislavFitz committed Aug 25, 2022
1 parent bb37ec0 commit 25d5e3e
Show file tree
Hide file tree
Showing 39 changed files with 4,371 additions and 4,343 deletions.
7,524 changes: 3,758 additions & 3,766 deletions InstantSearch.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ If you build a SwiftUI application, please check out the Getting [Started with S

If you only require business logic modules in your project and use `InstantSearchCore` framework, add `import InstantSearchCore` to your source files.

## Logs

There are 7 levels of logs severity produced by the library.
The default severity level is `.info`.
You can configure the logging level as follows:
```
Logs.logSeverityLevel = .debug
```


## Telemetry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class HitsCollectionViewDataSource<DataSource: HitsSource>: NSObject, UICol
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return 0
}

Expand All @@ -37,7 +37,7 @@ open class HitsCollectionViewDataSource<DataSource: HitsSource>: NSObject, UICol
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return templateCellProvider()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class HitsCollectionViewDelegate<DataSource: HitsSource>: NSObject, UIColle
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class HitsTableViewDataSource<DataSource: HitsSource>: NSObject, UITableVie
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return 0
}

Expand All @@ -37,7 +37,7 @@ open class HitsTableViewDataSource<DataSource: HitsSource>: NSObject, UITableVie
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return .init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class HitsTableViewDelegate<DataSource: HitsSource>: NSObject, UITableViewD
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return
}

Expand Down
44 changes: 44 additions & 0 deletions Sources/InstantSearch/Logging/InstantSearchLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// InstantSearchLog.swift
// InstantSearch
//
// Created by Vladislav Fitc on 31/01/2020.
//

import Foundation
import Logging
#if !InstantSearchCocoaPods
import struct InstantSearchInsights.Logs
import protocol InstantSearchInsights.LogCollectable
#endif

struct InstantSearchLog: LogCollectable {

static var logger: Logging.Logger = {
NotificationCenter.default.addObserver(forName: Logs.logLevelChangeNotficationName, object: nil, queue: .main) { notification in
if let logLevel = notification.userInfo?["logLevel"] as? LogLevel {
InstantSearchLog.logger.logLevel = logLevel.swiftLogLevel
}
}
var logger = Logging.Logger(label: "InstantSearch")
logger.logLevel = Logs.logSeverityLevel.swiftLogLevel
return logger
}()

static func missingHitsSourceWarning() {
warning("Missing hits source")
}

static func missingCellConfiguratorWarning(forSection section: Int) {
logger.warning("No cell configurator found for section \(section)")
}

static func missingClickHandlerWarning(forSection section: Int) {
logger.warning("No click handler found for section \(section)")
}

static func error(_ error: Error) {
logger.error("\(error)")
}

}
70 changes: 0 additions & 70 deletions Sources/InstantSearch/Logging/Logger+InstantSearch.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class MultiIndexHitsCollectionViewDataSource: NSObject, UICollectionViewDat
cellConfigurators[section] = { [weak self] (collectionView, row) in

guard let hitsSource = self?.hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return .init()
}

Expand All @@ -46,29 +46,29 @@ open class MultiIndexHitsCollectionViewDataSource: NSObject, UICollectionViewDat

open func numberOfSections(in collectionView: UICollectionView) -> Int {
guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return 0
}
return hitsSource.numberOfSections()
}

open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return 0
}
return hitsSource.numberOfHits(inSection: section)
}

open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cellConfigurator = cellConfigurators[indexPath.section] else {
InstantSearchLogger.missingCellConfiguratorWarning(forSection: indexPath.section)
InstantSearchLog.missingCellConfiguratorWarning(forSection: indexPath.section)
return .init()
}
do {
return try cellConfigurator(collectionView, indexPath.row)
} catch let underlyingError {
InstantSearchLogger.error(underlyingError)
InstantSearchLog.error(underlyingError)
return .init()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class MultiIndexHitsCollectionViewDelegate: NSObject, UICollectionViewDeleg
guard let delegate = self else { return }

guard let hitsSource = delegate.hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return
}

Expand All @@ -46,13 +46,13 @@ open class MultiIndexHitsCollectionViewDelegate: NSObject, UICollectionViewDeleg

open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let clickHandler = clickHandlers[indexPath.section] else {
InstantSearchLogger.missingClickHandlerWarning(forSection: indexPath.section)
InstantSearchLog.missingClickHandlerWarning(forSection: indexPath.section)
return
}
do {
try clickHandler(collectionView, indexPath.row)
} catch let underlyingError {
InstantSearchLogger.error(underlyingError)
InstantSearchLog.error(underlyingError)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class MultiIndexHitsTableViewDataSource: NSObject, UITableViewDataSource {
cellConfigurators[section] = { [weak self] (tableView, row) in

guard let hitsSource = self?.hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return .init()
}

Expand All @@ -46,29 +46,29 @@ open class MultiIndexHitsTableViewDataSource: NSObject, UITableViewDataSource {

open func numberOfSections(in tableView: UITableView) -> Int {
guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return 0
}
return hitsSource.numberOfSections()
}

open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let hitsSource = hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return 0
}
return hitsSource.numberOfHits(inSection: section)
}

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cellConfigurator = cellConfigurators[indexPath.section] else {
InstantSearchLogger.missingCellConfiguratorWarning(forSection: indexPath.section)
InstantSearchLog.missingCellConfiguratorWarning(forSection: indexPath.section)
return .init()
}
do {
return try cellConfigurator(tableView, indexPath.row)
} catch let underlyingError {
InstantSearchLogger.error(underlyingError)
InstantSearchLog.error(underlyingError)
return .init()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class MultiIndexHitsTableViewDelegate: NSObject, UITableViewDelegate {
guard let delegate = self else { return }

guard let hitsSource = delegate.hitsSource else {
InstantSearchLogger.missingHitsSourceWarning()
InstantSearchLog.missingHitsSourceWarning()
return
}

Expand All @@ -46,13 +46,13 @@ open class MultiIndexHitsTableViewDelegate: NSObject, UITableViewDelegate {

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let clickHandler = clickHandlers[indexPath.section] else {
InstantSearchLogger.missingClickHandlerWarning(forSection: indexPath.section)
InstantSearchLog.missingClickHandlerWarning(forSection: indexPath.section)
return
}
do {
try clickHandler(tableView, indexPath.row)
} catch let underlyingError {
InstantSearchLogger.error(underlyingError)
InstantSearchLog.error(underlyingError)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/InstantSearchCore/Hits/HitsInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ extension HitsInteractor: ResultUpdatable {
hitsInteractor.paginator.process(page)
hitsInteractor.onResultsUpdated.fire(searchResults)
} catch let error {
InstantSearchCoreLogger.HitsDecoding.failure(hitsInteractor: hitsInteractor, error: error)
InstantSearchCoreLog.HitsDecoding.failure(hitsInteractor: hitsInteractor, error: error)
hitsInteractor.onError.fire(error)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class InfiniteScrollingController: InfiniteScrollable {

/// Remove a page index from a pending set
public func notifyPending(pageIndex: Int) {
InstantSearchCoreLogger.trace("InfiniteScrolling: remove page from pending: \(pageIndex)")
InstantSearchCoreLog.trace("InfiniteScrolling: remove page from pending: \(pageIndex)")
pendingPageIndexes.remove(pageIndex)
}

/// Remove all pages from a pending set
public func notifyPendingAll() {
InstantSearchCoreLogger.trace("InfiniteScrolling: remove all pages from pending")
InstantSearchCoreLog.trace("InfiniteScrolling: remove all pages from pending")
pendingPageIndexes.removeAll()
}

Expand Down Expand Up @@ -62,7 +62,7 @@ class InfiniteScrollingController: InfiniteScrollable {

let pagesToLoad = previousPagesToLoad.union(nextPagesToLoad)

InstantSearchCoreLogger.trace("InfiniteScrolling: required rows: \(currentRow)±\(offset), pages to load: \(pagesToLoad.sorted())")
InstantSearchCoreLog.trace("InfiniteScrolling: required rows: \(currentRow)±\(offset), pages to load: \(pagesToLoad.sorted())")

for pageIndex in pagesToLoad {
pendingPageIndexes.insert(pageIndex)
Expand Down
Loading

0 comments on commit 25d5e3e

Please sign in to comment.