Search code examples
ioscocoaloggingnslog

How to clear the content of .log file programmatically in iOS


In my application, i have logged the details of the application in project.log with the help of following code.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"project.log"];
freopen([logPath fileSystemRepresentation],"a+",stderr);

This code will perfectly log all the details that i defined in nslog. Now i don't want my log file to grow much further. So i want to clear the content of log files that was logged before 1 week or more. But i need the content of log for last 7 days. Is there any possible solution to accomplish the above?


Solution

  • There is an easy solution. It's not exactly what you have asked, but maybe it's valuable to you.

    On startup, do this:

    • check if project.log exists and is greater than (e.g.) 2MB.
    • If no, just use project.log.
    • If yes: 1) remove project.old.log if it exists. 2) rename project.log to project.old.log 3) log to project.log (it will be created if you open with "a+")