Skip to main content

Using the logger-library

For logging, a custom class can be used, that enables adding a Prefix to all subsequent logs, to better identify the current script. Additionally the function-name of the function, that is currently in scope, is added in front of the log-message. A loglevel-preference can be specified, that is persistent between scripts. Every log can be given a loglevel, to only be executed, if the Logger-instance's preference allows it.

If the logger-library is already initialized through the script comet/scripts/initialize_global_library_objects.py, it is accessible through a variable in the comet-module:

LOGGER = comet.lib_logger.Logger("SOME PREFIX")

... otherwise it can be easily imported from the library-folder:

log_lib = comet.pyImport('[xml]/library/framework/logger.py')
LOGGER = log_lib.Logger("SOME PREFIX")

When initializing an instance of the logger-class, a prefix can be specified, that is added before each string that is logged. This prefix can be changed after initialization as well.

If a LogLevel was set in a previous instance of the logger-class, it was saved to a variable of the comet-module. When initializing a logger-instance, the log-level is retrieved from the comet-module and doesn't need to be set again. It can be changed through a method though:

LOGGER.set_prefix("MY PREFIX")
LOGGER.set_log_level(LOGGER.LogLevel.INFO)

The primary method of the logger-class is wlog which adds behaviour to a function of the comet-module by the same name: comet.wlog. The user-set prefix is added to each string that is logged by the method, as well as the current function's name. A custom prefix can be specified per method-call. By default, log-messages are written with the loglevel LogLevel.INFO. If the function is called with a lower level in the parameter (like LogLevel.DEBUG), it is only written, when the LogLevel is changed to a lower one. This might be useful for varying logging-behaviour on Staging and Production environments.
On Production the Loglevel could be set to LogLevel.WARNING (or above) by the build-script, so that subsequent scripts only write logs with a higher level than the default. On the Staging system, the build script could set a lower level to write more logs.

def main():

LOGGER.wlog("Hello World! (DEBUG)", LOGGER.LogLevel.DEBUG)
LOGGER.wlog("Hello World! (Default: INFO)")

LOGGER.set_log_level(LOGGER.LogLevel.DEBUG)
LOGGER.wlog("Hello World! (DEBUG)", LOGGER.LogLevel.DEBUG)

my_function()

return 0

def my_function():
LOGGER.wlog("Hello Function!", prefix="OTHER PREFIX")
return None

MY PREFIX [main] Hello World (Default: INFO)!
MY PREFIX [main] Hello World! (DEBUG)
OTHER PREFIX [my_function] Hello Function!



To easily retrieve more information about the current script, the following methods can be used to log the current function's parameters:

def main():
my_function("Hello World!", 42, {"key":"value"}, comet.host.getType())
return 0

def my_function(hello:str, num:int, keyvalues:dict, hosttype:int)
LOGGER.wlog_locals()
return None

MY PREFIX [my_function] local variables: {"hello": ("Hello World!", "str"), "num": (42, "int"), "keyvalues": ({"key":"value"}, "dict"), "hosttype": (comet.constants.kHostCometPDF, "int")}


... or the current comet-actions parameters (for example gParams of a layoutrule or function-variables of placeholder-scripts):

#pragma var "gEntityType" "generic"
#pragma var "gNodeType" "product"
#pragma var "gAttributeKey" "my_key"

(...)

LOGGER.wlog_action_parameters()

MY PREFIX [main] current action's parameters:
gEntityType: generic
gNodeType: product
gAttributeKey: my_key



info

For detailed information on the logger's methods see page Logger