{"id":2550,"date":"2016-01-23T23:09:45","date_gmt":"2016-01-24T05:09:45","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=2550"},"modified":"2016-06-05T15:13:54","modified_gmt":"2016-06-05T21:13:54","slug":"another-look-at-swift-logging","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/another-look-at-swift-logging\/","title":{"rendered":"Another Look at Swift Logging"},"content":{"rendered":"<p><a href=\"https:\/\/swift.org\/\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-3.0-orange.svg?style=flat\" alt=\"Swift 3.0\" \/><\/a> <a href=\"https:\/\/opensource.org\/licenses\/ISC\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/License-ISC-blue.svg?style=flat\" alt=\"ISC\" \/><\/a><\/p>\n<p>Shortly after Apple released Swift a number of folks provided logging libraries, as the venerable <a href=\"https:\/\/github.com\/CocoaLumberjack\/CocoaLumberjack\">Lumberjack<\/a> didn&#8217;t work out of the box.  We even provided a little write-up on using <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/logging-and-build-configurations-with-swift\/\"><code>swiftlog<\/code><\/a>, a collection of simple logging routines.<\/p>\n<p>Well, we&#8217;re back with an update to <code>swiftlog<\/code> that focuses on providing a quick way to add log messages to Swift applications on Linux systems.  The new features include:<\/p>\n<ul>\n<li>SwiftPM-ready\n<li>color-coded log messages using the awesome <a href=\"https:\/\/github.com\/onevcat\/Rainbow\">`Rainbow`<\/a> package\n<li>ability to write log messages to a file\n<\/ul>\n<p>As with all incarnations of <code>swiftlog<\/code>, we are not looking to develop the Cadillac of logging frameworks.  <code>swiftlog<\/code> is a very simple and no frills package.<\/p>\n<h3>Using `swiftlog`<\/h3>\n<p>To use the new <code>swiftlog<\/code> just add it to your <code>Package.swift<\/code> dependencies:<\/p>\n<pre class=\"lang:swift\">\nimport PackageDescription\n\nlet package = Package(\n    name:\"example\",\n    dependencies:[\n      .Package(url:\"https:\/\/github.com\/iachievedit\/swiftlog\", majorVersion:1)\n    ]\n)\n<\/pre>\n<p>You can begin using the logging facility by importing <code>swiftlog<\/code> and setting the <code>sLogLevel<\/code> global variable (it defaults to <code>.None<\/code> which turns logging off).<\/p>\n<pre class=\"lang:swift\">\nimport swiftlog\n\nslogLevel = .Verbose\n\nSLogVerbose(\"A verbose log\")\nSLogInfo(\"An info log\")\nSLogWarning(\"A warning log\")\nSLogError(\"An error log\")\n<\/pre>\n<p>All logs equal to or <i>higher<\/i> in severity are printed.  Logs are color-coded as well:<\/p>\n<figure id=\"attachment_2566\" aria-describedby=\"caption-attachment-2566\" style=\"width: 564px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/01\/Untitled-window_011.png\" rel=\"attachment wp-att-2566\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/01\/Untitled-window_011.png\" alt=\"Colored Logs!\" width=\"564\" height=\"340\" class=\"size-full wp-image-2566\" \/><\/a><figcaption id=\"caption-attachment-2566\" class=\"wp-caption-text\">Colored Logs!<\/figcaption><\/figure>\n<p>It doesn&#8217;t take a rocket surgeon to determine the options available for <code>sLogLevel<\/code>, but for completeness here they are:<\/p>\n<ul>\n<li>`.None`\n<li>`.Verbose`\n<li>`.Info`\n<li>`.Warning`\n<li>`.Error`\n<\/ul>\n<p>Two additional routines are provided:  <code>ENTRY_LOG<\/code> and <code>EXIT_LOG<\/code>.  These &#8220;macros&#8221; are designed for tracing function entry and exits in your logs:<\/p>\n<pre class=\"lang:swift\">\nimport swiftlog\n\nfunc multiply(_ multiplicand:Int, multiplier:Int) -> Int {\n  ENTRY_LOG()\n  let result = multiplicand * multiplier\n  EXIT_LOG()\n  return result\n}\n\nslogLevel = .Verbose\n\nSLogVerbose(\"A verbose log\")\nSLogInfo(\"An info log\")\nSLogWarning(\"A warning log\")\nSLogError(\"An error log\")\n\nSLogVerbose(\"10 times 10 equals \\(multiply(10, multiplier:10))\")\n<\/pre>\n<p>This results in three additional log messages:<\/p>\n<p><font color=\"green\"><\/p>\n<pre class=\"crayon:false\">\nVERBOSE - ENTRY multiply(_:multiplier:)\nVERBOSE - EXIT  multiply(_:multiplier:)\nVERBOSE - 10 times 10 equals 100\n<\/pre>\n<p><\/font><\/p>\n<p>Finally, we can instruct that logs also be written to a file with <code>slogToFile(atPath:)<\/code>.<\/p>\n<pre class=\"lang:swift\">\nimport swiftlog\n\nfunc multiply(_ multiplicand:Int, multiplier:Int) -> Int {\n  ENTRY_LOG()\n  let result = multiplicand * multiplier\n  EXIT_LOG()\n  return result\n}\n\nslogLevel = .Verbose\nslogToFile(atPath:\"\/tmp\/log.txt\", append:true)\n\nSLogVerbose(\"A verbose log\")\nSLogInfo(\"An info log\")\nSLogWarning(\"A warning log\")\nSLogError(\"An error log\")\n\nSLogVerbose(\"10 times 10 equals \\(multiply(10, multiplier:10))\")\n<\/pre>\n<p>The <code>append<\/code> parameter of <code>slogToFile(atPath:)<\/code> can be set to <code>false<\/code> to overwrite the previous contents of the file.<\/p>\n<h3>The Code<\/h3>\n<p>You can grab the code on <a href=\"https:\/\/github.com\/iachievedit\/swiftlog\">Github<\/a>.  If you don&#8217;t like the colors I chose for the log levels, hack away!  Perhaps a new revision will provide for more customization and options; I&#8217;ve deliberately chosen thus far to make this package very simple to use and reduced the number of knobs.<\/p>\n<p>There is an example in the <code>logexample<\/code> directory of the repository.  Enter <code>logexample<\/code> and type <code>swift build<\/code> to build and run!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shortly after Apple released Swift a number of folks provided logging libraries, as the venerable Lumberjack didn&#8217;t work out of the box. We even provided a little write-up on using swiftlog, a collection of simple logging routines. Well, we&#8217;re back with an update to swiftlog that focuses on providing a quick way to add log [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,5],"tags":[],"class_list":["post-2550","post","type-post","status-publish","format-standard","hentry","category-linux","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2550"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=2550"}],"version-history":[{"count":24,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2550\/revisions"}],"predecessor-version":[{"id":2971,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2550\/revisions\/2971"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=2550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=2550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=2550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}