SwiftyBeaver configuration: Configuring the timestamp

In this lesson

In this lesson, I’ll show you how to make SwiftyBeaver configuration changes to control if and how the timestamp appears for each log statement.


Tap on time to skip ahead


Hello everyone! In this lesson, I’m going to talk about configuring the timestamp on your logs. I’m here in the BaseDestination swift class in the SwiftyBeaver GitHub repo. And in previous lessons, I talked about some of these other configuration options. In this lesson I’m going to talk specifically about the date format. And we’ll learn how flexible that is and how you can alter this to make the date appear as you wish. Let’s start with the dateFormat option. And according to the comment here, the documentation that says “set this to an empty string to not log the date at all”. And you’ll notice that it provides a sensible default for us which is the year, month, day, hour, minute, second and millisecond of the timestamp when the log statement is logged.


To illustrate what that looks like, I’m in the AppDelegate of my sample application. And I’m in a function here where I’m configuring the ConsoleDestination for SwiftyBeaver. I have no specific configuration here, so it’s using all defaults which means each log statement, when my application runs, will include the full date as formatted per the default date format. That is the year, month, day, hour, minute, second and millisecond. So the first thing I’m going to show you is how to turn that off. So, as instructed in the documentation, the way to do that is to set the date format equal to an empty string. So I’m going to run this and we’ll see what it looks like.


As advertised, setting the date format to an empty string completely removes the timestamp from each log statement. Now this isn’t typically what we want. You always want to have some formatted timestamp in your log. And I’m going to show you how to do that next.


We can see here that SwiftyBeaver uses what may appear to be an odd collection of characters to format the timestamp. Actually. SwiftyBeaver uses the NSDateFormatter and I’m going to show you where you can find out what all is possible in your formatted timestamps.


I’m here at the Mac Developer Library which is an Apple site that contains documentation about all sorts of programming topics with Apple platforms. I’m going to scroll down here, where it says “Use Format Strings to Specify Common Formats”. This is what SwiftyBeaver actually uses. And depending upon the platform that you’re logging on, it uses a specific standardized date formatting structure. I’m using iOS 9.3 in this case, so the highest version of iOS is here and I’m going to click on this link and open it and show you what it looks like.


Clicking on that link takes me to another site where the date format patterns are documented. I’m going to scroll down to this table which you can view at your own convenience. The point that I want to make is that there are symbols that mean certain things. And what I’m going to do is change the format of the timestamp. And if you recall, it used a capital H to show the hour and that’s the 0 to 23 hour. I’m going to change that to a lowercase H. So we get only 1 through 12 and then I’m going to add the AM/PM designation, as well as time zone. So let me head back to the code and make that change.


I’ve copied in the default date format and I’m going to alter that slightly using the symbols that I found in the table. I’m going to add the AM/PM designator as well as the time zone. So let me run the app again and I’ll show you what impact this has on the timestamp in the logs.


Here in the console, in the log, you can see the format that the timestamp takes on is reflective of the change that I made in the dateFormat so you can see here. You can’t really tell because it’s not quite a 24 hour clock yet, but it’s 9:00 AM approximately and I’m in the central time zone. So very flexibly you can use the dateFormat configuration option for each console to format the date as you wish. 

Additional Info

Register to get access to additional resources and info.