Last active
February 21, 2016 19:56
-
-
Save lexrus/8c6414e7c0177e9e66ea to your computer and use it in GitHub Desktop.
My DDLog config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LTLog.h | |
// | |
// Created by Lex on 6/29/14. | |
// Copyright (c) 2014 LexTang.com. All rights reserved. | |
// https://gist.github.com/lexrus/8c6414e7c0177e9e66ea | |
// | |
#import <Foundation/Foundation.h> | |
#define DD_LEGACY_MACROS 0 | |
#import <CocoaLumberjack/CocoaLumberjack.h> | |
#import <CocoaLumberjack/DDTTYLogger.h> | |
#import <CocoaLumberjack/DDASLLogger.h> | |
#ifdef DEBUG | |
static const int ddLogLevel = DDLogLevelVerbose; | |
#else | |
static const int ddLogLevel = DDLogLevelOff; | |
#endif | |
@interface LTLog : NSObject <DDLogFormatter> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LTLog.m | |
// | |
// Created by Lex on 6/29/14. | |
// Copyright (c) 2014 LexTang.com. All rights reserved. | |
// https://gist.github.com/lexrus/8c6414e7c0177e9e66ea | |
// | |
#import "LTLog.h" | |
#import <libkern/OSAtomic.h> | |
#define kDateFormatString @"HH:mm:ss:SSS" | |
@implementation LTLog { | |
int atomicLoggerCount; | |
NSDateFormatter *threadUnsafeDateFormatter; | |
} | |
+ (void)load { | |
[DDLog addLogger:[DDASLLogger sharedInstance]]; | |
[DDLog addLogger:[DDTTYLogger sharedInstance]]; | |
[[DDTTYLogger sharedInstance] setColorsEnabled:YES]; | |
LTLog *log = LTLog.new; | |
[[DDASLLogger sharedInstance] setLogFormatter:log]; | |
[[DDTTYLogger sharedInstance] setLogFormatter:log]; | |
} | |
- (NSString *)stringFromDate:(NSDate *)date { | |
int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount); | |
NSLocale *posixLocale = | |
[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; | |
if (loggerCount <= 1) { | |
// Single-threaded mode. | |
if (threadUnsafeDateFormatter == nil) { | |
threadUnsafeDateFormatter = [[NSDateFormatter alloc] init]; | |
[threadUnsafeDateFormatter | |
setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[threadUnsafeDateFormatter setLocale:posixLocale]; | |
[threadUnsafeDateFormatter setDateFormat:kDateFormatString]; | |
} | |
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[key]; | |
if (dateFormatter == nil) { | |
dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[dateFormatter setLocale:posixLocale]; | |
[dateFormatter setDateFormat:kDateFormatString]; | |
threadDictionary[key] = dateFormatter; | |
} | |
return [dateFormatter stringFromDate:date]; | |
} | |
} | |
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage { | |
NSString *logLevel; | |
switch (logMessage->_flag) { | |
case DDLogFlagError: | |
logLevel = @"🆘"; | |
break; | |
case DDLogFlagWarning: | |
logLevel = @"❎"; | |
break; | |
case DDLogFlagInfo: | |
logLevel = @"✅"; | |
break; | |
case DDLogFlagDebug: | |
logLevel = @"🚸"; | |
break; | |
default: | |
logLevel = @"🔸"; | |
break; | |
} | |
NSString *dateAndTime = [self stringFromDate:(logMessage->_timestamp)]; | |
NSString *logMsg = logMessage->_message; | |
NSString *fileName = logMessage.fileName; | |
NSUInteger lineNumber = logMessage->_line; | |
return | |
[NSString stringWithFormat:@"%@ %@ %@:%lu> %@", logLevel, dateAndTime, | |
fileName, lineNumber, logMsg]; | |
} | |
- (void)didAddToLogger:(id<DDLogger>) __attribute__((unused))logger { | |
OSAtomicIncrement32(&atomicLoggerCount); | |
} | |
- (void)willRemoveFromLogger:(id<DDLogger>) __attribute__((unused))logger { | |
OSAtomicDecrement32(&atomicLoggerCount); | |
} | |
@end |
nice
赞
貌似Xcode6.1不输出了
我更新了一下,现在在 Xcode 6.3 里也能用。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pod "CocoaLumberjack", "~> 2.0"
andpod install
LTLog.h
andLTLog.m
into your project#import "LTLog.h"
to your***-Prefix.pch
XcolorColors
and set its value toYES