Skip to content

Instantly share code, notes, and snippets.

@payliu
Last active December 22, 2015 19:19
Show Gist options
  • Save payliu/6519088 to your computer and use it in GitHub Desktop.
Save payliu/6519088 to your computer and use it in GitHub Desktop.
Custom Level and Formatter for CocoaLumberjack

OLCustomCocoaLumberjack

this is a customize function for CocoaLumberjack

Feature

  • add Trace log level (deprecated)

  • a Formatter,

    • like NSLog Style and append extra information.
    • use __PRETTY_FUNCTION__ instead.

Installation

install via CocoaPods

Pod file

# OLCustomCocoaLumberjack
pod 'OLCustomCocoaLumberjack', :podspec => 'https://gist.github.com/payliu/6519088/raw/OLCustomCocoaLumberjack.podspec'

Usage

trace somthing. (deprecated)

DDLogTrace(@"hi trace");

output empty

DDLogMethod();

setup formatter.

OLDDFormatter *psLogger = [[OLDDFormatter alloc] init];

[[DDTTYLogger sharedInstance] setLogFormatter:psLogger];

[psLogger release];

Output

Pod::Spec.new do |s|
s.name = 'OLCustomCocoaLumberjack'
s.version = '0.0.4'
s.license = 'MIT'
s.summary = 'Custom Level and Formatter for CocoaLumberjack.'
s.homepage = 'https://gist.github.com/payliu/6519088'
s.author = { 'Pay Liu' => '[email protected]' }
s.source = { :git => 'https://gist.github.com/6519088.git', :tag => '0.0.4' }
s.source_files = '*.{h,m}'
s.platform = :ios, '8.0'
s.requires_arc = false
s.dependency 'CocoaLumberjack'
end
//
// OLCustomDDLog.h
//
// Error
// Warn
// Info
// Debug
// Verbose
//
// Created by Pay Liu on 13/9/10.
// Copyright (c) 2013年 Octalord. The MIT License.
//
#import <CocoaLumberjack/CocoaLumberjack.h>
// use __PRETTY_FUNCTION__
#undef LOG_OBJC_MAYBE
#undef LOG_C_MAYBE
#define LOG_OBJC_MAYBE(async, lvl, flg, ctx, frmt, ...) \
LOG_MAYBE(async, lvl, flg, ctx, __PRETTY_FUNCTION__, frmt, ## __VA_ARGS__)
#define LOG_C_MAYBE(async, lvl, flg, ctx, frmt, ...) \
LOG_MAYBE(async, lvl, flg, ctx, __PRETTY_FUNCTION__, frmt, ## __VA_ARGS__)
// Trace level
// #define LOG_FLAG_TRACE (1 << 4) // 0...010000
//
// #define LOG_LEVEL_TRACE (LOG_FLAG_ERROR | LOG_FLAG_WARN | LOG_FLAG_INFO | LOG_FLAG_VERBOSE | LOG_FLAG_TRACE) // 0...11111
//
// #define LOG_TRACE (ddLogLevel & LOG_FLAG_TRACE)
//
// #define DDLogTrace(frmt, ...) ASYNC_LOG_OBJC_MAYBE(ddLogLevel, LOG_FLAG_TRACE, 0, frmt, ## __VA_ARGS__)
//
// #define DDLogCTrace(frmt, ...) ASYNC_LOG_C_MAYBE(ddLogLevel, LOG_FLAG_TRACE, 0, frmt, ## __VA_ARGS__)
// empty ddlog
// default is DDLogVerbose
#define DDLogMethod() DDLogMethodVerbose()
#define DDLogMethodError() DDLogError(@"%s", __PRETTY_FUNCTION__)
#define DDLogMethodWarn() DDLogWarn(@"%s", __PRETTY_FUNCTION__)
#define DDLogMethodInfo() DDLogInfo(@"%s", __PRETTY_FUNCTION__)
#define DDLogMethodDebug() DDLogDebug(@"%s", __PRETTY_FUNCTION__)
#define DDLogMethodVerbose() DDLogVerbose(@"%s", __PRETTY_FUNCTION__)
//
// OLDDFormatter
//
// this formatter is like NSLog out, but more detail
// you can use it for DDTTYLogger.formatter
//
//
// Created by Pay on 13/2/1.
// Copyright (c) 2013年 Octalord. The MIT License.
//
#import <Foundation/Foundation.h>
#import <CocoaLumberjack/CocoaLumberjack.h>
@interface OLDDFormatter : NSObject <DDLogFormatter>
{
int _atomicLoggerCount;
NSDateFormatter *_threadUnsafeDateFormatter;
NSString *_appName;
}
@end
//
// MBDDFormatter.m
//
// Created by Pay on 13/2/1.
// Copyright (c) 2013年 Octalord. The MIT License.
//
#import "OLDDFormatter.h"
#import "OLCustomDDLog.h"
#import <unistd.h>
#import <libkern/OSAtomic.h>
@implementation OLDDFormatter
#pragma mark - class lifecycle
- (id) init
{
self = [super init];
if (self) {
_appName = [[NSProcessInfo processInfo] processName];
}
return self;
}
- (void) dealloc
{
[_threadUnsafeDateFormatter release];
[_appName release];
[super dealloc];
}
- (NSString *) stringFromDate:(NSDate *)date
{
int32_t loggerCount = OSAtomicAdd32(0, &_atomicLoggerCount);
static NSString *dateFormatString = @"yyyy-MM-dd HH:mm:ss.SSS";
if (loggerCount <= 1) {
// Single-threaded mode.
if (_threadUnsafeDateFormatter == nil) {
_threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
[_threadUnsafeDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[_threadUnsafeDateFormatter setDateFormat:dateFormatString];
}
return [_threadUnsafeDateFormatter stringFromDate:date];
} else {
// Multi-threaded mode.
// NSDateFormatter is NOT thread-safe.
NSString *key = @"MyCustomFormatter_NSDateFormatter";
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter *dateFormatter = [threadDictionary objectForKey:key];
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:dateFormatString];
[threadDictionary setObject:dateFormatter forKey:key];
[dateFormatter release];
dateFormatter = [threadDictionary objectForKey:key];
}
return [dateFormatter stringFromDate:date];
}
}
#pragma mark - protocol DDLogFormatter
- (NSString *) formatLogMessage:(DDLogMessage *)logMessage
{
NSString *logLevel = nil;
switch (logMessage->_flag) {
case DDLogFlagError:
logLevel = @"[ERROR]";
break;
case DDLogFlagWarning:
logLevel = @"[WARN ]";
break;
case DDLogFlagInfo:
logLevel = @"[INFO ]";
break;
case DDLogFlagDebug:
logLevel = @"[DEBUG]";
break;
default:
logLevel = @"[VERBO]";
break;
}
NSString *dateAndTime = [self stringFromDate:(logMessage->_timestamp)];
NSString *logMsg = logMessage->_message;
return [NSString stringWithFormat:@"%@ %@[%d:%@] %@ [Line %d] %5@ %@",
dateAndTime,
_appName,
(int)getpid(),
[logMessage threadID],
logMessage.function, // __PRETTY_FUNCTION__, @see OLCustomDDLog.h
logMessage->_line,
logLevel,
logMsg];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment