Logging from Python code in Blender

Logs In this article we take a look at using logging inside Blender. When things take an unexpected turn, which they always do, it is great to know what is going on behind all the nice GUIs and user-friendly operators. Python’s standard library with a very flexible and extendable logging module.

To familiarize yourself with Python’s logging module itself, we can recommend the Logging HOWTO by Vinay Sajip.

Using logging in your Python module

The general approach to logging is to use the module’s name as the logger name, so your module can have this at the top:

import logging

log = logging.getLogger(__name__)

Loggers use a hierarchy of dotted names, so if you want to create a sub-logger (for example to use in an operator), you can use log = logging.getLogger(__name__ + '.my_sublogger').

To log something, use one of the error, warning, info, or debug functions (for details, see the API documentation). Be careful which one you choose, though: errors and warnings are shown on the console by default, so they can be seen by anyone. Only use those sparingly, for those things you intend to be seen by end users.

The default configuration

We keep Blender’s Python as default and standard as possible. As a result, you can just grab a Python tutorial, apply it to Blender, and get the results you’d expect. This also means that the logging configuration is bog standard. This means that by default

  1. the name of the logger is not shown;
  2. only log entries at levels CRITICAL, ERROR, and WARNING are shown.

Point 1. is important for the way you write your log messages; you have to write sentences that can be understood without knowing their context (which is a good idea anyway). Point 2. tells you at which levels to log for developers/debugging (INFO and DEBUG), and at which levels to log for artists (WARNING and above). However, and this is important: never assume that people read the log file. Communication with the user of your code should happen through Blender’s GUI.

Configuring logging

Which logs you want to see, and which you don’t, is personal and depends on your needs of the moment. As such, we can’t ship Blender with a default other than Python’s default, as there won’t be one default that satisfies everybody. This also means that your addon should not configure the logging module.

To set up the logging module to your needs, create a file $HOME/.config/blender/{version}/scripts/startup/setup_logging.py (this is on Linux, but you’re likely a developer, so you’ll know where to find this directory). If the folder doesn’t exist yet, create it. I like to put something like this in there:

import logging

                    format='%(asctime)-15s %(levelname)8s %(name)s %(message)s')

for name in ('blender_id', 'blender_cloud'):

def register():

The register function is there just to stop Blender from nagging when it’s scanning for addons etc.


Now you know how to use logging, and how to configure Python’s logging module for your own personal preference. Enjoy not having to remove all those print() statements before you publish your addon!

7 comments 6,119 Views
  1. I write my debugging messages to sys.stderr, not sys.stdout. And I quite often leave them in production code, because it can be handy to keep an eye on what the code is up to.

    I wish Blender itself would be more consistent about writing messages to sys.stderr instead of sys.stdout.

    • With the logging module, you allow the user to choose which messages go where, including the choice between stderr and stdout. If you want more consistency, I’d suggest using the logging module, and updating existing addons to use that as well.

  2. Interesting post.
    I have two questions:

    1) Is there no way to configure the desired log level for our own addon, independently of other addons? For instance I want my addon to print on info level without affecting the other addons.

    2) By default logging only prints in the console and not to any text file? Is there a recommended way to log to a text file from Python in Blender?


    • Hi Arnaud. Good questions.

      1) Yes, I do this for the ‘blender_id’ and ‘blender_cloud’ addons, in the example above. You can of course use the module names of your own addons for this. That way you can get the level at the granularity you want.

      2) This is not specific to Blender, so you can simply add a handler that saves the log to a file as describe at https://docs.python.org/3/howto/logging.html#logging-to-a-file. If you want you can even add advanced features such as automatic log rotation based on age or size.

      Keep those questions coming!


  3. I was really hoping that this article was going to address the very old annoyance that when you open up Blender and switch to “Scripting” you’re presented with a text editor and a console that don’t talk to each other. When you make a mistake, Blender says “Python script fail, look in the console for now…” and you glance down at the panel that’s labeled “Console” and there’s nothing there.

    Yes, I know as a veteran that it means “the operating system console.”

    Sorry to rain on your parade. I just think that the console panel in blender could be a lot more useful. I had my hopes up that this article about logging would solve that.

    That said, I can see how logger is better than importing sys and using print(‘stuff’) or sys.stderr.write(‘stuff’) to monitor values. In the web development world, developers like me have grown rather accustomed to the console.log(), console.warn(), etc functions. All which seem pretty standard outputs until you look at the debug() function. I’ve been working on a complicated script that runs in a separate thread, and I think logger is really going to help with that. Thanks, sybren. 🙂

  4. I have been playing a bit with the logging system. The most important thing (for me) is that i can easily switch the logging level for each logger separately, regardless from where it comes. But i believe that many users do not want to fiddle with the configuration file, they just want to be able to set the log level “right now and here” when necessary.

    I added such a panel to our Addon preferences, but for demo purposes i also added the same Panel to the System preferences. In this panel you see the list of currently available loggers. For each logger you can select the current log level (Warning by default). Here is a screen shot:


    The Loggers in that panel are directly collected from the logging module using:

    logger_names = logging.Logger.manager.loggerDict.keys()

    So this panel should actually show all currently available named loggers which are active in any Addon in Blender.

    I propose to add one more user preferences tab dedicated to Logging only and add a panel similar to the one shown above plus maybe some more features which can be set without needing a configuration file.

  5. hello some one can tell me the code pyton of mouselock

  1. Leave a Reply

    Your email address will not be published. Required fields are marked *


share this story on