Python - Logging & Attaching | MUSE
In this video, you’ll learn how to use logging and program attachment features to efficiently debug your Python scripts running on an AMX MUSE controller. We’ll walk through generating log messages at different severity levels and show you how to view them in real time by attaching your program in Visual Studio Code. You’ll also see how to adjust logging levels to reveal more detailed output, helping you pinpoint issues faster. By the end, you’ll have the tools needed to monitor, troubleshoot, and refine your applications with confidence.
What You’ll Learn:
• Using logging in MUSE Python programs
• Generating log messages at different severity levels
• Attaching to a running program in VS Code
• Viewing real-time log output for debugging
• Adjusting logging levels for deeper troubleshooting insight
• Best practices for monitoring and debugging applications
Hello, and welcome to this video on creating log messages in your Python scripts. On MUSE controllers, things like stack trace or system errors are still gathered by the controller and reported to the logging system. However, information or errors that you would like to report from your script will need to utilize the program logger built into the context class.
To begin with, let’s take a look at the default script generated when we create a MUSE local program. The second line of the script uses the log object built into the context class to output an info level message to the standard output that says “Sample Python Program”.
To see what this looks like when it is printed out, let’s attach VSCode to the script and see its output. To do this, right-click on the name of the program that’s running on the controller and select Attach. This will automatically bring up the standard output in VSCode. You will see the message “Program attached, you will see outputs here.” to indicate it has succeeded in attaching to the script.
In addition, you will now see that the information beside the program name now says enabled/attached to show you which program you are attached to. Only one script can be attached at a time. This means that only log messages related to the attached program can be seen in the VSCode output section, but log messages from other scripts can still be seen in the Diagnostics -> Logs page of the controller or via the CLI.
Since this program outputs the “Sample Python Program” log message only when it boots up, let’s restart the program. To do this, click the restart button to the right of the program name. When the program restarts, you will see three different INFO messages appear in the output. In addition to the log message in our script, there are two other messages that say “received Python callback listener” and “Python context running…”. These two info messages are created by the MUSE controller itself on starting a Python script.
There are five different levels of messages that can be logged. They are info, warn, error, debug, and trace. Let’s see what each of those look like in code and in the output.
*** Add in code***
context.log.info('INFO message')
context.log.warn('WARN message')
context.log.error('ERROR message')
context.log.debug('DEBUG message')
context.log.trace('TRACE message')
******************
Notice that when we restarted the program, there are only three messages. The DEBUG and TRACE messages are missing. This is because the log level, by default, is set to INFO. How do we know that, how can we find that out? You can recall that parameter using context.log.level.
Let’s printout the log level before we printout our logs.
***Add in code***
context.log.info(f'Log level: {context.log.level}\r')
*****************
This command prints out a simple log message showing the Log level. Let’s restart the code and see what it says.
Just as we discussed earlier, the default value is set to INFO.
Now that we have a way to see what our log level is, we need a way to change the level so we can see all our log messages. Since context.log.level is a parameter, this is a simple case of a direct assignment. For this example, let’s set the log level to TRACE so we can see all the log messages.
***Add in code***
context.log.level = “TRACE”
*****************
With the log level set to TRACE, all our log messages are now visible in the output.
With this, you should be able to create any necessary messages to debug your code and give your program detailed messaging as it runs.