Logging in #Python is a popular topic and it's a really useful one to understand.

Today we are going to learn more about that!

๐Ÿงต๐Ÿ๐Ÿ‘‡
Python includes the `logging` module as a part of its standard library.

You can get started using it quite easily:
Python's `logging` module supports multiple logging levels:

๐Ÿ notset
๐Ÿ debug
๐Ÿ info
๐Ÿ warning
๐Ÿ error
๐Ÿ critical
You can set a logging level in #Python using `basicConfig()`
Normally, when using Python's `logging` module, you would create a logger object instead of using `logging` directly.

If you do that, then you call the `setLevel()` method to set the logging level
Once you have a logger level set in your logging object, you need to choose where you wish to log.

You do that by creating a logging handler.

One of the most common logging handler to use is `StreamHandler` which logs to stdout by default
Python's logging handler supports a lot of other logging handlers.

For example, you can log to an `SMTPHandler` which emails a log every time a log event happens.

Use this one with care!
For a full list of logging handlers, check out the Python documentation here:

It supports platform specific handlers, like the Windows Log, sys log and much, much more more!

docs.python.org/3/library/loggโ€ฆ
If you don't like the default logging format, you can change it with a `logging.Formatter()`

Here's an example of adding a formatter
For a full listing of the formatting types, called LogRecords, look to the Python documentation here:

docs.python.org/3/library/loggโ€ฆ
Python's logging module allows you to log to multiple handlers at once. For example, it's easy to log to a file and to stdout at the same time!

You can apply different formatters to different handlers too!
Some developers don't like coding up all that boilerplate code when working with the `logging` module

Fortunately, there are alternatives such as using a `dictConfig`:
You can also use a `fileConfig` to configure Python's logging module
I created a video tutorial about logging with Python on my YouTube channel:

I have several logging articles on @mousevspython too:

blog.pythonlibrary.org/page/2/?s=loggโ€ฆ
The code for these logging examples is up on my PyTips GitHub repo: github.com/driscollis/pytโ€ฆ
I hope you enjoyed learning about logging in #Python with me.

Follow me for more great information on the Python programming language

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

12 Nov
One of the harder concepts to learn in #Python are decorators.

So let's take a few moments and learn about them!

๐Ÿงต๐Ÿ๐Ÿ‘‡
It's always good to start with a regular function. Why?

Because you create a decorator using functions!

This function, `doubler()` takes in a number and doubles it. That's it! Image
But wait! #Python functions are also objects. You can get their `__name__` and their docstring (`__doc__`), if they have one.

You can also get a listing of their other methods with `dir()`

What all this means is that you can pass a function to another function! Image
Read 11 tweets
8 Nov
Did you know you can watermark images with #Python?

Find out how using the #Pillow package in this mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
If you want to add text, such as a URL, to an image, you can do that pretty easily with #Python and #pillow

The following code snippet is only 21 lines!
Here are the before and after photos of the Yequina lighthouse showing how to add a watermark with #Python
Read 5 tweets
8 Nov
Let's take some time and learn about testing in #Python today.

To kick things off, you'll learn about the `doctest` module!

๐Ÿงต๐Ÿ๐Ÿ‘‡
What is `doctest`? #Python's `doctest` modules looks for pieces of text in docstrings that look like REPL sessions.

Next, it executes them and verifies that the output in the docstring matches when the code is actually run

Check out the docs: docs.python.org/3/library/doctโ€ฆ
Here is an example of a #Python function with a docstring that could be used by the `doctest` module to test that function:
Read 8 tweets
8 Nov
Have you ever needed to kill โ˜ ๏ธ a subprocess in #Python?

Let's talk a little about that a little!

๐Ÿงต๐Ÿ๐Ÿ‘‡
I had a use-case where I would spin up a process using `subprocess` that I wanted to kill if it ran too long

One way to do that is to use #Python's `threading` module, which has a handy `Timer` class

In this example, the `Timer` class is set to die after 5 seconds
#Python's `subprocess` module has a `run()` function that includes a `timeout` parameter (starting in 3.5) that you can use as well.

Here's an example of how that would work:
Read 5 tweets
7 Nov
#Python 3 has had asynchronous capabilities since 3.4. So let's talk about that today!

๐Ÿงต๐Ÿ๐Ÿ‘‡
Your first stop when learning about async in #Python is the official documentation.

There you will learn about the following:

๐Ÿ The `asyncio` module
๐Ÿ The `async` and `await` keywords

docs.python.org/3/library/asynโ€ฆ
The #Python documentation has a nice "Hello World" example for async.

In this example, you see how to create an asynchronous function, sleep and print out some strings:
Read 10 tweets
7 Oct
I use a lot of #Python resource. Here are some of my favorite!

Yes, it's another thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
Mouse vs Python, which is my own blog, is a resource I use myself because I write it for my future self as well as for you.

I put what I am currently working on or neat code snippets on there that I think I will use

blog.pythonlibrary.org
If I need to know about a specific module in #Python, I still check the Python Module of the Week website by @doughellmann

pymotw.com/3/
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(