ios 將NSLog日誌重定向輸出到檔案中儲存
對於那些做後端開發的工程師來說,看LOG解Bug應該是理所當然的事,但我接觸到的移動應用開發的工程師裡面,很多人並沒有這個意識,查Bug時總是一遍一遍的試圖重現,試圖除錯,特別是對一些不太容易重現的Bug經常焦頭爛額。而且iOS的異常機制比較複雜,Objective-C的語言駕馭也需要一定的功力,做出來的應用有時候挺容易產生崩潰閃退。一遍一遍的用XCode取應用崩潰記錄、解析符號,通常不勝其煩,有時還對著解析出來的呼叫棧發呆,因為程式當時的內部狀態常常難以看明白,只能去猜測。
http://dingran.iteye.com/blog/1773573
http://blog.csdn.net/marujunyy/article/details/12005767
//
// ZJAppDelegate.m
// Example
//
// Created by laizhenjie on 14-5-7.
// Copyright (c) 2014年 Laizhenjie. All rights reserved.
//
#import "ZJAppDelegate.h"
@implementation ZJAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self redirectNSlogToDocumentFolder];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma mark - 使用者方法,將nslog的輸出資訊寫入到dr.log檔案中;
// 將NSlog列印資訊儲存到Document目錄下的檔案中
- (void)redirectNSlogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是NSData!
NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
// 先刪除已經存在的檔案
NSFileManager *defaultManager = [NSFileManager defaultManager];
[defaultManager removeItemAtPath:logFilePath error:nil];
// 將log輸入到檔案
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
@end
相關文章
- 將程式碼中的除錯資訊輸出到日誌檔案中除錯
- tee - 重定向輸出到多個檔案
- Linux 重定向把錯誤輸出到檔案中Linux
- Golang:將日誌以Json格式輸出到KafkaGolangJSONKafka
- 如何將rust日誌輸出到android終端RustAndroid
- log4j將日誌輸出到資料庫資料庫
- 『無為則無心』Python日誌 — 66、將日誌資訊儲存到檔案中Python
- filebeat 收集nginx日誌輸出到kafkaNginxKafka
- Log4j輸出到指定日誌檔案
- MySQL資料庫中的日誌檔案---(4)配置日誌檔案輸出路徑MySql資料庫
- (OAF)jdeveloper整合log4j並將日誌輸出到指定檔案並寫入資料庫Developer資料庫
- mysql 資料儲存檔案及6類日誌MySql
- python怎麼將列印輸出日誌檔案Python
- 轉儲日誌檔案頭
- linux重定向標準錯誤與標準輸出到同一檔案Linux
- 【整理】將Linux指令碼中的正常輸出,警告,錯誤等資訊輸出到檔案中Linux指令碼
- 如何將終端輸出的資訊重定向寫入檔案中呢?
- IOS資料儲存之檔案沙盒儲存iOS
- iOS 日誌重定向和異常捕獲iOS
- Linux伺服器常見的日誌檔案儲存位置!Linux伺服器
- 【儲存管理】日誌管理
- (定時)任務輸出重定向到日誌
- 捕獲NSLog日誌小記
- Matlab將.mat檔案儲存為.txt檔案Matlab
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- 想將多個伺服器的日誌輸出到一個地方怎麼破·?伺服器
- 如何將MAC的檔案儲存至NAS網路儲存?Mac
- oracle歸檔日誌儲存路徑的設定Oracle
- Object-C使用NSLog列印日誌Object
- oracle日誌檔案頭轉儲說明Oracle
- 通過Docker部署Java專案的日誌輸出到宿主機指定目錄DockerJava
- lumen cli日誌和普通日誌分開儲存
- 塊儲存 檔案儲存 物件儲存物件
- 【iOS資料儲存】iOS檔案系統介紹iOS
- 定時將系統時間更新在日誌檔案中
- 探究MySQL中的日誌檔案MySql
- 重做日誌檔案中的SCN
- 在SQL Server中儲存和輸出任意型別的檔案SQLServer型別