看了网上相关的介绍比较少,个人感觉它比较实用,写一下用法。
本人已经将其封装, github地址 直接pod GQLogger 也可以放心食用 :blush:
#import "LumberjackLauncher.h"
[LumberjackLauncher launchStandardLoggers];
[LumberjackLauncher setXcodeConsoleLogColorsWithErrorColor:self.setting.errorColor warningColor:self.setting.warningColor infoColor:self.setting.infoColor debugColor:self.setting.debugColor verboseColor:self.setting.verboseColor]; DDLogLevel logLevel = DDLogLevelAll; [LumberjackLauncher changeStandardLoggersLevel:logLevel]; [LumberjackLauncher changeAppConsoleLoggerLevel:logLevel];
4. 使用
[LumberjackLauncher showAppConsole]; [LumberjackLauncher hideAppConsole];
GQLogger.h
#import <Foundation/Foundation.h> #import "GQLoggerSetting.h" typedef void(^GQLoggerSettingBlock)(GQLoggerSetting *setting); @interface GQLogger : NSObject + (instancetype)sharedInstance; - (void)activeWithSetting:(GQLoggerSettingBlock) settingBlock; - (void)stop; - (void)showConsole; - (void)closeConsole; - (NSString *)logPath; @end
GQLogger.m
#import "GQLogger.h" #import "LumberjackLauncher.h" static GQLogger *sharedInstance; @interface GQLogger() @property (nonatomic, assign, getter = isActive)BOOL active; @property (nonatomic, strong)GQLoggerSetting *setting; @property (nonatomic, strong)DDFileLogger *fileLogger; @end @implementation GQLogger + (instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[[self class] alloc] init]; }); return sharedInstance; } - (instancetype)init { if (sharedInstance != nil) { return sharedInstance; } if ((self = [super init])) { } return self; } - (GQLoggerSetting *)setting { if (_setting == nil) { _setting = [[GQLoggerSetting alloc] init]; _setting.errorColor = [UIColor redColor]; _setting.warningColor = [UIColor yellowColor]; _setting.infoColor = [UIColor greenColor]; _setting.debugColor = [UIColor darkGrayColor]; } return _setting; } - (DDFileLogger *)fileLogger { if (_fileLogger == nil) { _fileLogger = [[DDFileLogger alloc] init]; _fileLogger.maximumFileSize = 1024 * 1024; _fileLogger.rollingFrequency = 60 * 60; [[_fileLogger logFileManager] setMaximumNumberOfLogFiles:7]; } return _fileLogger; } - (void)activeWithSetting:(GQLoggerSettingBlock)settingBlock { if (self.isActive) { return; } self.active = YES; if (settingBlock) { settingBlock(self.setting); } [self activeLogger]; } - (void)stop { if (!self.isActive) { return; } self.active = NO; [LumberjackLauncher changeStandardLoggersLevel:DDLogLevelOff]; [LumberjackLauncher changeAppConsoleLoggerLevel:DDLogLevelOff]; [DDLog removeLogger:self.fileLogger]; [DDLog addLogger:self.fileLogger withLevel:DDLogLevelOff]; } - (void)showConsole { [LumberjackLauncher showAppConsole]; } - (void)closeConsole { [LumberjackLauncher hideAppConsole]; } - (NSString *)logPath { return self.fileLogger.currentLogFileInfo.filePath; } - (void)activeLogger { [LumberjackLauncher launchStandardLoggers]; [LumberjackLauncher setXcodeConsoleLogColorsWithErrorColor:self.setting.errorColor warningColor:self.setting.warningColor infoColor:self.setting.infoColor debugColor:self.setting.debugColor verboseColor:self.setting.verboseColor]; DDLogLevel logLevel = DDLogLevelAll; [LumberjackLauncher changeStandardLoggersLevel:logLevel]; [LumberjackLauncher changeAppConsoleLoggerLevel:logLevel]; [DDLog removeLogger:self.fileLogger]; [DDLog addLogger:self.fileLogger withLevel:logLevel]; } @end