转载

iOS - LumberjackLauncher 使用 前台直接查看日志

看了网上相关的介绍比较少,个人感觉它比较实用,写一下用法。

本人已经将其封装, github地址 直接pod GQLogger 也可以放心食用 :blush:

主要功能

  1. 结合ddlog也就是CocoaLumberjack使用,能将打印的日志显示到一个View上,便于开发排查问题,直接显示网络包等。
  2. 方便查看打印的Log

界面展示

iOS - LumberjackLauncher 使用 前台直接查看日志 iOS - LumberjackLauncher 使用 前台直接查看日志

用法

  1. 头文件
    #import "LumberjackLauncher.h"
  2. 初始化
    [LumberjackLauncher launchStandardLoggers];
  3. 配置
[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 
原文  http://www.linit.space/ios-lumberjacklauncher-shi-yong/
正文到此结束
Loading...