Skip to content

Show logs in the correct Jupyter notebook cell#1952

Open
rossjjennings wants to merge 1 commit intonanograv:masterfrom
rossjjennings:logging-fix
Open

Show logs in the correct Jupyter notebook cell#1952
rossjjennings wants to merge 1 commit intonanograv:masterfrom
rossjjennings:logging-fix

Conversation

@rossjjennings
Copy link
Member

Fixes #1951. The problem where logging messages were shown in the incorrect cell (the cell where pint.logging.setup() was called) seems to have been caused by passing the enqueue=True option to Loguru's logger.add() function. This PR simply removes the option. I've confirmed that this works both in a newer environment that showed the issue, and in an older one, which didn't have this issue, even with enqueue=True.

I'm not sure why we were using enqueue=True to begin with. The docstring for logger.add() says this about it:

enqueue : |bool|, optional
    Whether the messages to be logged should first pass through a multiprocessing-safe queue
    before reaching the sink. This is useful while logging to a file through multiple
    processes. This also has the advantage of making logging calls non-blocking.

Hopefully getting rid of this option (and switching back to enqueue=False, which is the default in Loguru) will not cause issues in some use case I'm not aware of involving multiple PINT processes trying to log to a single file. But I think most likely this option was originally added in an attempt to get logging in a Jupyter notebook to work better, which is now backfiring.

@rossjjennings
Copy link
Member Author

rossjjennings commented Jan 5, 2026

The test failure here is happening for MacOS only, so I can't easily reproduce it. It doesn't seem to be directly related to the changes here; it might be that the current master branch version would fail in the same way.

The problem seems to be a bunch of cases of TypeError: cannot pickle 'EncodedFile' instances in test_grid.py.

Edit: On second thought, it might be that this is related to the changes here, because test_grid.py is doing some stuff involving inter-process communication, and it might be that using enqueue=False is making it fall back to some method involving pickling. If so, that's unfortunate, and will make this harder to fix.

@dlakaplan
Copy link
Contributor

How about just make this an option to logging.setup() (could be notebook=... or grid=... depending on how you want to frame the question). Then the logger can be set up in both ways depending on usage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log messages print in the wrong Jupyter notebook cell

2 participants