暫無描述

FKLogUtil.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // FKLogUtil.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/8/6.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKLogUtil.h"
  9. #import <libkern/OSAtomic.h>
  10. #import "FKReachabilityUtil.h"
  11. #import "SSZipArchive.h"
  12. #import "UploadFileModel.h"
  13. @implementation FKLogUtil
  14. + (void)configCocoaLumberjack {
  15. FKLogFormatter *formatter = [[FKLogFormatter alloc] init];
  16. DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
  17. fileLogger.rollingFrequency = 60 * 60 * 24 * 7; // 7 day rolling
  18. fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
  19. [fileLogger setLogFormatter:formatter];
  20. [DDLog addLogger:fileLogger];
  21. DDTTYLogger *logger = [DDTTYLogger sharedInstance];
  22. [logger setLogFormatter:formatter];
  23. //#ifdef DEBUG
  24. // [DDLog addLogger:logger withLevel:DDLogLevelDebug];
  25. //#else
  26. [DDLog addLogger:logger withLevel:DDLogLevelInfo];
  27. //#endif
  28. // DDASLLogger *consoleLogger = [DDASLLogger sharedInstance];
  29. // [consoleLogger setLogFormatter:formatter];
  30. //
  31. // [DDLog addLogger:consoleLogger withLevel:DDLogLevelDebug];
  32. }
  33. + (void)logTransformEntity:(FKLogTransformEntity *)entity {
  34. if (entity && entity.code) {
  35. DDLogInfo(@"%@", [entity description]);
  36. }
  37. }
  38. + (void)asyncUploadRollingFiles {
  39. if (![FKLogUtil isUploadLogOpen]) {
  40. return;
  41. }
  42. #ifndef DEBUG
  43. NSString *net = [FKReachabilityUtil sharedInstance].networkType;
  44. if ([net isEqualToString:@"wifi"]) {
  45. DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
  46. // check and remove zip file
  47. NSError *error;
  48. NSString *logsDirectoryPath = [fileLogger.logFileManager logsDirectory];
  49. NSArray *allFilePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logsDirectoryPath error:&error];
  50. if (!error) {
  51. for (NSString *file in allFilePaths) {
  52. if ([file containsString:@".zip"]) {
  53. [FKLogUtil removeFiltAtPath:file];
  54. }
  55. }
  56. }
  57. NSArray *filePaths = [fileLogger.logFileManager sortedLogFilePaths];
  58. if (filePaths.count > 1) {
  59. NSString *filePath = filePaths.lastObject;
  60. NSString *zipFilePath = [NSString stringWithFormat:@"%@.zip", filePath];
  61. BOOL ret = [SSZipArchive createZipFileAtPath:zipFilePath withFilesAtPaths:@[filePath]];
  62. if (ret) {
  63. // upload file
  64. [FKLogUtil uploadRollingZipFile:zipFilePath source:filePath];
  65. } else {
  66. [FKLogUtil removeFiltAtPath:zipFilePath];
  67. }
  68. }
  69. }
  70. #endif
  71. }
  72. + (BOOL)isUploadLogOpen {
  73. return YES;
  74. }
  75. + (void)removeFiltAtPath:(NSString *)filePath {
  76. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  77. [[NSFileManager defaultManager] removeItemAtPath:filePath
  78. error:nil];
  79. }
  80. }
  81. + (void)uploadRollingZipFile:(NSString *)zipFilePath source:(NSString *)sourceFilePath {
  82. NSData *data = [[NSData alloc] initWithContentsOfFile:zipFilePath];
  83. [UploadFileModel uploadLogFile:data
  84. fileType:@"log"
  85. contentType:@"text/plain"
  86. parameters:nil
  87. success:^(MSGHeader *header, id responseObject)
  88. {
  89. if (header.code.integerValue == RESPONSE_MSG_NORMAL) {
  90. [FKLogUtil removeFiltAtPath:sourceFilePath];
  91. }
  92. [FKLogUtil removeFiltAtPath:zipFilePath];
  93. } failure:^(MSGHeader *header, NSError *error) {
  94. [FKLogUtil removeFiltAtPath:zipFilePath];
  95. }];
  96. }
  97. @end
  98. @implementation FKLogFormatter
  99. static NSString *dateFormatString = @"yyyy-MM-dd HH:mm:ss.SSS";
  100. - (NSString *)stringFromDate:(NSDate *)date {
  101. int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount);
  102. if (loggerCount <= 1) {
  103. // Single-threaded mode.
  104. if (threadUnsafeDateFormatter == nil) {
  105. threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
  106. [threadUnsafeDateFormatter setDateFormat:dateFormatString];
  107. }
  108. return [threadUnsafeDateFormatter stringFromDate:date];
  109. } else {
  110. // Multi-threaded mode.
  111. // NSDateFormatter is NOT thread-safe.
  112. NSString *key = @"MyCustomFormatter_NSDateFormatter";
  113. NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
  114. NSDateFormatter *dateFormatter = [threadDictionary objectForKey:key];
  115. if (dateFormatter == nil) {
  116. dateFormatter = [[NSDateFormatter alloc] init];
  117. [dateFormatter setDateFormat:dateFormatString];
  118. [threadDictionary setObject:dateFormatter forKey:key];
  119. }
  120. return [dateFormatter stringFromDate:date];
  121. }
  122. }
  123. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  124. // NSString *logLevel;
  125. // switch (logMessage->_flag) {
  126. // case DDLogFlagError : logLevel = @"error"; break;
  127. // case DDLogFlagWarning : logLevel = @"warning"; break;
  128. // case DDLogFlagInfo : logLevel = @"info"; break;
  129. // case DDLogFlagDebug : logLevel = @"debug"; break;
  130. // default : logLevel = @"verbose"; break;
  131. // }
  132. NSString *dateAndTime = [self stringFromDate:(logMessage.timestamp)];
  133. NSString *logMsg = logMessage->_message;
  134. return [NSString stringWithFormat:@"%@;%@;%@;%@;%@"
  135. , dateAndTime
  136. , PlatformIdentify
  137. , [[UIDevice currentDevice] systemVersion]
  138. , [SystemUtil applicationVersion]
  139. , logMsg
  140. ];
  141. }
  142. - (void)didAddToLogger:(id <DDLogger>)logger {
  143. OSAtomicIncrement32(&atomicLoggerCount);
  144. }
  145. - (void)willRemoveFromLogger:(id <DDLogger>)logger {
  146. OSAtomicDecrement32(&atomicLoggerCount);
  147. }
  148. @end