我的DDLog

.PCH中的内容

#import "CocoaLumberjack.h"
 static const int ddLogLevel = DDLogLevelVerbose;//定义日志级别

Appdelegate

#import "AppDelegate.h"
#import "CocoaLumberjack.h"
#import "MyCustomFormatter.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    setenv("XcodeColors", "YES", 0);
    static const int ddLogLevel = DDLogLevelVerbose;//定义日志级别
    [DDLog addLogger:[DDTTYLogger sharedInstance]];// 初始化DDLog日志输出
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];// 启用颜色区分
    [DDTTYLogger sharedInstance].logFormatter = [MyCustomFormatter sharedInstance];
   
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor grayColor] backgroundColor:nil forFlag:DDLogFlagVerbose];
    
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor greenColor] backgroundColor:nil forFlag:DDLogFlagDebug];
    
    DDLogError(@"DDLogError");      // red
    DDLogWarn(@"DDLogWarn");        // orange
    DDLogDebug(@"DDLogDebug");      // green
    DDLogInfo(@"DDLogInfo");        // pink
    DDLogVerbose(@"DDLogVerbose");  // gray

    return YES;
}
@end

Formatter

//.h
#import 
#import "DDLog.h"

@interface MyCustomFormatter: NSObject
+(instancetype)sharedInstance;
@end

//.m
#define DATE_STRING @"HH:mm:ss:SS"

#import "MyCustomFormatter.h"
#import 
@interface MyCustomFormatter(){
    int atomicLoggerCount;
    NSDateFormatter *threadUnsafeDateFormatter;
}
@end

@implementation MyCustomFormatter
+(instancetype)sharedInstance{
    static MyCustomFormatter *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}


- (id)init {
    if((self = [super init])) {
        threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
        [threadUnsafeDateFormatter setDateFormat:DATE_STRING];
    }
    return self;
}


- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
    NSString *logLevel;
    switch (logMessage->_flag) {
        case DDLogFlagError    : logLevel = @"E"; break;
        case DDLogFlagWarning  : logLevel = @"W"; break;
        case DDLogFlagInfo     : logLevel = @"I"; break;
        case DDLogFlagDebug    : logLevel = @"D"; break;
        default                : logLevel = @"V"; break;
    }
    
    NSString *dateAndTime = [threadUnsafeDateFormatter stringFromDate:(logMessage->_timestamp)];
    NSString *logMsg = logMessage->_message;
    
    return [NSString stringWithFormat:@"%@ %@ | %@", logLevel, dateAndTime, logMsg];
}

- (NSString *)stringFromDate:(NSDate *)date {
    int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount);
    
    if (loggerCount <= 1) {
        // Single-threaded mode.
        if (threadUnsafeDateFormatter == nil) {
            threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
            [threadUnsafeDateFormatter setDateFormat:DATE_STRING];
        }
        
        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 setDateFormat:DATE_STRING];
            [threadDictionary setObject:dateFormatter forKey:key];
        }
        
        return [dateFormatter stringFromDate:date];
    }
}

- (void)didAddToLogger:(id )logger {
    OSAtomicIncrement32(&atomicLoggerCount);
}

- (void)willRemoveFromLogger:(id )logger {
    OSAtomicDecrement32(&atomicLoggerCount);
}

@end

CocoaLumberjack中的文件

  • CocoaLumberjack.h
  • DDLegacyMacros.h
  • DDLog.h DDLog.m
  • DDTTYLogger.h DDTTYLogger.m
  • MyCustomFormatter.h MyCustomFormatter.m

你可能感兴趣的:(我的DDLog)