Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 126.96.36.199 release ready!
Owner and Founder. Opencart
Hi Guyz , Today we will learn about “How to create logs in swift ?”.
Sometimes we need to solve a major issue at client end which got skipped in our testing. Thus if our client provides a log file regarding the issue then this would be very helpful for us.
For this purpose we need to provide a log generating application to the client that will generate the log file after a successful event.
We will create a SwiftLoggor class which will handle all the log generation process.
First copy the below code into your class.
We will also create a date extension like this
We will define our log cases like this.
Your class will look like this
Now lets dive into the code to see whats happening over here ,
dateFormat variable to represent the date format. You can create your own.
fileContent variable will store all your logs to be written in the log file.
A dateFormatter variable in which the locale and timezone are according to the device’s current locale and timezone.
The sourceFileName will provide the file name in which we are placing the log.
You need to know about these literals
The log function will take a log message and event of ErroTypes as parameters and will return a string which will include the above literal results like line and column number of the log , file and function name in which the log appears and date.
The writeToFile function allow us to write the log into a text file named “Log.txt” in our app’s documents directory.
Note:- Whenever we call writeToFile it will overwrite the previous generated log file. Thus try to call it once in the application with complete logs stored in a separate variable , here we are using fileContent.
The function getDocumentsDirectory will return the url of documents directory.
You can store all your logs into fileContent variable and then pass it to writeToFile function in your applicationWillTerminate function . inside Appdelegate.
Store your logs like this
to print your log use
Now in your applicationWillTerminate put this
Now you have all your logs into your Log file. Here are the steps to how to get your log file from the documents directory of your app.
Note:- Your app’s info.plist must include UIFileSharingEnabled key with its value as “Yes”.
that how you will get your log file.
Thats all guys enjoy! .
You can take detailed reference from this blog
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
The results provided were above our expectations both in deliverability timeline as well as operation. They are a top-notch company with many resources and a company we feel honoured to work with again!
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.