iOS日志埋点方案 发表于 2024-11-27 | 更新于: 2024-11-27 | 字数统计: 537 字 | 阅读时长 ≈ 2 分钟 采用主流的日志库方案作为基础依赖 CocoaLumberjack 然后自己封装一层调用 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140#import <Foundation/Foundation.h>#import <CocoaLumberjack/CocoaLumberjack.h>#define OILogError(frmt, ...) OI_LOG_MAYBE(DDLogLevelError, DDLogFlagError, frmt, ##__VA_ARGS__)#define OILogWarn(frmt, ...) OI_LOG_MAYBE(DDLogLevelWarning, DDLogFlagWarning, frmt, ##__VA_ARGS__)#define OILogInfo(frmt, ...) OI_LOG_MAYBE(DDLogLevelInfo, DDLogFlagInfo, frmt, ##__VA_ARGS__)#define OILogDebug(frmt, ...) OI_LOG_MAYBE(DDLogLevelDebug, DDLogFlagDebug, frmt, ##__VA_ARGS__)#define OI_LOG_MAYBE(lvl, flg, frmt, ...) \ if (lvl & flg) { \ [DDLog log:YES level:lvl flag:flg context:0 file:__FILE__ function:__FUNCTION__ line:__LINE__ tag:0 format:(frmt), ## __VA_ARGS__]; \ }@interface OILog : NSObject+ (NSString *)defaultLogFilePath;@endNS_ASSUME_NONNULL_END #import "OILog.h"#import "OILogFormatter.h"@implementation OILog //无需主动调用 把这些代码加进项目即可, app启动无感调用+ (void)load { @weakify(self); __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { @strongify(self); [self didFinishLaunchingOnLoad:note]; [[NSNotificationCenter defaultCenter] removeObserver:observer]; }];}+ (void)didFinishLaunchingOnLoad:(NSNotification *)noti { [self initSDK];}+ (void)initSDK { [DDLog removeAllLoggers]; OILogFormatter *logFormatter = [[OILogFormatter alloc] init];// add logger for Terminal output or Xcode console output. DDOSLogger *ttyLogger = [DDOSLogger sharedInstance]; ttyLogger.logFormatter = logFormatter;// ttyLogger.colorsEnabled = YES; [DDLog addLogger:ttyLogger withLevel:DDLogLevelAll]; // add logger for Cached DDLogFileManagerDefault *fileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:[self defaultLogFilePath]]; DDFileLogger *fileLogger = [[DDFileLogger alloc] initWithLogFileManager:fileManager]; // 刷新频率为一天,超过一天会生成新的log文件 fileLogger.rollingFrequency = 60 * 60 * 24; // 文件大小阈值,超过该大小,也会生成新的log文件 fileLogger.maximumFileSize = 1 * 1024 * 1024; // 为0.则表示不限制文件个数 fileLogger.logFileManager.maximumNumberOfLogFiles = 0; fileLogger.logFormatter = logFormatter; // info以上上报日志 [DDLog addLogger:fileLogger withLevel:DDLogLevelInfo];}+ (NSString *)defaultLogFilePath { NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"OILog"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:filePath]) { NSError *error = nil; [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error]; if (error) { OILogDebug(@"日志目录创建失败!!!"); } } return filePath;}@end #import <Foundation/Foundation.h>#import <CocoaLumberjack/CocoaLumberjack.h>NS_ASSUME_NONNULL_BEGIN@interface OILogFormatter : NSObject<DDLogFormatter>@endNS_ASSUME_NONNULL_END #import "OILogFormatter.h"@implementation OILogFormatter- (NSString *)formatLogMessage:(DDLogMessage *)logMessage { NSString *logLevel; switch (logMessage.flag) { case DDLogFlagDebug: logLevel = @"DEBUG"; break; case DDLogFlagInfo: logLevel = @"INFO"; break; case DDLogFlagWarning: logLevel = @"WARN"; break; case DDLogFlagError: logLevel = @"ERROR"; break; case DDLogFlagVerbose: logLevel = @"VERBOSE"; break; default: logLevel = @"VERBOSE"; break; } static dispatch_once_t onceToken; static NSDateFormatter *logDateFormatter; dispatch_once(&onceToken, ^{ logDateFormatter = [[NSDateFormatter alloc] init]; logDateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS"; }); /// 此处可以使用其他APM日志收集工具 进行收集日志埋点 打印的时候区分一下level 后台查埋点的时候就可以进行区分查找 /// 从而更方便定位生产问题 return [NSString stringWithFormat:@"\n%@ *** [%@] %@ (line: %lu)\n*** %@\n", [logDateFormatter stringFromDate:logMessage.timestamp], logLevel, logMessage.function, logMessage.line, logMessage.message];} 本文作者: CoderWGB 本文链接: http://wangguibin/github.io/hexo-github-action/2024/11/27/iOS%E6%97%A5%E5%BF%97%E5%9F%8B%E7%82%B9%E6%96%B9%E6%A1%88/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!