This solution can realize behavior statistics, combined with crash statistics, can quickly find the root cause of online problems.

  • CocoaLumberjack
  • SSZipArchive
  • Alibaba Cloud (or Tencent Cloud)Ali Cloud sample will be used in this paper

CocoaLumberjack

  • CocoaLumberjack is a very useful logging framework, also known as DDLog, which has much better performance and speed than NSLog.

  • DDLog can collect all the printed records to a local sandbox file, so that you can view all the records that you want to collect from the current user.

1. Initialize the configuration

1.1. DDLog log levels must be defined globally for compilation to pass

#ifdef DEBUG
static const int ddLogLevel = DDLogLevelVerbose;
#else
static const int ddLogLevel = DDLogLevelWarning;
#endif

DDLogLevelOff Closes the Log
// DDLogLevelError Prints Error level logs
// DDLogLevelWarning Prints Error and Warning logs
// DDLogLevelInfo Prints logs at the Error, Warn, and Info levels
// DDLogLevelDebug Prints Error, Warn, Info, and Debug logs
// DDLogLevelVerbose Displays logs at the Error, Warn, Info, Debug, and Verbose levels
Copy the code

1.2 Initial Configuration

#import <CocoaLumberjack/CocoaLumberjack.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Add DDASLLogger and your logging statements will be sent to the Xcode console
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    
    / / add DDFileLogger, your log statements will be written to a file, the default path in the sandbox [fileLogger. LogFileManager logsDirectory] directory and file called bundleid + space + date. Log.
    DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
    // The refresh frequency is 24 hours
    fileLogger.rollingFrequency = 60 * 60 * 24;
    // The maximum number of files that a colleague can contain
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    [DDLog addLogger:fileLogger];

    return YES;
}

// This configuration doesn't have to be in the AppDelegate, as long as it's executed in the correct order
Copy the code

2. Perform the log

The same way you use NSLog

DDLogVerbose (@ "Verbose");// Log detailsDDLogDebug (@ "Debug");// Debug logsDDLogInfo (@ "Info");// Information logDDLogWarn (@ "Warn");// Warning logDDLogError (@ "Error");// Error log
Copy the code

3. The sample

The above basic simple use process has been introduced, for example, easy to understand.

3.1 Simulating two Methods to Print logs

3.2 Find the storage path (how to find above) and view the resultPs: The time is UTC time. Add 8 hours to Beijing time.

Two, SSZipArchive simple usage

  • SSZipArchiveIs a useful third party for compression and decompression of object files

1. Use

/ / compression
- (void)createZipFile {
    // Destination path
    NSString *destinationPath = @"/Users/Administrator/Desktop/xxxxx.zip";// Note that this is the suffix of the zip format
    // Source file path
    NSString *sourceFilePath = @"/Users/Administrator/Desktop/xxxxx.txt";
    // Multiple source files can be placed in the array, and these files will be packaged into a compressed package, to the destinationPath.
    if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath]]) {
        NSLog(@" Compression succeeded");
    }
    else {
        NSLog(@" Compression failed"); }}/ /
- (void)unzipFile {
    // Source file path
    NSString *sourceFilePath = @"/Users/Administrator/Desktop/xxxxx.zip";
    // Destination file path
    NSString *destinationPath = @"/Users/Administrator/Desktop/";
    // Unzip zip package from sourceFilePath to destinationPath
    if ([SSZipArchive unzipFileAtPath:sourceFilePath toDestination:destinationPath delegate:self uniqueId:nil]) {NSLog(@" Unzip successful");
    }
    else {
        NSLog(@" Unzip failed"); }}/// proxy method
#pragma mark - SSZipArchiveDelegate
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
    NSLog(@" Going to decompress");
}
 
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {
    NSLog(@" Decompression completed");
}
Copy the code

Alibaba Cloud

  • As for how to use Ali Cloud, I will not talk directly here, click to view the development document

Four, in combination with

Combined with the above, a user behavior collection scheme is set up

  1. throughCocoaLumberjackCollect logs in a unified manner
  2. throughSSZipArchiveCompress the collected logs (to facilitate the search process)
  3. Upload the compressed package through Ali CloudAli cloudDesignated folder

Through three frameworks, all simple to use, the user’s behavior is collected, and problems can be clearly seen (if your Log is detailed enough) by looking for specific users’ logs in directories that exist in the Ali Cloud.

Five, the actual combat

  1. Setup macro what not to say. The above are
  2. Go straight to code
// upload local logs
- (void)uploadLocalLog {
	// Determine the location to upload to Aliyun and the file name
    NSString * fileName = [NSString stringWithFormat:@"iOSLog/%@.zip",userID];
	// Configure aliyun information
    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
	/ / bucket name
    put.bucketName = [self baseBucketName]; // Check with the backend developer
    put.objectKey = fileName;
    
    // Configure DDLog information
    DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
	
	// Find the default log storage path
    NSString * logDirStr = [fileLogger.logFileManager logsDirectory];
    
    // Path of the current log
    NSString *filePath = [NSString stringWithFormat:@"%@/logs.zip",logDirStr];
    
    // Sort by time
    NSArray *fileLogsArr = [fileLogger.logFileManager sortedLogFilePaths];
	
	/ / compression
    BOOL success = [SSZipArchive createZipFileAtPath:filePath withFilesAtPaths:fileLogsArr] ;
    if (success) {
        put.uploadingFileURL = [NSURL URLWithString:filePath];

		// Upload to Aliyun according to the current file path address. Omit the upload code. }}Copy the code
  1. Add the DDLog wherever you want the phone log, and it will be automatically saved to the specified location.

About the timing of uploading to Aliyun

In fact, the timing of upload is more about the current product demand

  • When some apps are uploaded, all the logs of the last time will be uploaded.
  • Some app upload time ask to set timer upload.
  • Some apps are when they crash.
  • Some apps are requesting interfaces.
  • Some apps are a combination of the above.

You can go to ali OSS browser to see the corresponding behavior log file.

Open to view:

reference

  1. zhuanlan.zhihu.com/p/138116219
  2. Help.aliyun.com/document_de…
  3. Blog.csdn.net/happyrabbit…
  4. Blog.csdn.net/u014600626/…