title: How to catch stderr output and send messages to a remote log file via a POST request in objective c?
body: <p>I have a simple webservice online listening for post requests. It writes to a local file strings sent to it as parameters in the body.</p>
  
  <p>Now, I have a ios app and I want to send each message of the stderr of the app to the webservice to log errors on the online file and be able to see app errors in real time, remotely.</p>
  
  <p>I know I can redirect stderr messages to a file (local to device) like this</p>
  
  <pre><code>+ (void) redirectNSLogToDocuments {
      NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [allPaths objectAtIndex:0];
      NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"errLog.txt"];
      freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
  }
  </code></pre>
  
  <p>I know i can send the post request like this, and so write the string 'errorString' to the remote log</p>
  
  <pre><code>    NSString *errorString=@"errorIOS";
      NSString *errorData =[NSString stringWithFormat:@"errorString=%@",errorString];
      NSData *postData = [errorData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
      NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
      [request setURL:[NSURL URLWithString:@"http://mywebserviceurl"]];
      [request setHTTPMethod:@"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:postData];
      NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  </code></pre>
  
  <p>But if I want to redirect the stderr messages so that each time a message of error is raised it triggers the post request to my webservice and writes the message to the remote log instead of writing to the device local file?</p>