Changing the log Format

833 views
Skip to first unread message

Sohil

unread,
Dec 17, 2011, 11:57:52 PM12/17/11
to CocoaLumberjack
Hi ,

I have just recently started using lumberjack.

I was checking out the logging framework.

Following is the thing i tried out

NSLog(@"Logger Iniit Done");
DDLogVerbose(@"Hello Done int");
DDLogVerbose(@"Broken sprocket detected!");
DDLogError(@"Broken sprocket detected!");

and following was the output

2011-12-18 10:21:29.644 TestingLogging[3323:10103] Logger Iniit Done
2011-12-18 10:21:29:646 TestingLogging[3323:10103] Hello Done int
2011-12-18 10:21:29:653 TestingLogging[3323:10103] Broken sprocket
detected!
2011-12-18 10:21:29:653 TestingLogging[3323:10103] Broken sprocket
detected!

I was looking for output like this which tells me more about the type
of message

2011-12-18 10:21:29:646 TestingLogging[3323:10103] INFO Hello Done int
2011-12-18 10:21:29:653 TestingLogging[3323:10103] INFO Broken
sprocket detected!
2011-12-18 10:21:29:653 TestingLogging[3323:10103] ERROR Broken
sprocket detected!

wanted to know if there was a easy way of changing this.
I looked through the wiki ,i think CustomFormatter will be the way.
But wanted to know if there was a easy way to do so.


Thanks,
Sohil

Joe Francia

unread,
Dec 18, 2011, 1:45:46 PM12/18/11
to cocoalu...@googlegroups.com
CustomLogFormatters are the easy way to do it:

------------------------------------------------------------------------------------------------------------------------------
//MyCustomLogFormatters.h
#import "DDLog.h"

@interface MyFileLogFormatter : NSObject <DDLogFormatter>
@end

//Add other custom formatters if you want the messages displayed differently in say the console vs. the log file


//-----------------------------------------------------------------------------------------------------------------------------
//MyCustomLogFormatters.m
#import "MyCustomLogFormatters.h"

@implementation MyFileLogFormatter

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
{
    //Alter the message to your liking
    NSString *msg = [logMessage->logMsg stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    
    NSString *logLevel;
    switch (logMessage->logFlag)
    {
        case LOG_FLAG_ERROR : logLevel = @"E"; break;
        case LOG_FLAG_WARN  : logLevel = @"W"; break;
        case LOG_FLAG_INFO  : logLevel = @"I"; break;
        default             : logLevel = @"V"; break;
    }

//Also display the file the logging occurred in to ease later debugging
    NSString *file = [[[NSString stringWithUTF8String:logMessage->file] lastPathComponent] stringByDeletingPathExtension];

//Format the message for the server-side log file parser    
    return [NSString stringWithFormat:@"%@ %x %@ \"%@\" || [%@@%s@%i]", logMessage->timestamp, logMessage->machThreadID, logLevel, msg, file, logMessage->function, logMessage->lineNumber];
}

@end

//-----------------------------------------------------------------------------------------------------------------------
//And in your application:didFinishLaunchingWithOptions:
//Setup logging
    MyFileLogFormatter *fileLogFormatter = [[[MyFileLogFormatter alloc] init] autorelease];
    
    DDFileLogger *fileLogger = [[DDFileLogger alloc] initWithLogFileManager:logFileManager];
    fileLogger.rollingFrequency = 60*60; // 1 hour rolling
    fileLogger.logFileManager.maximumNumberOfLogFiles = 10;
    [fileLogger setLogFormatter:fileLogFormatter];
    
    [DDLog addLogger:fileLogger];
    [fileLogger release];

-- 
Joe Francia
Sent with Sparrow

--
You received this message because you are subscribed to the Google Groups "CocoaLumberjack" group.
To post to this group, send email to cocoalu...@googlegroups.com.
To unsubscribe from this group, send email to cocoalumberja...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cocoalumberjack?hl=en.

Reply all
Reply to author
Forward
0 new messages