Skip to content

Commit d1f4824

Browse files
committed
fix: dispose method to dispose of SI instance only if it exists. Session to be created only if it does not already
1 parent f5b124b commit d1f4824

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

smartinspect_handler/smartinspect_handler.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ def get_si(self) -> SmartInspect:
6161
| logger = logging.getLogger(__name__)
6262
6363
| # create a connection string using ConnectionStringBuilder
64-
| conn_string = ConnectionStringBuilder().add_tcp_protocol().set_host("127.0.0.1").set_port(4228).set_timeout(
65-
30000).set_async_enabled(False).end_protocol().build()
64+
| conn_string = ConnectionStringBuilder().add_tcp_protocol().set_host("127.0.0.1").set_port(4228).
65+
set_timeout(30000).set_async_enabled(False).end_protocol().build()
6666
6767
| # create a SmartInspectHandler instance, set format and attach handler to the logger
6868
| handler = SmartInspectHandler("Client app", conn_string)
69-
| handler.setFormatter(logging.Formatter("%(threadName)s, %(asctime)s: %(module)s @ %(funcName)s: %(message)s"))
69+
| handler.setFormatter(logging.Formatter("%(asctime)s: %(module)s @ %(funcName)s: %(message)s"))
7070
| logger.addHandler(handler)
7171
| logger.setLevel(logging.DEBUG)
7272
@@ -116,9 +116,10 @@ def _enable_si(self) -> None:
116116
def _create_si_session(self) -> None:
117117
"""
118118
Create SmartInspect Session used to dispatch logging Records.
119+
A Session is only created if it does not exist already.
119120
"""
120-
121-
self._si_session = self._si.add_session("Session", True)
121+
if not self._si_session:
122+
self._si_session = self._si.add_session("Session", True)
122123

123124
def _do_emit(self, record: logging.LogRecord) -> None:
124125
"""
@@ -160,7 +161,7 @@ def dispose(self) -> None:
160161
161162
| # create a SmartInspectHandler instance, set format and attach handler to the logger
162163
| handler = SmartInspectHandler("Client app", conn_string)
163-
| handler.setFormatter(logging.Formatter("%(threadName)s, %(asctime)s: %(module)s @ %(funcName)s: %(message)s"))
164+
| handler.setFormatter(logging.Formatter("%(asctime)s: %(module)s @ %(funcName)s: %(message)s"))
164165
| logger.addHandler(handler)
165166
| logger.setLevel(logging.DEBUG)
166167
@@ -169,4 +170,5 @@ def dispose(self) -> None:
169170
| # explicitly dispose of handler when finished working in async mode
170171
| handler.dispose()
171172
"""
172-
self._si.dispose()
173+
if self._si:
174+
self._si.dispose()

0 commit comments

Comments
 (0)