Log
In the development process, it is well known that the key part of logging debugging, especially when the product release, there are users feedback some crashes or other problems, the log is particularly important, through the analysis of logs can quickly find the crux of the problem and quickly solve the problem.
Proper logging of user logs is an art. What information should be written to the log (usually including user behavior and error messages, separate records), the information written to the log is too small for debugging, and frequent logging can affect the performance of the system, and can cause the log file to expand rapidly, making it difficult to find the information needed. For different applications, the information that should be logged is not necessary, but there are some general rules. For the log engine, there are a few things to note:
1. In a development environment, logs should be written to the console, and in a production environment, the log should be written to a file. When you debug your code, you won't be able to see the logs in Xcode without outputting to the console. The best way to do this is to write both the console and the log file.
2, should be divided into a variety of different log levels (errors, warnings, information, details).
3. When a log level is disabled, the call cost of the corresponding log function is very small.
4. When writing logs to the console or file, it is not possible to block the caller thread.
5. To periodically delete log files to avoid full disk.
6, the call of the log function is very convenient, usually using the C syntax to support the argument, the use of OBJECT-C syntax is not recommended. The invocation of NSLog is usually very simple, which is worth learning.
When adding a log, you should think about what this log is for, and whether the data recorded in this statement has been recorded elsewhere. Do not waste computer resources for content that is not sure to be recorded.
Needless to say, the error message must be logged into the log file. One thing to emphasize here is that the assertion (Nsassert) is also logged into the log file instead of directly crashing the program (the assertion should precede the program crash Code)
Example:
This will cause the assertion to fail, even if the shutdown assertion program will still crash. So change the code to the following:
It's much better to record the logs before the Nsassert crashes. Of course, you can rewrite the Nsassert, such as: Mynsassert, the logging code and Nsassert package together to use, so more convenient, recommended to use.
about recording sensitive information
Logging often involves privacy issues, and careful consideration should be given to which log information should not be logged, such as the user's username and password, or the credit card number and password. Do not forget that the purpose of logging is just to make it easy to reproduce and navigate to the wrong location when the program is wrong, that's all.
Get log file
If you can't get a log file, that log is useless. The Get log allows users to upload logs to the server via a network protocol. Also note that the log file may be larger and should be compressed before uploading to reduce the size. Considering the user traffic situation, it is best to silently upload the log file in the case of WIFI.
when to upload logs
Non-program crashes in the log, preferably in the user's leisure time and WIFI situation upload, and the crash of the log, considering that the program in the crash will be in a strange and unknown state, it is best to choose to restart the program at the time (not during the crash) upload crash report. Try not to do anything during a program crash.
The log log of the Bee framework used in the project is well written, and it is directly posted to everyone to learn:
___ ___ ______///\ __ \/\ ___\/\ ___//\ \ __< \ \ __\_ \ __\_//\ \_____\ \ \_____\ \ \__ ___//\/_____/\/_____/\/_____///////copyright (c) 2013-2014, {Bee} open source community//http://www.bee-framework.co M//////permission is hereby granted, free of charge, to all person obtaining a//copy of this software and associated Docum Entation files (the "Software"),//to deal in the software without restriction, including without limitation//the rights to Use, copy, modify, merge, publish, distribute, sublicense,//and/or sell copies of the software, and to permit persons to Whom The//software is furnished to does so, subject to the following conditions:////the above copyright notice and this perm Ission notice shall be included in//all copies or substantial portions of the Software.////the software are provided "as is ", without WARRANTY of any KIND, EXPRESS or//implied, including and not LIMITED to the warranties of Merchantability,//fit NESS for A particular PURpose and Noninfringement. In NO EVENT shall the//authors or COPYRIGHT holders is liable for any CLAIM, damages OR other//liability, WHETHER in an AC tion of contract, TORT or OTHERWISE, Arising//from, out of or in CONNECTION with the software or the use or other dealings In the software.//#import "bee_precompile.h" #import "bee_singleton.h" #pragma mark-typedef enum{beeloglevelnone= 0, beeloglevelinfo= 100,beeloglevelperf= + 1,beeloglevelprogress= + 2,beeloglevelwarn= 200,BeeLogLevelError= 300} beeloglevel; #pragma mark-#undefCC # define CC (...) [[Beelogger sharedinstance] Level:beeloglevelnone format:__va_args__]; #undefINFO # define INFO (...) [[Beelogger sharedinstance] Level:beeloglevelinfo format:__va_args__]; #undefPERF # define PERF (...) [[Beelogger sharedinstance] Level:beeloglevelperf format:__va_args__]; #undefWARN # define WARN (...) [[Beelogger sharedinstance] Level:beeloglevelwarn format:__va_args__]; #undefERROR # define ERROR (...) [[Beelogger Sharedinstance] Level:beelogleveLerror format:__va_args__]; #undefPROGRESS # define PROGRESS (...) [[Beelogger sharedinstance] level:beeloglevelprogress format:__va_args__]; #undefVAR_DUMP # define VAR_DUMP (__obj) [[ Beelogger Sharedinstance] level:beeloglevelnone format:[__obj description]]; #undefOBJ_DUMP # define OBJ_DUMP (__obj) [[ Beelogger Sharedinstance] Level:beeloglevelnone format:[__obj objecttodictionary]]; #undefTODO # define TODO (desc, ...) #pragma mark-@interface beebacklog:nsobject@property (nonatomic, assign) Beeloglevellevel; @property (Nonatomic, Retain) NSDate *time; @property (nonatomic, retain) nsstring *text; @end #pragma mark-@interface beelogger:nsobjectas_sin Gleton (Beelogger); @property (nonatomic, assign) boolenabled; @property (nonatomic, assign) Boolbacklog; @property ( Nonatomic, retain) Nsmutablearray *backlogs; @property (nonatomic, assign) nsuintegerindenttabs;-(void) toggle;-(void) enable;-(void) disable;-(void) indent;-(void) indent: (Nsuinteger) tabs;-(void) unindent;-(void) Unindent: (Nsuinteger ) Tabs;-(void) Level: (Beeloglevel) level format: (NSString *) format, ...; -(void) level: (Beeloglevel) level format: (NSString *) format args: (va_list) args; @end #pragma mark-#if __cplusplusextern "C" {#endifvoid beelog (NSString * format, ...); #if __cplusplus}; #endif
___ ___ ______///\ __ \/\ ___\/\ ___//\ \ __< \ \ __\_ \ __\_//\ \_____\ \ \_____\ \ \__ ___//\/_____/\/_____/\/_____///////copyright (c) 2013-2014, {Bee} open source community//http://www.bee-framework.co M//////permission is hereby granted, free of charge, to all person obtaining a//copy of this software and associated Docum Entation files (the "Software"),//to deal in the software without restriction, including without limitation//the rights to Use, copy, modify, merge, publish, distribute, sublicense,//and/or sell copies of the software, and to permit persons to Whom The//software is furnished to does so, subject to the following conditions:////the above copyright notice and this perm Ission notice shall be included in//all copies or substantial portions of the Software.////the software are provided "as is ", without WARRANTY of any KIND, EXPRESS or//implied, including and not LIMITED to the warranties of Merchantability,//fit NESS for A particular PURpose and Noninfringement. In NO EVENT shall the//authors or COPYRIGHT holders is liable for any CLAIM, damages OR other//liability, WHETHER in an AC tion of contract, TORT or OTHERWISE, Arising//from, out of or in CONNECTION with the software or the use or other dealings In the software.//#import "bee_log.h" #import "bee_unittest.h" #import "bee_sandbox.h" #import "nsarray+ BeeExtension.h "//----------------------------------//Source code//----------------------------------#pragma mark -#undefMAX_BACKLOG # define Max_backlog (#pragma mark-@implementation beebacklog@synthesize level = _level;@ Synthesize time = _time; @synthesize Text = _text;-(id) init{self = [Super Init];if (self) {self.level = Beeloglevelnone;s Elf.time = [NSDate date];self.text = nil;} return self;} -(void) Dealloc{self.time = Nil;self.text = nil;[ Super Dealloc];} @end #pragma mark-@interface Beelogger () {bool_enabled; Bool_backlog; Nsmutablearray *_backlogs; Nsuinteger_indenttabs;} @end #pragma mark-@implementation BeeloggerDef_singleton (Beelogger); @synthesize enabled = _enabled; @synthesize backlog = _backlog; @synthesize backlogs = _backlogs ; @synthesize Indenttabs = _indenttabs;-(id) init{self = [Super Init];if (self) {self.enabled = Yes;self.backlog = Yes;sel F.backlogs = [Nsmutablearray array];self.indenttabs = 0;} return self;} -(void) Dealloc{self.backlogs = nil;[ Super Dealloc];} -(void) toggle{_enabled = _enabled? No:yes;} -(void) enable{_enabled = YES;} -(void) disable{_enabled = YES;} -(void) indent{_indenttabs + = 1;} -(void) indent: (nsuinteger) Tabs{_indenttabs + = tabs;} -(void) unindent{if (_indenttabs > 0) {_indenttabs-= 1;}} -(void) Unindent: (Nsuinteger) Tabs{if (_indenttabs < tabs) {_indenttabs = 0;} Else{_indenttabs-= tabs;}} -(void) level: (Beeloglevel) level format: (NSString *) format, ... {#if (__on__ = = __bee_log__) if (nil = = Format | | NO = = [Format iskindofclass:[nsstring class]]) return;va_list args;va_start (args, format); [Self level:level format:format args:args];va_end (args); #endif#if (__on__ = = __bee_log__)}-(void) Level: (Beeloglevel) level format: (NSString *) format args: (va_list) args{#if (__on_ _ = = __bee_log__) if (NO = = _enabled) return;//formattingnsstring * prefix = nil;if (Beeloglevelinfo = level) {prefix = @ "INFO";} else if (Beeloglevelperf = = level) {prefix = @ "PERF";} else if (Beeloglevelwarn = = level) {prefix = @ "WARN";} else if (Beeloglevelerror = = level) {prefix = @ "ERROR";} if (prefix) {prefix = [nsstring stringwithformat:@ "[%@]", prefix];p refix = [prefix stringbypaddingtolength:8 withstring: @ "" startingatindex:0];} nsmutablestring * tabs = nil; nsmutablestring * Text = nil;if (_indenttabs > 0) {tabs = [nsmutablestring string];for (int i = 0; i < _indenttabs ; ++i) {[tabs appendstring:@ "\ t"];}} Text = [nsmutablestring string];if (prefix && prefix.length) {[Text Appendstring:prefix];} if (tabs && tabs.length) {[Text Appendstring:tabs];} if (beeloglevelprogress = = level) {nsstring *name = [Format stringbypaddingtolength:32 withstring:@ "" startingatindex:0]; NSString *state = Va_arg (args, NSString *); [Text appendformat:@ "%@\t\t\t\t[%@]", name, State];} else{nsstring * content = [[[NSString Alloc] Initwithformat: (NSString *) format Arguments:args] autorelease];if (content & amp;& content.length) {[Text appendstring:content];}} if ([text rangeofstring:@ "\ n"].length) {[Text replaceoccurrencesofstring:@ "\ n" withstring:[nsstring stringWithFormat : @ "\n%@", tabs? Tabs: @ "\t\t"] Options:nscaseinsensitivesearch range:nsmakerange (0, Text.length)];} Print to consolefprintf (stderr, [text utf8string], null), fprintf (stderr, "\ n", null);//Back Logif (_backlog) {Bee Backlog * log = [[[Beebacklog alloc] init] Autorelease];log.level = Level;log.text = text; [_backlogs Pushtail:log]; [_backlogs Keeptail:max_backlog];} #endif//#if (__on__ = = __bee_log__)} @endextern "C" void Beelog (NSString * format, ...) {#if (__on__ = = __bee_log__) if (nil = = Format | | NO = = [Format iskindofclass:[nsstring class]]) Return;va_List Args;va_start (args, format); [[Beelogger sharedinstance] level:beeloglevelinfo format:format args:args];va_end (args); #endif//#if (__on__ = = __BEE_ log__)}//----------------------------------//Unit test//----------------------------------#if defined (__bee_ unittest__) && __bee_unittest__test_case (Beelog) {times (3) {here ("output log", {CC (nil); CC (@ ""); CC (@ "format%@", @ "");}); Here ("Test Info", {info (nil); info (nil, nil); info (nil, @ "format%@", @ ""); info (@ "A", nil); info (@ "A", @ ""); INFO (@ "A", @ "format%@", @ "");}); Here ("Test warn", {warn (nil); WARN (nil, nil); WARN (Nil, @ ""); WARN (Nil, @ "format%@", @ ""); WARN (@ "a", nil); WARN (@ "A", @ ""); WARN (@ "A", @ "format%@", @ "");}); Here ("Test Error", {error (nil); ERROR (nil, nil); ERROR (Nil, @ ""); ERROR (Nil, @ "format%@", @ ""); ERROR (@ "a", nil); ERROR (@ "A", @ ""); ERROR (@ "A", @ "format%@", @ "");});} test_case_end#endif//#if defined (__bee_unittest__) && __bee_unittest__
It's a pity that the log log of the Bee framework is not written directly to the sandbox, so let's add it ourselves. It is important to remember to periodically clear unused or expired log files (you can choose to delete the logs in the sandbox after uploading to the server first).
Log Analysis
Third-party statistics are a good choice in order to get instant access to the user's crash log and record the user's behavior. Here's an example of a friend League statistic:
As can be seen from the above picture, the crash information of the log is very detailed, including the error summary , version information, the number of errors and the time of occurrence, the most important thing is to record the crash when the stack information , so the error is much easier to find.
For some more difficult to understand error summary, such as: Application received signal SIGSEGV, it is necessary to use the version code and the error occurred. Dysm file symbolic positioning to the place where the error occurred, the specific method will not be detailed, you can look at here, or Google a keyword.