-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7924 from aldbr/v8.0_FIX_Matcher-logging
[8.0] fix(wms): correctly log the pilot job reference during the matching process
- Loading branch information
Showing
9 changed files
with
278 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from DIRAC.FrameworkSystem.private.standardLogging.LoggingContext import contextLogger, setContextLogger | ||
from DIRAC.FrameworkSystem.private.standardLogging.LoggingRoot import LoggingRoot | ||
|
||
gLogger = LoggingRoot() | ||
|
||
|
||
def getLogger(): | ||
return gLogger | ||
|
||
|
||
__all__ = ["contextLogger", "setContextLogger", "getLogger"] |
16 changes: 16 additions & 0 deletions
16
src/DIRAC/FrameworkSystem/private/standardLogging/LoggingContext.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" Logging context module""" | ||
|
||
# Context variable for the logger (adapted to the request of the pilot reference) | ||
import contextvars | ||
from contextlib import contextmanager | ||
|
||
contextLogger = contextvars.ContextVar("Logger", default=None) | ||
|
||
|
||
@contextmanager | ||
def setContextLogger(logger_name): | ||
token = contextLogger.set(logger_name) | ||
try: | ||
yield | ||
finally: | ||
contextLogger.reset(token) |
71 changes: 71 additions & 0 deletions
71
src/DIRAC/FrameworkSystem/private/standardLogging/test/Test_Logging_ContextVars.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" Test the context variable logger """ | ||
|
||
from DIRAC import gLogger | ||
from DIRAC.FrameworkSystem.private.standardLogging.Logging import Logging | ||
from DIRAC.FrameworkSystem.private.standardLogging.test.TestLogUtilities import gLoggerReset | ||
from DIRAC.FrameworkSystem.private.standardLogging.LoggingContext import contextLogger, setContextLogger | ||
|
||
|
||
class A: | ||
def __init__(self): | ||
# Get the logger from the context variable | ||
self._defaultLogger = gLogger.getSubLogger("A") | ||
|
||
# Use a property to get and set the logger, this is necessary to use the context variable | ||
@property | ||
def log(self): | ||
return contextLogger.get() or self._defaultLogger | ||
|
||
@log.setter | ||
def log(self, value: Logging): | ||
self._defaultLogger = value | ||
|
||
def do_something(self): | ||
self.log.notice("A is doing something") | ||
|
||
|
||
class B: | ||
def __init__(self, a: A, pilotRef: str = None): | ||
self.a = A() | ||
|
||
# Get the logger from the context variable | ||
if pilotRef: | ||
self.log = gLogger.getLocalSubLogger(f"[{pilotRef}]B") | ||
contextLogger.set(self.log) | ||
else: | ||
self.log = gLogger.getSubLogger("B") | ||
|
||
def do_something_else(self): | ||
with setContextLogger(self.log): | ||
self.a.do_something() | ||
self.log.notice("B is doing something else") | ||
|
||
|
||
def test_contextvar_logger(): | ||
capturedBackend, log, sublog = gLoggerReset() | ||
|
||
# Create an instance of A | ||
a = A() | ||
|
||
# Create an instance of B and call its method without setting the pilotRef | ||
# Log signature coming from A and B should be different | ||
b1 = B(a) | ||
b1.do_something_else() | ||
assert "Framework/B NOTICE: A is doing something" in capturedBackend.getvalue() | ||
assert "Framework/B NOTICE: B is doing something else" in capturedBackend.getvalue() | ||
|
||
# Create an instance of B and call its method with setting the pilotRef | ||
# Log signature coming from A and B should be similar because of the pilotRef | ||
capturedBackend.truncate(0) | ||
|
||
b2 = B(a, "pilotRef") | ||
b2.do_something_else() | ||
assert "Framework/[pilotRef]B NOTICE: A is doing something" in capturedBackend.getvalue() | ||
assert "Framework/[pilotRef]B NOTICE: B is doing something else" in capturedBackend.getvalue() | ||
|
||
# Now we check that the logger of b1 is not the same as the logger of b2 (b1 should still use its own logger) | ||
capturedBackend.truncate(0) | ||
|
||
b1.do_something_else() | ||
assert "Framework/B NOTICE: A is doing something" in capturedBackend.getvalue() | ||
assert "Framework/B NOTICE: B is doing something else" in capturedBackend.getvalue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.