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

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

==本人已经将其封装,github地址 直接pod GQLogger 也可以放心食用
博文地址 https://linit.space/ios-lumberjacklauncher-shi-yong/ ==

主要功能

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

界面展示

iOS - LumberjackLauncher 使用 前台直接查看日志_第1张图片
Simulator Screen Shot 2016年6月24日 上午10.38.46.png

iOS - LumberjackLauncher 使用 前台直接查看日志_第2张图片
Simulator Screen Shot 2016年6月24日 上午10.24.47.png

用法

  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];
  1. 使用
 [LumberjackLauncher showAppConsole];
 [LumberjackLauncher hideAppConsole];

代码

GQLogger.h

#import 
#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

你可能感兴趣的:(iOS - LumberjackLauncher 使用 前台直接查看日志)