Logging
Logging is useful during Skill development, as well as to help end-users diagnose problems in the future.
To track events and data within your Skill we can use logging. If you are new to programming, this is a way to output a message that can tell you the state of your Skill at a particular point in time, details about an error that has occured, or simply noting that a program reached a particular point in the code.

Video Tutorial

Video Tutorial: Logging from Mycroft Skills

Basic Usage

A logger is available through the MycroftSkill base class. This means that you can use it within a Skill without needing to import the logging package. You can simply call self.log from within the class of your Skill.
Here is a quick example of an INFO level log message used in a Skill. We will learn more about the other levels shortly.
1
from adapt.intent import IntentBuilder
2
from mycroft import MycroftSkill, intent_handler
3
4
class LoggingSkill(MycroftSkill):
5
6
@intent_handler(IntentBuilder('HelloWorldIntent')
7
.require('HelloWorld'))
8
def handle_hello_world_intent(self, message):
9
""" Skills can log useful information. These will appear in the CLI and
10
the skills.log file."""
11
self.log.info("This is an info level log message.")
12
self.speak_dialog("hello.world")
13
14
def create_skill():
15
return LoggingSkill()
Copied!

What is included in a log message?

The above Skill would log an INFO level message each time the intent handler was triggered. The resulting log would look like this:
1
2021-03-19 16:06:43.731 | INFO | 14745 | HelloWorldSkill | This is an info level log message.
Copied!
From left to right, this consists of:
    A timestamp identifying when the log was emitted.
    The level of the log message
    The Process ID (PID) of the system process where the log originated.
    The origin of the log from within Mycroft. In the case of Skills the Class name is used.
    The log message passed as an argument to the logger.

Where do these messages get logged?

Log messages from a Skill are displayed in the Mycroft CLI so that a User can see in real-time what is happening in the Skill. They are also written to the skills.log file located at: /var/log/mycroft/skills.log
By default all INFO, WARNING, ERROR, EXCEPTION and CRITICAL level messages will be logged. DEBUG level messages will be logged if the User explicitly requests it. This can be done by issuing the :log level debug command in the CLI, or changing the log_level attribute in the mycroft configuration.
When you first turn on DEBUG level logging, you will quickly notice that there is a lot happening behind the scenes in Mycroft. If you are debugging a particular Skill it is very useful to limit the displayed log messages using the Class name of your Skill. For the HelloWorldSkill we would use the CLI command: :find HelloWorldSkill
Finally to return to the default INFO level logging, you can issue the :log level info CLI command.

Logging Levels

There are five types of log messages available that are used for different purposes.

DEBUG

1
self.log.debug()
Copied!
Debug messages are used for information that will help to diagnose problems. These are particularly useful if there is anything that has the potential to break in the future.
By default these messages will not be logged unless the User has explicitly turned on debug level logging.

INFO

1
self.log.info()
Copied!
Info messages provide general information when the Skill is running as expected. These messages will always be logged so are useful when actively developing a Skill. When preparing to publish a Skill to the Marketplace, you will likely want to convert many of these to the DEBUG level.

WARNING

1
self.log.warning()
Copied!
Warning messages are used to indicate that something has gone wrong, but the Skill will continue to function.

ERROR

1
self.log.error()
Copied!
Error messages indicate that a serious problem has occured and the Skill will not be able to function. In the Mycroft CLI these messages are shown in red to make them highly visible.

EXCEPTION

1
self.log.exception()
Copied!
Exception messages are an extended form of the error level message. These messages include a stack trace and should only be called from an exception handler. For example:
1
try:
2
1/0
3
except ZeroDivisionError as e:
4
self.log.exception("Cannot divide by zero")
Copied!

CRITICAL

1
self.log.critical()
Copied!
A more serious error, indicating that the Skill is unable to continue running.

Using the logger outside the Skill class

As the logger is provided by the MycroftSkill class, it is only available within that scope. If you need to log messages from outside of this class, you can import the logger manually.
1
from mycroft.util import LOG
Copied!
This can then be used outside your Skill's class. Extending our first example:
1
from adapt.intent import IntentBuilder
2
from mycroft import MycroftSkill, intent_handler
3
from mycroft.util import LOG
4
5
LOG.info("This is a logged info level message outside of the MycroftSkill Class scope")
6
7
def my_special_function():
8
LOG.info("Another usage of LOG.")
9
10
class LoggingSkill(MycroftSkill):
11
12
@intent_handler(IntentBuilder('HelloWorldIntent')
13
.require('HelloWorld'))
14
def handle_hello_world_intent(self, message):
15
""" Skills can log useful information. These will appear in the CLI and
16
the skills.log file."""
17
self.log.info("This is an info level log message.")
18
self.speak_dialog("hello.world")
19
my_special_function()
20
21
def create_skill():
22
return LoggingSkill()
Copied!
Last modified 3mo ago