RemoteLogger 0.1.5

RemoteLogger 0.1.5

测试已测试
语言语言 Obj-CObjective C
许可 Apache-2.0
发布最新发布2023年3月

Evgeniy Sinev 维护。



  • Evgeniy Sinev

RemoteLogger

Build Status

RemoteLogger 是一个 Objective-C 库,帮助您将日志发送到您的 HTTPS 服务器。

当我们开始开发新的应用程序时,通常需要将调试信息发送到服务器。我们应该处理远程日志。当我们收集调试信息时,我们应始终考虑资源限制。

之前我们使用 UDP 传输时有过不好的体验。总是通过 UDP 传输将所有日志消息放在服务器上令人难以置信,因此我们提出了 HTTPS 传输。

介绍 RemoteLogger 库。它首先将调试信息保存到内存缓冲区,然后将其存储到文件中,然后将其发送到 HTTPS 服务器。

功能

  • 轻量(无外部依赖项)
  • 通过 http/https 发送日志

使用依赖管理器设置

Cocoapods

pod 'RemoteLogging', '0.1.0'

pod "BerTlv", :git => '[email protected]:payneteasy/RemoteLogging.git', :tag => '0.1.0'

Carthage

github "RemoteLogging/BerTlv"  "0.1.0"

如何使用

请参阅本项目中的SampleApp以了解最佳使用方法。 https://github.com/payneteasy/RemoteLogger/tree/master/SampleApp

以下代码展示了一些原则。

# define DLog(fmt, ...) [RemoteLogging send:__PRETTY_FUNCTION__ line:__LINE__ originalFormat:fmt , ##__VA_ARGS__];
 
 @interface RemoteLogging : NSObject
 
 +(void)incrementSession;
 
 +(void)     send:(const char*) aSender
             line:(int) aLine
   originalFormat:(NSString *)aOriginalFormat
                 , ...
 ;
 
 @end

@implementation RemoteLogging

+ (RLHttpRemoteLogger *) sharedInstance {
    static dispatch_once_t      predicate;
    static RLHttpRemoteLogger   * instance;

    dispatch_once(&predicate, ^{
        RLDirectory * directory = [[RLDirectory alloc] initCacheWithName:@"remote-logs"];
        [directory mkDir];

        instance = [[RLHttpRemoteLogger alloc]
                initWithDirectory:directory.path
                        uploadUrl:YOUR_UPLOAD_URL
                      accessToken:YOUR_ACCESS_TOKEN
                 memoryBufferSize:50  * 1024
                         fileSize:200 * 1024
                       loggerName:@"remote"
                       filesCount:50
                    directorySize:10 * 1024 * 1024];
    });
    return instance;
}

+ (void)incrementSession {
    [[RemoteLogging sharedInstance] mark];
}

+ (void)send:(const char *)aSender line:(int)aLine originalFormat:(NSString *)aOriginalFormat, ... {
    va_list args;
    va_start(args,aOriginalFormat);
    NSString *logLine = [[NSString alloc] initWithFormat:aOriginalFormat arguments:args];
    va_end(args);

    // write to log immediately
    NSLog(@"%@: %@", [NSThread currentThread], logLine);

    NSString *outputText = [self createOutputText:aSender aLine:aLine text:logLine];
    [[RemoteLogging sharedInstance] send:outputText];
}

+ (NSString *)createOutputText:(char const *)aSender aLine:(int)aLine text:(NSString *)aText {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss.SSS ZZZZZ"];

    // date
    // sender
    // line
    // log line
    NSString *outputText = [NSString stringWithFormat:@"%@ %s,%d - %@\n"
            , [dateFormat stringFromDate:[NSDate date]]
            , aSender
            , aLine
            , aText
    ];
    return outputText;
}

@end