#100DaysOfWeb in Python Transcripts
Chapter: Days 77-80: Twitter and Slack bots
Lecture: Turn on logging

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Logging is a robust standard library module in Python. It's one of those examples where Python comes with batteries included, in that you have
0:11 all this great functionality out of the box. I started using it recently, on the karma bot and it has been very useful
0:19 especially when stuff runs from mode you're not watching the console. Logging writes all the stuff you want
0:27 to a file and you can then later inspect that. So it's very easy to set up. It's just this logging basic config.
0:34 Which I will demo in our script in a bit. And then you can send logging events like this and based on priority. So this is an info event
0:44 but you can also send debug or error based on the level or urgency of the event. So here we have an info retrieving a file
0:54 here we use logging again, to send every thing that's posted to Slack to the log file. There's a nice how-to on the documentation page
1:05 that's worth reading through and it explains logging pretty well. Back to the script, this is done. Let's first define the remaining constants
1:17 we will need.
1:35 All right, generated URL that's the URL we're going to scrape. It has a variable username and project which you will fill in with the format command.
1:45 Help text that's for arg parse that's when we use the script from command-line. To show a title, so the purpose how to tweak your 100 days progress.
1:57 And this is for the logging so this is the format of the log message. So it will have the time, the name of the module the level of the message
2:07 so debug, info, error, etc. and the message itself. Check out the logging how-to and the change in the format of displaced messages section
2:16 for more information. I'm going to write the logs to bot.log and I have a today constant. Now let's set up the logging.
2:28 And this snippet you can just pop into any project. I mean, you give it a log file you have logging out of the box. It's pretty neat.
2:44 All right, level means the starting level. So everything from debug which is the lowest level up to info error. So basically log everything.
2:54 The format we defined here. The date format, hour minute seconds as per date time formatting. Log file, defined here. And this is important, append.
3:07 File mode append, 'cause otherwise you will wipe out the previous log file. Let's see if it works. All right. That seems good.
3:24 A bot log file got created. And here you see debug info error. Awesome. So we got logging enabled. In the next video, we're going to get
3:40 the daily progress tweet from the challenge create URL using requests and BeautifulSoup.


Talk Python's Mastodon Michael Kennedy's Mastodon