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
- the name of the logger is not shown;
- 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 logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(levelname)8s %(name)s %(message)s') for name in ('blender_id', 'blender_cloud'): logging.getLogger(name).setLevel(logging.DEBUG) def register(): pass
The register
function is there just to stop Blender from nagging when it’s scanning for addons etc.
Conclusion
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!
I think this is among the most vital info for me. And i’m glad reading your article. But should remark on few general things, The web site style is great, the articles is really excellent : D. Good job, cheers|
Aw, this was a really good post. Taking the time and actual effort to generate a great article… but what can I say… I put things off a whole lot and never seem to get nearly anything done.
I like this web site very much, Its a really nice situation to read and incur information.
Good Afternoon:) I’m a financially strapped university student currently studying Geology at London Institute of Banking and Finance. I’m in the midst of starting work as an escort. Is it a good idea? Is it a good way of making money? I have already listed myself on https://glamourescorts69.com. Can anyone at blender.org suggest any upmarket escort agencies or directories? xox
Hello there, You’ve done a great job. I’ll certainly digg it and personally recommend to my friends. I’m sure they will be benefited from this site.
When someone writes an paragraph he/she retains the thought of a user in his/her mind that how a user can know it. Therefore that’s why this post is outstdanding. Thanks!
This post will help the internet visitors for building up new web site or even a weblog from start to end.
You made some nice points there. I did a search on the subject and found most people will consent with your website.
Impotence problems is the inability to get and keep an penile erection firm enough.
Having penile erection trouble every now and then isn’t necessarily a cause for concern. If erectile dysfunction is a continuing issue, however, it can cause stress, impact your self-confidence and make contributions to relationship problems. Challenges getting or keeping an erection can even be a sign of an underlying health condition that really needs treatment and a risk factor for heart disease.
hi!,I love your writing very so much! percentage we communicate extra approximately your post on AOL? I need a specialist in this house to unravel my problem. Maybe that is you! Taking a look forward to see you.
hello some one can tell me the code pyton of mouselock
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:
http://pasteall.org/pic/show.php?id=109477
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.
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. :)
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?
Thanks!
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!
Cheers,
Sybren
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.
In order to prevent spam, comments are closed 7 days after the post is published. Feel free to continue the conversation on the forums.