How to use slacklogger for easy application-level logging
Recently I started on a new Django project and wanted to track a few occasional events, like a new user registering for the app or when a user’s data was modified by a recurring task. My criteria:
- I wanted the logging solution to be simple to implement
- I wanted it to be compatible with any python framework
- I just wanted application-level logs
- I wanted a decent mobile experience
Most of the logging solutions I found on the market were overkill for my use case (ex: Loggly’s painfully outdated Django documentation). I didn’t want to use the default Django logging options either — I just needed something plug-and-play.
Initially, I just used the Slack API to send myself some logs. Simple right? But even Slack’s Python SDK is bloated for my use case. I could use the requests
library to send regular API calls, but that would still require some setup and formatting. To streamline things as much as possible — for this project and future ones — I created the slacklogger
package. You can find the GitHub page here.
It does as the name suggests— it logs app-level events in a Slack channel of your choice.
In this brief tutorial, I’ll walk you through installing, setting up, and using slacklogger
for your own application-level logging.
Getting Started
To get started, install slacklogger
with pip
(its sole dependencies are requests
and pytz
):
pip install simple-slacklogger
Once this is installed, you’ll need a few credentials — your Slack channel’s ID and a Slack app access token. Getting these is a pretty straightforward process:
- If you already have a Slack Workspace setup, great, move on to step 2, otherwise you can create a free account on the Slack homepage.
- Once you have the Workspace set up, head over to the Slack Apps page. Then click on Create New App, name your app, and choose your Workspace.
3. Now click on OAuth & Permissions in the left sidebar, and add the ‘chat:write.public’ OAuth Scope under the Bot Token Scopes section. This will also add the ‘chat:write’ scope.
4. On the same page, at the top, click the Install App to Workspace button. Click Allow on the next screen.
5. Almost done! Save the generated Bot User OAuth Access Token. Head over to the Slack homepage and launch your workspace in your browser.
6. Head to the channel where you want your logging app to post logs, and note the path value in the URL after the final ‘/’. It will look something like D05ZAYNAASL. Copy this as well.
Initiating slacklogger
Now you can use your channel ID and access token to initiate slacklogger
:
Using slacklogger
You can log application-level events with slacklogger
in two ways: using a decorator or a normal function. Let’s overview both.
Using the Decorator
The slacklogger.log
decorator wraps around any function and runs before your main function. For example:
message
is really the only required parameter, but you’ll want to add a few more options to be able to easily overview and search your Slack logs.
The above will output something like
The list of tags
are helpful for searching through your logs on Slack. You can also customize your level
and its color, along with the timezone
— we’ll take a look at this in a bit. First, let’s see how the normal function works.
Using the function
slacklogger.send_log
works much the same way as the decorator, except it doesn't automatically extract your function's name and script path — you can do that with an included helper function, my_details
, which takes the function's name as a parameter.
Note: if your function is in a class, and you’re using the
my_details
helper function, prepend your function's name withself
- for example,my_details(self.new_user)
.
slacklogger
Options
slacklogger
gives a bit of room for customization. You can define two settings after you enter your credentials: date_format
and level_colors
.
Date formatting and level colors
The date_format
setting refers to the format of your log’s date and time. The level_colors
setting allows you to define a dict
of level names and their corresponding hex color codes.
The above values are the default settings slacklogger
uses — so you can start using the above level names right away, and the date is already formatted. To create your own date formatting, check out this cheat sheet for strftime
directives.
slacklogger.settings["date_format"] = "%b %d, %H:%M"
For level_colors
, you can add any name and hex color code you need to slacklogger.settings["level_colors"]
- or overwrite the entire dict
. For example, we can add a new level (‘ntk’, or ‘Nice to Know’) and assign it a purple color (you can use color-hex.com to choose color hex codes for your levels).
slacklogger.settings["level_colors"]["ntk"] = "#730073"
Afterwards, we can use ‘ntk’ in a decorator or function.
The above should output something like this:
Timezone settings
Let’s wrap things up with the final customizable option: timezone
. To change the timezone, simply modify the timezone
parameter in either the decorator or the function. Check this Wiki link for relevant timezone names.
You should see something like this using the above code (notice that function_name
, script_path
, and tags
are omitted, as they're all optional):