bloomberg / pytest-memray

pytest plugin for easy integration of memray memory profiler
https://pytest-memray.readthedocs.io/en/latest/
Apache License 2.0
353 stars 25 forks source link

Allow logs from memray core #134

Open Clae224 opened 2 weeks ago

Clae224 commented 2 weeks ago

I am currently unable to see any of the core memray logs when running pytest-memray with my project's test suite.

As an example, I am trying to see if support for greenlet is running inside memray when running pytest-memray, but I cannot get memray core to print or log any lines whether greenlet support is enabled or not. I have tried building memray from scratch and adding log lines and prints to both when greenlet is detected and not detected, but nothing shows up not matter how I run pytest-memray or what logging level I use. I can see the greenlet log when running pytest directly on memray without pytest-memray e.g. memray run -m pytest ... in the same environment.

Perhaps this is already implemented somewhere, but I cannot find it on the docs or see an option to enable anything like this.

Ideally there would be an option to get output from core memray code, so we can debug issues directly when using pytest-memray without having to run pytest in memray.

godlygeek commented 1 week ago

Memray doesn't use the Python logging module, it instead logs by printing directly to stderr. By default pytest captures everything logged to stderr, but you can see it on the terminal if you run pytest with the -s / --capture=no flag. If you run pytest with -s in addition to --memray, you'll see the "Memray support for Greenlet is experimental" warning if your tests import the greenlet module.

You'll only see Memray's logs from the WARNING level and above, as that's the default threshold Memray sets. If you need to see logs from a lower level than that, you could apply a patch like this to pytest-memray:

diff --git a/src/pytest_memray/plugin.py b/src/pytest_memray/plugin.py
index c1f7d3d..f6fb9f8 100644
--- a/src/pytest_memray/plugin.py
+++ b/src/pytest_memray/plugin.py
@@ -3,6 +3,7 @@ from __future__ import annotations
 import collections
 import functools
 import inspect
+import logging
 import math
 import os
 import pickle
@@ -34,6 +35,7 @@ from pytest import Item
 from pytest import Parser
 from pytest import TestReport
 from pytest import hookimpl
+from memray import set_log_level

 from .marks import limit_memory
 from .marks import limit_leaks
@@ -42,6 +44,8 @@ from .utils import positive_int
 from .utils import sizeof_fmt
 from .utils import value_or_ini

+set_log_level(logging.DEBUG)
+

 class SectionMetadata(Protocol):
     long_repr: str

What are you trying to debug, specifically?