forked from dclelland/AlamofireLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlamofireLogger.swift
91 lines (72 loc) · 2.83 KB
/
AlamofireLogger.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// AlamofireLogger.swift
// Ticketing
//
// Created by Daniel Clelland on 10/12/15.
// Copyright © 2015 Daniel Clelland. All rights reserved.
//
import Alamofire
// MARK: Logging
extension DataRequest {
/// The logging level. `.simple` prints only a brief request/response description; `.verbose` prints the request/response body as well.
public enum LogLevel {
/// Prints the request and response at their respective `.simple` levels.
case simple
/// Prints the request and response at their respective `.verbose` levels.
case verbose
}
/// Log the request and response at the specified `level`.
public func log(_ level: LogLevel = .simple) -> Self {
switch level {
case .simple:
return logRequest(.simple).logResponse(.simple)
case .verbose:
return logRequest(.verbose).logResponse(.verbose)
}
}
}
// MARK: - Request logging
extension DataRequest {
/// The request logging level. `.simple` prints only the HTTP method and path; `.verbose` prints the request body as well.
public enum RequestLogLevel {
/// Print the request's HTTP method and path.
case simple
/// Print the request's HTTP method, path, and body.
case verbose
}
/// Log the request at the specified `level`.
public func logRequest(_ level: RequestLogLevel = .simple) -> Self {
guard let method = request?.httpMethod, let path = request?.url?.absoluteString else {
return self
}
if case .verbose = level, let data = request?.httpBody, let body = String(data: data, encoding: .utf8) {
print("\(method) \(path): \"\(body)\"")
} else {
print("\(method) \(path)")
}
return self
}
}
// MARK: - Response logging
extension DataRequest {
/// The response logging level. `.simple` prints only the HTTP status code and path; `.verbose` prints the response body as well.
public enum ResponseLogLevel {
/// Print the response's HTTP status code and path, or error if one is returned.
case simple
/// Print the response's HTTP status code, path, and body, or error if one is returned.
case verbose
}
/// Log the response at the specified `level`.
public func logResponse(_ level: ResponseLogLevel = .simple) -> Self {
return response { response in
guard let code = response.response?.statusCode, let path = response.request?.url?.absoluteString else {
return
}
if case .verbose = level, let data = response.data, let body = String(data: data, encoding: .utf8) {
print("\(code) \(path): \"\(body)\"")
} else {
print("\(code) \(path)")
}
}
}
}