Skip to content

Logging with Moa

Evgenii Neumerzhitckii edited this page Jun 26, 2016 · 4 revisions

Sometimes it is useful to see when images are loaded or to find out why they aren't. If you assign a closure to Moa.logger property it will be called during these events:

  • HTTP request is sent.
  • Image is successfully received.
  • Error occurred.
  • Request is canceled.
Moa.logger = { logType, url, statusCode, error in
  switch logType {
  case .RequestSent: print("GET \(url)")
  case .RequestCancelled: print("Cancelled \(url)")
  case .ResponseSuccess: print("Success \(url)")
  case .ResponseError:
    let errorDescription = error?.localizedDescription ?? ""
    print("Error \(url) \(errorDescription)")
  } 
}

This logging closure receives four parameters:

  1. logType: A type of the log event. Type: MoaLogType. Possible values: .RequestSent, .RequestCancelled, .ResponseSuccess, .ResponseError.
  2. url: URL of the image request. Type: String.
  3. statusCode: An HTTP status code where applicable: 200, 404 etc. Type: Int.
  4. error: Contains an error object. This parameter is only present in .ResponseError logs. Type: NSError. You can use this parameter to get human readable error description by accessing error.localizedDescription property.

Logging to console

If you need to quickly log moa activity to console you can use this pre-made function MoaConsoleLogger.

// Log to console
Moa.logger = MoaConsoleLogger

// Load an image
imageView.moa.url = "https://bit.ly/moa_image"

// Attempt to load a missing image
imageView.moa.url = "https://bit.ly/moa_image_missing.jpg"
Logging to console with moa