iOS日誌本地化 日誌重定向

2021-07-31 20:28:09 字數 2686 閱讀 9642

本文主要是給讀者分享技術的,如何儲存能控制台輸出的log,以便於查詢程式本身出現的問題。

我們要判斷,是不是模擬器

#if !(target_iphone_simulator)//真機

//連線xcode時可以從監視器中看日誌 沒連線時log日誌會輸出到檔案中,

[self redirectnslogtodocumentfolder];

nslog(@"真機");

#else//模擬器

nslog(@"模擬器");

#endif

我們還要接著判斷是不是真機連線了xcode,然後才開始進行日誌本地化檔案的生成

- (void)redirectnslogtodocumentfolder

uidevice *device = [uidevice currentdevice];

if([[device model] hassuffix:@"simulator"])

//將nslog列印資訊儲存到document目錄下的log資料夾下

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

bool fileexists = [filemanager fileexistsatpath:logdirectory];

if (!fileexists)

nsdateformatter *formatter = [[nsdateformatter alloc] init];

[formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"zh_cn"]];

[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; //每次啟動後都儲存乙個新的日誌檔案中

nsstring *datestr = [formatter stringfromdate:[nsdate date]];

self

// 將log輸入到檔案

freopen([self

.filepath cstringusingencoding:nsasciistringencoding], "a+", stdout);

freopen([self

.filepath cstringusingencoding:nsasciistringencoding], "a+", stderr);

//未捕獲的objective-c異常日誌

nssetuncaughtexceptionhandler (&uncaughtexceptionhandler);

}

這是乙個報錯的異常資訊,也就是我們所說的崩潰資訊

void uncaughtexceptionhandler(n***ception* exception)

//將crash日誌儲存到document目錄下的log資料夾下

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

if (![filemanager fileexistsatpath:logdirectory])

nsdateformatter *formatter = [[nsdateformatter alloc] init];

[formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"zh_cn"]];

[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];

nsstring *crashstring = [nsstring stringwithformat:@"<- %@ ->[ uncaught exception ]\r\nname: %@, reason: %@\r\n[ fe symbols start ]\r\n%@[ fe symbols end ]\r\n\r\n", datestr, name, reason, strsymbols];

//把錯誤日誌寫到檔案中

}else

//把錯誤日誌傳送到郵箱

nsurl *url = [nsurl urlwithstring:[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];

}

#if !(target_iphone_simulator)//真機

//連線xcode時可以從監視器中看日誌 沒連線時log日誌會輸出到檔案中,

[self redirectnslogtodocumentfolder];

nslog(@"真機");

#else//模擬器

nslog(@"模擬器");

#endif

return

yes;

}

本地化 日誌本地化

目錄 概要執行時日誌 國際化與本地化 定義你的本地化日誌資訊mymsg enum package org.skzr.logging basename charset utf 8 value org.skzr.logging.msglocallog public enum mymsg 定義國際化檔案o...

日誌 重定向

python 將終端 terminal 或者控制台的輸出結果輸出至 log 檔案 以檔案形式儲存 重定義 logger 類,然後 sys.stdout logger log檔名及路徑 import sys class logger object def init self,logfile defau...

iOS學習筆記 日誌重定向

我們在ios開發過程中,我們時常會使用nslog列印到控制台的日誌資訊進行 除錯,但這樣除錯的前提是連線上xcode。如果進行真機除錯但同時又不能連線xcode的時候,就不能直接在xcode的控制台檢視輸出日誌了,但是程式還是會執行那些log的,如果要檢視這些日誌,我們就需要把輸出日誌資訊儲存到檔案...