Today, tell me how to create your own log system.
First look at Xcode's own log (NSLog here)
System comes with NSLog printed information only simple time/project name/print content
Content is relatively simple, difficult to do classification management and write files or upload and so on.
Today we borrowed cocoalumberjack to customize our own log system, and the result is:
#1. Log information classification
#2. Time
#3. The class that generated the log
#4. The method name where log is generated
#5. Code is in the row
#6. Log Write file
Finally and implement log on to the background server
Okay, here's how it's going to come true.
First, let's look at the basic usage of cocoalumberjack.
Pod Import
Pod ' Cocoalumberjack '
Create a new PCH file and add:
#import <cocoalumberjack/cocoalumberjack.h>static const Ddloglevel ddloglevel = Ddlogleveldebug;
Ddloglevel can be set to:
Ddloglevelerror/ddloglevelwarning/ddloglevelinfo/ddlogdebug/ddlogleveloff
If set to debug level, Ddloglevelerror/ddloglevelwarning/ddloglevelinfo/ddlogleveldebug can generate logs
Info level, Ddloglevelerror/ddloglevelwarning/ddloglevelinfo can generate logs, Ddlogleveldebug cannot generate logs and cannot be displayed in the Xcode console
And so forth,
Ddlogleveloff levels are neither displayed nor generated logs
Finish the above two steps to complete the previous configuration work
Then the official code can be used directly:
[Ddlog Addlogger:[ddttylogger sharedinstance]; TTY = Xcode console[ddlog addlogger:[ddasllogger sharedinstance]; ASL = Apple System Logsddfilelogger *filelogger = [[Ddfilelogger alloc] init]; File loggerfilelogger.rollingfrequency = 60 * 60 * 24; Hour rollingfileLogger.logFileManager.maximumNumberOfLogFiles = 7; [Ddlog Addlogger:filelogger]; Ddlogverbose (@ "Verbose");DD Logdebug (@ "Debug");DD Loginfo (@ "Info");DD Logwarn (@ "Warn");DD logerror (@ "Error");
However, only the basic information of verbose/debug and other content can be printed.
Here we need to define our own style, to define our own Ddfilelogger Logfilemanager.
Directly on the code bar:
#import <Foundation/Foundation.h> @interface mycustomformatter:nsobject <DDLogFormatter> { int Loggercount; NSDateFormatter *threadunsafedateformatter;} @end
#import "MyCustomFormatter.h" @implementation mycustomformatter-(ID) init {if (self = [super init]) { Threadunsafedateformatter = [[NSDateFormatter alloc] init]; [Threadunsafedateformatter setdateformat:@ "Yyyy/mm/dd HH:mm:ss:SSS"]; } return self;} -(NSString *) Formatlogmessage: (Ddlogmessage *) logmessage {nsstring *loglevel; Switch (logmessage->_flag) {Case ddlogflagerror:loglevel = @ "Error"; Break Case ddlogflagwarning:loglevel = @ "Warning"; Break Case ddlogflaginfo:loglevel = @ "Info"; Break Case ddlogflagdebug:loglevel = @ "Debug"; Break Default:loglevel = @ "Default"; Break } nsstring *dateandtime = [Threadunsafedateformatter stringfromdate: (Logmessage->_timestamp)]; NSString *logmsg = logmessage->_message; return [NSString stringwithformat:@ "[%@%@] \n%@", LogLevel, DateAndTime, logmsg];} -(void) Didaddtologger: (ID ≪ddlogger>) Logger {loggercount++; Nsassert (Loggercount <= 1, @ "This logger isn ' t thread-safe");} -(void) Willremovefromlogger: (ID <DDLogger>) logger {loggercount--;} @end
Then configure the Cocoalumberjack in the following ways
Configuration Cocoalumberjack [Ddlog addlogger:[ddasllogger sharedinstance];//add log to Apple System Logs [Ddlog Addlogger:[ddttylogger Sharedinstance]]; Add log to Xcode console [Ddttylogger sharedinstance].logformatter = [[Mycustomformatter alloc] init]; Custom logfile path ddlogfilemanagerdefault *logfilemanager = [[Ddlogfilemanagerdefault alloc] Initwithlogsdirectory:[nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject ]]; Ddfilelogger *customfilelogger = [[Ddfilelogger alloc] initwithlogfilemanager:logfilemanager]; Customfilelogger.rollingfrequency = * *; CustomFileLogger.logFileManager.maximumNumberOfLogFiles = 7; [Ddlog Addlogger:customfilelogger];
But it's just finished customizing the log format you want and customizing the path.
Can not be uploaded online, if there are two ways to upload:
1> get the log file address directly, upload
2> Create a management class to unify the closing and enabling of the management log
How to get the log file address:
CustomFileLogger.currentLogFileInfo.filePath;
Recommend a second
ios-to create your own log system