Coder Social home page Coder Social logo

log-swift's Introduction

Log

Log provides a way to log stuff to the console and to file. It provides the method Log.print() which require minimal changes to your code because you probably already use Swift.print().

Example

This creates a file log output in the default logs directory (path/to/application-support-dir/Logs) with a name containing the current month and year (e.g. log-2018-10.txt for October 2018).

guard let dirURL = Logging.defaultLogsDirectoryURL() else {
	Swift.print("Logs directory not found")
	return
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "y-MM"

let fileName = "log-\(dateFormatter.string(from: Date()))"
let fileURL = dirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
let output = FileOutput(filePath: fileURL.path)
Logger.sharedInstance.addOutput(output)

Swift.print("Log file placed at \(fileURL.path)")

You can then start logging using Log.print().

Log.print("Ups, something went wrong")

The log file will look like this

2018-10-29 12:27:11.050 Ups, something went wrong

Attach to emails

If a user reports an issue with your app, you can attach the log files to an email like this.

The following example uses the last 2 log files and attaches them to a mail compose sheet.

let mailController = MFMailComposeViewController()
if let urls = Logging.fileURLs { // returns the log files sorted by the creation date
	let fileManager = FileManager()
	for url in urls.prefix(2) {
		guard let data = fileManager.contents(atPath: url.path) else {
			continue
		}
		// Attach log as text file
		mailController.addAttachmentData(data, mimeType: "text/plain", fileName: url.lastPathComponent)
	}
}

log-swift's People

Contributors

brutella avatar jvigneshcs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cdf1982

log-swift's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.