-
Notifications
You must be signed in to change notification settings - Fork 42
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:
-
logType: A type of the log event. Type:
MoaLogType
. Possible values:.RequestSent
,.RequestCancelled
,.ResponseSuccess
,.ResponseError
. -
url: URL of the image request. Type:
String
. -
statusCode: An HTTP status code where applicable: 200, 404 etc. Type:
Int
. -
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 accessingerror.localizedDescription
property.
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](https://raw.githubusercontent.com/evgenyneu/moa/master/Graphics/screenshots/logging_to_console_moa_swift_2.png)