Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand logging configuration support in PennyLane, and also target Catalyst #5528

Merged
merged 72 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
6feac37
Add log-rules for catalyst
mlxd Apr 11, 2024
eb0d72e
Update logging with file and syslog supports
mlxd Apr 12, 2024
9bf93c3
Add support for direct modification of log config
mlxd Apr 16, 2024
c997ed7
Add support for log decorator
mlxd Apr 17, 2024
4eb2aa2
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 6, 2024
886a80a
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 8, 2024
ef129d4
Update logging to latest release
mlxd May 8, 2024
f095479
Run sort n lint
mlxd May 8, 2024
83b3a40
Merge branch 'update/logging_rules_catalyst' into update/logging_rule…
mlxd May 8, 2024
1e4a923
Fix format
mlxd May 8, 2024
e721a39
Pylint adhere
mlxd May 8, 2024
3d1e81c
[WIP] Update PennyLane pipeline logging support (#5534)
mlxd May 8, 2024
a463859
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 8, 2024
e0ae2b5
Listen to codefactor
mlxd May 8, 2024
42ee75f
Ensure Windows function doesn't warn on *nix
mlxd May 8, 2024
8f244ca
Merge branch 'update/logging_rules_pennylane' into update/logging_rul…
mlxd May 8, 2024
8a06f02
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 8, 2024
fb64c3d
Disable extraneous warning
mlxd May 8, 2024
ff9d8b7
Merge branch 'update/logging_rules_catalyst' of github.com:PennyLaneA…
mlxd May 8, 2024
447782b
Apply suggestions from code review
mlxd May 17, 2024
23c5049
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 17, 2024
1c46ff1
Update logging tests for new structure
mlxd May 17, 2024
027fa1e
Update according to reviews
mlxd May 17, 2024
cccc436
Update logging docs
mlxd May 17, 2024
50b764d
Remove unused variable
mlxd May 17, 2024
42a1f21
Spellcheck
mlxd May 17, 2024
3e7ca34
Be more explicit about logging levels in tests
mlxd May 17, 2024
608fa73
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 17, 2024
0205a4d
Forbid pointless warning
mlxd May 17, 2024
e811891
Silence Pylints verbose complaint
mlxd May 17, 2024
a638f36
Silence Pylints verbose complaint +1
mlxd May 17, 2024
1a6c3a7
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 21, 2024
9507454
Update doc/development/guide/logging.rst
mlxd May 23, 2024
c45f765
Update doc/development/guide/logging.rst
mlxd May 23, 2024
76d49a8
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 23, 2024
a8a0c53
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 23, 2024
b8c9a23
Indent and enumerate sphinx list
mlxd May 23, 2024
c1f5b9a
Fix indentation issue
mlxd May 23, 2024
1c36a2b
Update tests/logging/test_logging_autograd.py
mlxd May 24, 2024
bac25c5
Update tests/logging/test_logging_autograd.py
mlxd May 24, 2024
b747d14
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 24, 2024
131b5e9
Add context-manager around test logging
mlxd May 24, 2024
f4ab67b
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 24, 2024
f4dd8a2
Update log rule context manager
mlxd May 24, 2024
b3911d6
Merge branch 'update/logging_rules_catalyst' of github.com:PennyLaneA…
mlxd May 24, 2024
793f150
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 24, 2024
fab9d81
Update config
mlxd May 24, 2024
a9b714e
Explicit remove all loggers from test config after run
mlxd May 24, 2024
a56c174
Update logging to newer format across devices
mlxd May 27, 2024
f25d502
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 27, 2024
40e06ba
Update changelog
mlxd May 27, 2024
6a14487
Merge branch 'update/logging_rules_catalyst' of github.com:PennyLaneA…
mlxd May 27, 2024
811b875
Remove unneeded packages
mlxd May 27, 2024
5fd081c
Remove redundant except
mlxd May 27, 2024
b898b0f
Update logging config
mlxd May 27, 2024
1b5a2fd
Add qutrit mixed log tests to logging module tests
mlxd May 27, 2024
c570cac
Update log tests
mlxd May 28, 2024
f298581
Remove redundant functions in tests
mlxd May 28, 2024
66a5942
Remove unused import
mlxd May 28, 2024
29bad9e
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 28, 2024
ecbe2e2
Update doc/development/guide/logging.rst
mlxd May 28, 2024
42c2511
Update doc/development/guide/logging.rst
mlxd May 28, 2024
44589ad
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 29, 2024
3f3e339
Update pennylane/logging/configuration.py
mlxd May 29, 2024
81dc6e8
Update pennylane/logging/configuration.py
mlxd May 29, 2024
cda291c
Add parenthesis for safety to env var check
mlxd May 29, 2024
25910f6
Disable pylint incorrect parenthesis error
mlxd May 29, 2024
77044d8
Update review feedback
mlxd May 29, 2024
2922292
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 29, 2024
400428d
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 30, 2024
c062986
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 30, 2024
3f44012
Merge branch 'master' into update/logging_rules_catalyst
mlxd May 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pennylane/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.
"""This module enables support for log-level messaging throughout PennyLane, following the native Python logging framework interface. Please see the :doc:`PennyLane logging development guidelines</development/guide/logging>`, and the official Python documentation for details on usage https://docs.python.org/3/library/logging.html"""

from .configuration import enable_logging
from .configuration import config_path
from .configuration import config_path, edit_system_config, enable_logging
from .configuration import TRACE
from .formatters.formatter import DefaultFormatter, SimpleFormatter
from .filter import LocalProcessFilter, DebugOnlyFilter
from .decorators import debug_logger
43 changes: 39 additions & 4 deletions pennylane/logging/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""
import logging
import logging.config
import os
import os, platform, subprocess
from importlib import import_module
from importlib.util import find_spec

Expand Down Expand Up @@ -53,7 +53,7 @@
lc.trace = trace


def _configure_logging(config_file):
def _configure_logging(config_file: str, config_override: dict = {}):

Check notice on line 56 in pennylane/logging/configuration.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/logging/configuration.py#L56

Dangerous default value {} as argument (dangerous-default-value)

Check notice on line 56 in pennylane/logging/configuration.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/logging/configuration.py#L56

Unused argument 'config_override' (unused-argument)
"""
This method allows custom logging configuration throughout PennyLane.
All configurations are read through the ``log_config.toml`` file.
Expand All @@ -71,7 +71,7 @@
logging.config.dictConfig(pl_config)


def enable_logging():
def enable_logging(config_file: str = "log_config.toml"):
mlxd marked this conversation as resolved.
Show resolved Hide resolved
"""
This method allows to selectively enable logging throughout PennyLane, following the configuration options defined in the ``log_config.toml`` file.

Expand All @@ -82,7 +82,7 @@
>>> qml.logging.enable_logging()
"""
_add_trace_level()
_configure_logging("log_config.toml")
_configure_logging(config_file)


def config_path():
Expand All @@ -99,3 +99,38 @@
"""
path = os.path.join(_path, "log_config.toml")
return path


def show_system_config():
"""
This function opens the logging configuration file in the system-default browser.
"""

Check notice on line 107 in pennylane/logging/configuration.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/logging/configuration.py#L107

Import outside toplevel (webbrowser) (import-outside-toplevel)
import webbrowser
mlxd marked this conversation as resolved.
Show resolved Hide resolved

webbrowser.open(config_path())


def edit_system_config(wait_on_close=False):
"""
This function opens the log configuration file using OS-specific editors.

Setting the `EDITOR` environment variable will override xdg-open/open on
Linux and MacOS, and allows use of `wait_on_close` for editor close before
continuing execution.
mlxd marked this conversation as resolved.
Show resolved Hide resolved

Warning: As each OS configuration differs user-to-user, you may wish to
instead open this file manually with the `config_path()` provided path.

Check notice on line 122 in pennylane/logging/configuration.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/logging/configuration.py#L122

Consider using 'with' for resource-allocating operations (consider-using-with)
mlxd marked this conversation as resolved.
Show resolved Hide resolved
"""
if editor := os.getenv("EDITOR"):
mlxd marked this conversation as resolved.
Show resolved Hide resolved
p = subprocess.Popen((editor, config_path()))
if wait_on_close: # Only valid when editor is known
p.wait()
elif (s := platform.system()) in ["Linux", "Darwin"]:
f_cmd = None
if s == "Linux":
f_cmd = "xdg-open"

Check notice on line 131 in pennylane/logging/configuration.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/logging/configuration.py#L131

Consider using 'with' for resource-allocating operations (consider-using-with)
else:
f_cmd = "open"
subprocess.Popen((f_cmd, config_path()))
else: # Windows
os.startfile(config_path())
49 changes: 49 additions & 0 deletions pennylane/logging/formatters/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""The PennyLane log-level formatters are defined here with default options, and ANSI-terminal color-codes."""
import inspect
import logging
from logging import Formatter
from typing import NamedTuple, Tuple, Union
Expand Down Expand Up @@ -114,6 +115,54 @@ def format(self, record):
return formatter.format(record)


class DynamicFormatter(Formatter):
mlxd marked this conversation as resolved.
Show resolved Hide resolved
"""This formatter has the default rules used for formatting PennyLane log messages, with a dynamically updated log format rule"""

# 0x000000 Background
_text_bg = (0, 0, 0)

cmap = ColorScheme(
debug=(220, 238, 200), # Grey 1
debug_bg=_text_bg,
info=(80, 125, 125), # Blue
info_bg=_text_bg,
warning=(208, 167, 133), # Orange
warning_bg=_text_bg,
error=(208, 133, 133), # Red 1
error_bg=_text_bg,
critical=(135, 53, 53), # Red 2
critical_bg=(210, 210, 210), # Grey 2
use_rgb=True,
)

def _build_formats(self, fmt_str):
cmap = DynamicFormatter.cmap
local_formats = {
logging.DEBUG: build_code_rgb(cmap.debug, cmap.debug_bg) + fmt_str + _ANSI_CODES["end"],
logging.INFO: build_code_rgb(cmap.info, cmap.info_bg) + fmt_str + _ANSI_CODES["end"],
logging.WARNING: build_code_rgb(cmap.warning, cmap.warning_bg)
+ fmt_str
+ _ANSI_CODES["end"],
logging.ERROR: build_code_rgb(cmap.error, cmap.error_bg) + fmt_str + _ANSI_CODES["end"],
logging.CRITICAL: build_code_rgb(cmap.critical, cmap.critical_bg)
+ fmt_str
+ _ANSI_CODES["end"],
}

return local_formats

def format(self, record):
f = inspect.getouterframes(inspect.currentframe())[
8
] # Stack depth from log-func call to format function

fmt_str = f'[%(asctime)s][%(levelname)s][<PID %(process)d:%(processName)s>] - %(name)s.{f.function}()::"%(message)s"'

log_fmt = self._build_formats(fmt_str).get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)


class SimpleFormatter(Formatter):
"""This formatter has a simplified layout and rules used for formatting PennyLane log messages."""

Expand Down
27 changes: 27 additions & 0 deletions pennylane/logging/log_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ version = 1
[formatters.qml_default_formatter]
"()" = "pennylane.logging.formatters.formatter.DefaultFormatter"

[formatters.qml_dynamic_formatter]
"()" = "pennylane.logging.formatters.formatter.DynamicFormatter"

[formatters.qml_alt_formatter]
"()" = "pennylane.logging.formatters.formatter.SimpleFormatter"

Expand Down Expand Up @@ -46,12 +49,24 @@ formatter = "qml_default_formatter"
level = "DEBUG"
stream = "ext://sys.stdout"

[handlers.qml_debug_stream_dyn]
class = "logging.StreamHandler"
formatter = "qml_dynamic_formatter"
level = "DEBUG"
stream = "ext://sys.stdout"

[handlers.qml_debug_stream_alt]
class = "logging.StreamHandler"
formatter = "qml_alt_formatter"
level = "DEBUG"
stream = "ext://sys.stdout"

[handlers.qml_debug_syslog]
class = "logging.handlers.SysLogHandler"
formatter = "local_standard"
level = "DEBUG"
address = "/dev/log"

[handlers.qml_debug_file]
class = "logging.handlers.RotatingFileHandler"
formatter = "local_standard"
Expand Down Expand Up @@ -80,12 +95,24 @@ handlers = ["qml_debug_stream",]
level = "WARN"
propagate = false

# Control JAXlib logging
[loggers.jaxlib]
handlers = ["qml_debug_stream",]
level = "WARN"
propagate = false

# Control logging across pennylane
[loggers.pennylane]
handlers = ["qml_debug_stream",]
level = "DEBUG" # Set to TRACE for highest verbosity
propagate = false

# Control logging across catalyst
[loggers.catalyst]
handlers = ["qml_debug_stream",]
level = "TRACE" # Set to TRACE for highest verbosity
propagate = false

# Control logging specifically in the pennylane.qnode module
# Note the required quotes to overcome TOML nesting issues
[loggers."pennylane.qnode"]
Expand Down
Loading