back to home
   
Blog
 
Objective-C logging framework (CocoLogger)

07/10/2010  |  Programming/Cocoa  |  Objective-C, Logging, Cocoa
 

Yes, well, Logging can easily be done using some #define statements in one of the projects header files.

But still, maybe this simple logging framework can be of use. It can log to console and/or to a file. You can find the project at Launchpad as well. It also was published under BSD license.

Here is how it can be used:

It first needs to be initialized whch can be done in the +initialize method of you AppController class.

+ (void)initialize {
    
    // get path to "Logs" folder of current user
    NSString *logPath = LOGFILE;
	
#ifdef DEBUG
    [CocoLogger initLogger:logPath 
                 logPrefix:@"[MyPrefix]" 
            logFilterLevel:LEVEL_DEBUG 
              appendToFile:YES 
              logToConsole:YES];
#endif
#ifdef RELEASE
    [CocoLogger initLogger:logPath 
                 logPrefix:@"[MyPrefix]" 
            logFilterLevel:LEVEL_WARN 
              appendToFile:YES 
              logToConsole:NO];
#endif
    // this goes into the log file
    CocoLog(LEVEL_DEBUG, @"logging initialized");    
}

This code snipped shows how to initialize the main logging Singleton object depending on the a define. DEBUG and RELEASE are set in the build settings of your Xcode project. The "appendToFile" setting does control whether a new file is created each time or if logging statements are appended to a file.

The log entry as in the last line of the snipped then looks like this in the log file:

Tue Jul  6 11:20:22 2010 DEBUG [MyPrefix] +[AppController initialize] logging initialized

The method in which the log statement happens is automatically added.

A nice feature addition to this would be to have rolling log files for each day or a configured time frame.

 


Comments


Add comment
 

all copyright by manfred bergmann
last update: 07-07-2010

logo Wicket logo Scala logo OpenEJB