Skip to content

Instantly share code, notes, and snippets.

@lexrus lexrus/LTLog.h
Last active Feb 21, 2016

Embed
What would you like to do?
My DDLog config
//
// 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
//
// 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
@lexrus

This comment has been minimized.

Copy link
Owner Author

lexrus commented Aug 15, 2014

  1. Install CocoaLumberjack with CocoaPods: pod "CocoaLumberjack", "~> 2.0" and pod install
  2. Add LTLog.h and LTLog.m into your project
  3. Append #import "LTLog.h" to your ***-Prefix.pch
  4. Install XcodeColor: https://github.com/robbiehanson/XcodeColors
  5. Add an environment variable to your debug scheme XcolorColors and set its value to YES
    2014-08-15 at 10 29 pm
@lexrus

This comment has been minimized.

Copy link
Owner Author

lexrus commented Aug 15, 2014

screen shot 2014-08-15 at 2 06 43 pm

@kikohz

This comment has been minimized.

Copy link

kikohz commented Aug 15, 2014

nice

@phpmaple

This comment has been minimized.

Copy link

phpmaple commented Aug 17, 2014

@phpmaple

This comment has been minimized.

Copy link

phpmaple commented Oct 21, 2014

貌似Xcode6.1不输出了

@lexrus

This comment has been minimized.

Copy link
Owner Author

lexrus commented Apr 29, 2015

我更新了一下,现在在 Xcode 6.3 里也能用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.