{"id":9223,"date":"2022-03-05T20:09:05","date_gmt":"2022-03-05T20:09:05","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9223"},"modified":"2022-03-05T20:09:05","modified_gmt":"2022-03-05T20:09:05","slug":"logging-in-swift","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/logging-in-swift\/","title":{"rendered":"Logging in Swift"},"content":{"rendered":"<p>Ofcourse we can use <em>print<\/em> statements to output messages but this is pretty basic, a preference is for a logging framework which allows us to log different levels of information, for example debug, trace, info, error etc.<\/p>\n<p>The documentation <a href=\"https:\/\/developer.apple.com\/documentation\/os\/logging\/generating_log_messages_from_your_code\" rel=\"noopener\" target=\"_blank\">Generating Log Messages from Your Code<\/a> shows that for macOS 11 and later to use the Logger structure. We can find a Logger implementation here <a href=\"https:\/\/github.com\/apple\/swift-log\" rel=\"noopener\" target=\"_blank\">apple swift-log<\/a>.<\/p>\n<p>We&#8217;ll need to update the Package.swift file in the Package dependencies as per the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.package(url: &quot;https:\/\/github.com\/apple\/swift-log.git&quot;, from: &quot;1.0.0&quot;),\r\n<\/pre>\n<p><em>Obviously change the version in the above to suit the latest available version.<\/em><\/p>\n<p>Next, add the following to the (in my case) executableTarget<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.product(name: &quot;Logging&quot;, package: &quot;swift-log&quot;)\r\n<\/pre>\n<p>To use the library in your code, declare a variable\/const, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet logger: Logger = Logger(label: &quot;com.putridparrot.EurekaService&quot;)\r\n<\/pre>\n<p>The label is output to the logger to distinguish the components which are logging. <\/p>\n<p>Now to log we simply use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlogger.info(&quot;Sending data&quot;)\r\n<\/pre>\n<p>The output would look like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n2022-03-05T16:39:52+0000 info com.putridparrot.EurekaService : Sending data\r\n<\/pre>\n<p>Out of the box the output is streamed to stderr, for example <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nLoggingSystem.bootstrap(StreamLogHandler.standardError)\r\n<\/pre>\n<p>Checkout the <a href=\"https:\/\/github.com\/apple\/swift-log\" rel=\"noopener\" target=\"_blank\">swift-log<\/a> repository for more information on alternate LogHandlers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ofcourse we can use print statements to output messages but this is pretty basic, a preference is for a logging framework which allows us to log different levels of information, for example debug, trace, info, error etc. The documentation Generating Log Messages from Your Code shows that for macOS 11 and later to use the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9223","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9223"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9223\/revisions"}],"predecessor-version":[{"id":9226,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9223\/revisions\/9226"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}