Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active September 8, 2018 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopherjohnson/c674dde5c5b62a20e1875c743cf452a8 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/c674dde5c5b62a20e1875c743cf452a8 to your computer and use it in GitHub Desktop.
Simple Swift logging functions
import Foundation
extension String {
/// Return last path component.
public var lastPathComponent: String {
return (self as NSString).lastPathComponent
}
}
/// Write a message to the system log.
///
/// The message will be prepended with function/file/line information.
func log(_ message: String,
_ function: String = #function,
_ file: String = #file,
_ line: Int = #line) {
NSLog("\(function) (\(file.lastPathComponent):\(line)): \(message)")
}
/// Write an error message to the system log.
///
/// The message will be prepended with function/file/line information.
func log_error(_ message: String,
_ function: String = #function,
_ file: String = #file,
_ line: Int = #line) {
NSLog("ERROR: \(function) (\(file.lastPathComponent):\(line)): \(message)")
}
/// Write a warning message to the system log.
///
/// The message will be prepended with function/file/line information.
func log_warning(_ message: String,
_ function: String = #function,
_ file: String = #file,
_ line: Int = #line) {
NSLog("WARNING: \(function) (\(file.lastPathComponent):\(line)): \(message)")
}
@kristopherjohnson
Copy link
Author

kristopherjohnson commented Sep 3, 2018

Example use:

log("Hello, world!")

Log output:

2018-09-03 15:13:28.992075-0400 Hello[93427:6074998] main(args:) (Hello.swift:10) Hello, world!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment