Skip to content

Commit efc77a4

Browse files
Refactor Logging Hierarchy (#997)
* Refactored logger names to adhere to a hierarchical structure for better log source tracing. * API docs: add hierarchical logging + usage clarification --------- Co-authored-by: Robsdedude <[email protected]>
1 parent 9fa9bfa commit efc77a4

File tree

22 files changed

+61
-29
lines changed

22 files changed

+61
-29
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
44

55
## NEXT RELEASE
66
- No breaking or major changes.
7+
- Implemented a hierarchical logger structure to improve log source
8+
identification and traceability.
9+
Introduced child loggers:
10+
- `neo4j.io`: For socket and bolt protocol related logging.
11+
- `neo4j.pool`: For logs pertaining to connection pooling and routing.
12+
- `neo4j.auth_management`: For logging inside the provided AuthManager
13+
implementations.
714

815

916
## Version 5.15

docs/source/api.rst

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,9 +2080,30 @@ following code:
20802080
Logging
20812081
*******
20822082

2083-
The driver offers logging for debugging purposes. It is not recommended to
2084-
enable logging for anything other than debugging. For instance, if the driver is
2085-
not able to connect to the database server or if undesired behavior is observed.
2083+
The driver offers logging for debugging purposes.
2084+
It is not recommended to enable logging for anything other than debugging.
2085+
For instance, if the driver is not able to connect to the database server or if
2086+
undesired behavior is observed.
2087+
2088+
This includes messages logged on ``WARNING`` level or higher.
2089+
They are logged to help understand what is going on *inside* the driver.
2090+
All relevant information is passed through return values, raised exceptions,
2091+
warnings, etc.
2092+
The logs are not the right place to look for actionable information.
2093+
2094+
The driver supports hierarchical logging.
2095+
This means you can selectively configure all of the driver's logging or only
2096+
parts of it.
2097+
Currently available:
2098+
2099+
* ``neo4j``: The root logger for the driver.
2100+
High-level code (e.g., Session, Transaction, Driver, etc.) logs here.
2101+
2102+
* ``neo4j.io``: Logs network activity and bolt protocol messages, handshakes,
2103+
etc.
2104+
* ``neo4j.pool``: Logs connection pool activity (including routing).
2105+
* ``neo4j.auth_management``: Logger for provided :class:`.AuthManager`
2106+
implementations.
20862107

20872108
There are different ways of enabling logging as listed below.
20882109

src/neo4j/_async/auth_management.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from ..exceptions import Neo4jError
3333

3434

35-
log = getLogger("neo4j")
35+
log = getLogger("neo4j.auth_management")
3636

3737

3838
class AsyncStaticAuthManager(AsyncAuthManager):
@@ -67,7 +67,11 @@ def __init__(
6767
self._lock = AsyncLock()
6868

6969
async def _refresh_auth(self):
70-
self._current_auth = await self._provider()
70+
try:
71+
self._current_auth = await self._provider()
72+
except BaseException as e:
73+
log.error("[ ] _: <AUTH MANAGER> provider failed: %r", e)
74+
raise
7175
if self._current_auth is None:
7276
raise TypeError(
7377
"Auth provider function passed to expiration_based "

src/neo4j/_async/io/_bolt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858

5959
# Set up logger
60-
log = getLogger("neo4j")
60+
log = getLogger("neo4j.io")
6161

6262

6363
class ServerStateManagerBase(abc.ABC):

src/neo4j/_async/io/_bolt3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
)
5050

5151

52-
log = getLogger("neo4j")
52+
log = getLogger("neo4j.io")
5353

5454

5555
class BoltStates(Enum):

src/neo4j/_async/io/_bolt4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
)
5252

5353

54-
log = getLogger("neo4j")
54+
log = getLogger("neo4j.io")
5555

5656

5757
class AsyncBolt4x0(AsyncBolt):

src/neo4j/_async/io/_bolt5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
)
5555

5656

57-
log = getLogger("neo4j")
57+
log = getLogger("neo4j.io")
5858

5959

6060
class AsyncBolt5x0(AsyncBolt):

src/neo4j/_async/io/_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929

3030

31-
log = logging.getLogger("neo4j")
31+
log = logging.getLogger("neo4j.io")
3232

3333

3434
class AsyncInbox:

src/neo4j/_async/io/_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969

7070
# Set up logger
71-
log = getLogger("neo4j")
71+
log = getLogger("neo4j.pool")
7272

7373

7474
@dataclass

src/neo4j/_async/work/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
_P = te.ParamSpec("_P")
6262

6363

64-
log = getLogger("neo4j")
64+
log = getLogger("neo4j.pool")
6565

6666

6767
class AsyncSession(AsyncWorkspace):

0 commit comments

Comments
 (0)