// // FKLogUtil.m // FirstLink // // Created by ascii on 15/8/6. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "FKLogUtil.h" #import #import "FKReachabilityUtil.h" #import "SSZipArchive.h" #import "UploadFileModel.h" @implementation FKLogUtil + (void)configCocoaLumberjack { FKLogFormatter *formatter = [[FKLogFormatter alloc] init]; DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; fileLogger.rollingFrequency = 60 * 60 * 24 * 7; // 7 day rolling fileLogger.logFileManager.maximumNumberOfLogFiles = 7; [fileLogger setLogFormatter:formatter]; [DDLog addLogger:fileLogger]; DDTTYLogger *logger = [DDTTYLogger sharedInstance]; [logger setLogFormatter:formatter]; //#ifdef DEBUG // [DDLog addLogger:logger withLevel:DDLogLevelDebug]; //#else [DDLog addLogger:logger withLevel:DDLogLevelInfo]; //#endif // DDASLLogger *consoleLogger = [DDASLLogger sharedInstance]; // [consoleLogger setLogFormatter:formatter]; // // [DDLog addLogger:consoleLogger withLevel:DDLogLevelDebug]; } + (void)logTransformEntity:(FKLogTransformEntity *)entity { if (entity && entity.code) { DDLogInfo(@"%@", [entity description]); } } + (void)asyncUploadRollingFiles { if (![FKLogUtil isUploadLogOpen]) { return; } #ifndef DEBUG NSString *net = [FKReachabilityUtil sharedInstance].networkType; if ([net isEqualToString:@"wifi"]) { DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; // check and remove zip file NSError *error; NSString *logsDirectoryPath = [fileLogger.logFileManager logsDirectory]; NSArray *allFilePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logsDirectoryPath error:&error]; if (!error) { for (NSString *file in allFilePaths) { if ([file containsString:@".zip"]) { [FKLogUtil removeFiltAtPath:file]; } } } NSArray *filePaths = [fileLogger.logFileManager sortedLogFilePaths]; if (filePaths.count > 1) { NSString *filePath = filePaths.lastObject; NSString *zipFilePath = [NSString stringWithFormat:@"%@.zip", filePath]; BOOL ret = [SSZipArchive createZipFileAtPath:zipFilePath withFilesAtPaths:@[filePath]]; if (ret) { // upload file [FKLogUtil uploadRollingZipFile:zipFilePath source:filePath]; } else { [FKLogUtil removeFiltAtPath:zipFilePath]; } } } #endif } + (BOOL)isUploadLogOpen { return YES; } + (void)removeFiltAtPath:(NSString *)filePath { if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil]; } } + (void)uploadRollingZipFile:(NSString *)zipFilePath source:(NSString *)sourceFilePath { NSData *data = [[NSData alloc] initWithContentsOfFile:zipFilePath]; [UploadFileModel uploadLogFile:data fileType:@"log" contentType:@"text/plain" parameters:nil success:^(MSGHeader *header, id responseObject) { if (header.code.integerValue == RESPONSE_MSG_NORMAL) { [FKLogUtil removeFiltAtPath:sourceFilePath]; } [FKLogUtil removeFiltAtPath:zipFilePath]; } failure:^(MSGHeader *header, NSError *error) { [FKLogUtil removeFiltAtPath:zipFilePath]; }]; } @end @implementation FKLogFormatter static NSString *dateFormatString = @"yyyy-MM-dd HH:mm:ss.SSS"; - (NSString *)stringFromDate:(NSDate *)date { int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount); if (loggerCount <= 1) { // Single-threaded mode. if (threadUnsafeDateFormatter == nil) { threadUnsafeDateFormatter = [[NSDateFormatter alloc] init]; [threadUnsafeDateFormatter setDateFormat:dateFormatString]; } 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 objectForKey:key]; if (dateFormatter == nil) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:dateFormatString]; [threadDictionary setObject:dateFormatter forKey:key]; } return [dateFormatter stringFromDate:date]; } } - (NSString *)formatLogMessage:(DDLogMessage *)logMessage { // NSString *logLevel; // switch (logMessage->_flag) { // case DDLogFlagError : logLevel = @"error"; break; // case DDLogFlagWarning : logLevel = @"warning"; break; // case DDLogFlagInfo : logLevel = @"info"; break; // case DDLogFlagDebug : logLevel = @"debug"; break; // default : logLevel = @"verbose"; break; // } NSString *dateAndTime = [self stringFromDate:(logMessage.timestamp)]; NSString *logMsg = logMessage->_message; return [NSString stringWithFormat:@"%@;%@;%@;%@;%@" , dateAndTime , PlatformIdentify , [[UIDevice currentDevice] systemVersion] , [SystemUtil applicationVersion] , logMsg ]; } - (void)didAddToLogger:(id )logger { OSAtomicIncrement32(&atomicLoggerCount); } - (void)willRemoveFromLogger:(id )logger { OSAtomicDecrement32(&atomicLoggerCount); } @end