mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-05-12 19:19:41 +08:00
Added support for truncating the length of logged request and response bodies via the 'RKLogMaxLength' environment variable to avoid getting hammered with output on the Trace level
This commit is contained in:
@@ -28,6 +28,27 @@
|
||||
#undef RKLogComponent
|
||||
#define RKLogComponent RKlcl_cRestKitNetwork
|
||||
|
||||
static BOOL RKLogIsStringBlank(NSString *string)
|
||||
{
|
||||
return ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0);
|
||||
}
|
||||
|
||||
static NSString *RKLogTruncateString(NSString *string)
|
||||
{
|
||||
static NSInteger maxMessageLength;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
NSDictionary *envVars = [[NSProcessInfo processInfo] environment];
|
||||
maxMessageLength = RKLogIsStringBlank(envVars[@"RKLogMaxLength"]) ? NSIntegerMax : [envVars[@"RKLogMaxLength"] integerValue];
|
||||
});
|
||||
|
||||
return ([string length] <= maxMessageLength)
|
||||
? string
|
||||
: [NSString stringWithFormat:@"%@... (truncated at %ld characters)",
|
||||
[string substringToIndex:maxMessageLength],
|
||||
(long) maxMessageLength];
|
||||
}
|
||||
|
||||
@interface RKHTTPRequestOperationLogger : NSObject
|
||||
|
||||
+ (id)sharedLogger;
|
||||
@@ -76,12 +97,13 @@
|
||||
- (void)HTTPOperationDidStart:(NSNotification *)notification
|
||||
{
|
||||
RKHTTPRequestOperation *operation = [notification object];
|
||||
NSString *body = nil;
|
||||
if ([operation.request HTTPBody]) {
|
||||
body = [NSString stringWithUTF8String:[[operation.request HTTPBody] bytes]];
|
||||
}
|
||||
|
||||
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
|
||||
NSString *body = nil;
|
||||
if ([operation.request HTTPBody]) {
|
||||
body = RKLogTruncateString([NSString stringWithUTF8String:[[operation.request HTTPBody] bytes]]);
|
||||
}
|
||||
|
||||
RKLogTrace(@"%@ '%@':\nrequest.headers=%@\nrequest.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], [operation.request allHTTPHeaderFields], body);
|
||||
} else {
|
||||
RKLogInfo(@"%@ '%@'", [operation.request HTTPMethod], [[operation.request URL] absoluteString]);
|
||||
@@ -99,7 +121,7 @@
|
||||
}
|
||||
} else {
|
||||
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
|
||||
RKLogTrace(@"%@ '%@' (%ld):\nresponse.headers=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode], [operation.response allHeaderFields], operation.responseString);
|
||||
RKLogTrace(@"%@ '%@' (%ld):\nresponse.headers=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode], [operation.response allHeaderFields], RKLogTruncateString(operation.responseString));
|
||||
} else {
|
||||
RKLogInfo(@"%@ '%@' (%ld)", [operation.request HTTPMethod], [[operation.request URL] absoluteString], (long)[operation.response statusCode]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user