Cintu
Cintu

Reputation: 913

NSLog / Log4Cocoa console logs needs to be saved in file

For my application i need to save all kind of console logs in the text or log file. I am using Log4Cocoa to generating different kind of logs. So how can i save all those console logs into the file? Could anyone please help me.

Thanks

Upvotes: 0

Views: 351

Answers (1)

Matthew Burke
Matthew Burke

Reputation: 2364

With Log4Cocoa, each logger can have one or more destinations attached to it. So, if you attach a file appender to the root logger object, every log message will be sent to that file. When you initialize the logging system, add something like:

NSString *logFileName = @"path-to-your-log-file";

[[L4Logger rootLogger] addAppender: 
        [[L4FileAppender alloc] initWithLayout:[L4Layout simpleLayout] 
                                      fileName:logFileName];

If you use the L4FileAppender, the file you write to can grow without bounds. So, you might consider using either an L4RollingFileAppender or L4DailyRollingFileAppender instead. The L4RollingFileAppender rolls the file based on file size. The L4DailyRollingFileAppender rolls the file based on time. Despite what the name suggests, you can configure an L4DailRollingFileAppender to roll at different time periods (every minute, every hour, every 1/2 day, etc.).

If you do use a file appender, then you need to give some thought to how you are going to get the log files off of your device, particularly if you need to do logging in the production version of your application.

Much as I like the elegance of Log4Cocoa, I wound up switching to NSLogger (https://github.com/fpillet/NSLogger) and you might want to give it a look.

Upvotes: 1

Related Questions