SwiftyBeaver configuration: Customizing level strings and colors

In this lesson

In this lesson, you will learn about some of the lesser-used SwiftyBeaver configuration options; customizing the level strings and colors.

Transcript

Tap on time to skip ahead

00:11

Hello everyone! In this lesson, I’m going to talk about some additional configuration options for SwiftyBeaver. And as I have in other lessons, I’m here in the BaseDestination swift class, on the SwiftyBeaver GitHub repo. And I’m covering these 2 configuration options. These are not very commonly used, but it does show you the level of customization you can achieve with SwiftyBeaver. I’m going to talk about the levelString, which is what’s used to print the text VERBOSE, DEBUG, INFO, WARNING and ERROR in the log. And you can customize that. You can even customize the colors. And I’ll touch on that next.

00:58

I’m over here in XCode in the AppDelegate of my sample application, in the function where I add the ConsoleDestination and you’ll notice that I’m not doing anything special. I’m just creating a ConsoleDestination, which means it uses all the default configuration options. Down here in the log, you’ll notice that each statement prints the level at which that statement was logged and you can customize this. So I’m going to paste in some changes where I can customize the level strings that will be generated in place of VERBOSE, DEBUG, INFO, WARNING and ERROR. I’m going to use levels 100, 200, 300, 400 or 500. You can specify whatever you like. But, just to show you how this works, let me run this and show you the results.

01:50

If we look in the log now, the individual text, the text for each of these individual statements has been replaced with the values that I configured up here. So while you might not do that very often, it is possible and you don’t have to change them all if you don’t like 1 or 2 of them, you can change them to whatever you like.

02:10

Next up is the level color. You’ll notice that SwiftyBeaver defaults this to a new LevelColor struct. There are preconfigured values for each of the logging levels for the colors. And so this uses standard ANSI color coding for color-coding your text in a terminal or a file. Verbose is set to specify so the first thing is whether it’s foreground or background and you can specify both or either or neither. So here, we’re specifying the foreground color to be RGB values; 150 for the red, 178 for the green and 193 for the blue. So you’ll notice that SwiftyBeaver uses silver for Verbose. Green for Debug, blue for Info, yellow for Warning and red for Error, so we’re going to change this up a little bit and I’ll you know show you how to use the setting to customize your colors.

03:12

Being the artistic genius that I am, I headed over to color.adobe.com because I want to use this tool to design some snazzy colors for each of the logging levels. So I’m starting here in the middle. You’ll notice I can specify the RGB values. And I’m going to paste in the color for each one of these and it’s going to generate shades of that color here. So the first color code is 150, 178, 193. And when I hit enter, you’ll notice that that generates shades of the silver. This is the base color. What I’m going to do is repeat this for each process. I’m going to use a background of the darkest and a foreground of the lightest and I’m going to come up with sets of colors using this tool and apply that to my color-coding so that I can have some snazzy colors for my various logging levels.

04:16

Admittedly, this is a little bit of an advanced use of the color-coding, but what I’ve done is I’ve gone through the process of using that tool to determine what I want to use for my background and my foreground colors, using colors similar to the defaults for SwiftyBeaver. So when you specify both, you have to insert this special code, which I’ll paste in the notes. But, this is a prefix that’s required to precede in the color definition. It’s already defaulted for the initial color by SwiftyBeaver, so if we specify two colors, we have to insert that prefix code. Nonetheless, I’m specifying both a background and a foreground color for each logging level. You can just specify one if you wish and if you do that you can omit all of this, for example. If you just want to do the foreground color, all you have to you do is specify that. If you just want to use the background color, then you do something like this. Nonetheless, you can see in the log here that I’ve been able to specify both the foreground and background color. If I do a quick search for WARNING, you’ll notice that it applies there to the WARNING is well. I don’t have any ERRORS, so I can’t show you that. You can see that I’ve been able to customize the colors using an advanced use of these color codes. Admittedly, these are not used that often, but you may find occasion where you want to customize the colors and/or the level strings.