diff --git a/README.md b/README.md index e4a8a766885cc6af4b99db506874a40026d7763d..e4e7c67edf7096187da697a0bdbdcdbf34246fb0 100644 --- a/README.md +++ b/README.md @@ -128,35 +128,39 @@ def command(self, parameter_json): # ... ``` -The context handler logs to the root logger by default. Logs can be sent to a custom logger by passing a logger object as a keyword argument. Use `configure_logging` method for ska formatted logs. +The context handler logs to the local logger by default. Logs can be sent to a custom logger by passing a logger object as a keyword argument. Use `configure_logging` method for ska formatted logs. **Example ska formatted logs for successful transaction** Log message formats: - On Entry: - - Transaction [id]: Enter [name] with parameters [arguments] + - Transaction[id]: Enter[name] with parameters [arguments] marker[marker] - On Exit: - - Transaction [id]: Exit [name] + - Transaction[id]: Exit[name] marker[marker] - On exception: - - Transaction [id]: Exception [name] - --Stacktrace-- + - Transaction[id]: Exception[name] marker[marker] + -- Stacktrace -- + +The marker can be used to match entry/exception/exit log messages. ```txt -1|2020-09-22T11:43:22.760Z|INFO|Dummy-1|__enter__|transactions.py#124|tango-device:log/test/upstream|Transaction [txn-local-20200922-232544376]: Enter[CallWithContext] with parameters[{"ArgKey2": "ArgVal2"}] -1|2020-09-22T11:43:22.782Z|INFO|Dummy-1|__exit__|transactions.py#133|tango-device:log/test/upstream|Transaction[txn-local-20200922-232544376]: Exit[CallWithContext] +1|2020-10-01T12:49:31.119Z|INFO|Thread-210|thread_with_transaction_exception|test_transactions_threaded.py#23|transaction_id:txn-local-20201001-981667980|Transaction[txn-local-20201001-981667980]: Enter[Command] with parameters [{}] marker[52764] +1|2020-10-01T12:49:31.129Z|INFO|Thread-210|thread_with_transaction_exception|test_transactions_threaded.py#23|transaction_id:txn-local-20201001-981667980|Transaction[txn-local-20201001-981667980]: Exit[Command] marker[52764] + ``` **Example ska formatted logs for failed transaction** ```txt -1|2020-09-22T11:45:07.122Z|ERROR|Dummy-1|__exit__|transactions.py#131|tango-device:log/test/upstream|Transaction[txn-local-20200922-362753747]: Exception[CallRaisesException] -Traceback (most recent call last): - File "LogTestUpstream.py", line 47, in CallRaisesException - raise RuntimeError("An exception has occured") -RuntimeError: An exception has occured -1|2020-09-22T11:45:07.123Z|INFO|Dummy-1|__exit__|transactions.py#133|tango-device:log/test/upstream|Transaction[txn-local-20200922-362753747]: Exit[CallRaisesException] +1|2020-10-01T12:51:35.588Z|INFO|Thread-204|thread_with_transaction_exception|test_transactions_threaded.py#23|transaction_id:txn-local-20201001-354400050|Transaction[txn-local-20201001-354400050]: Enter[Transaction thread [7]] with parameters [{}] marker[21454] +1|2020-10-01T12:51:35.598Z|ERROR|Thread-204|thread_with_transaction_exception|test_transactions_threaded.py#23|transaction_id:txn-local-20201001-354400050|Transaction[txn-local-20201001-354400050]: Exception[Transaction thread [7]] marker[21454] +Traceback (most recent call last): + File "python_file.py", line 27, in thread_with_transaction_exception + raise RuntimeError("An exception has occurred") +RuntimeError: An exception has occurred +1|2020-10-01T12:51:35.601Z|INFO|Thread-204|thread_with_transaction_exception|test_transactions_threaded.py#23|transaction_id:txn-local-20201001-354400050|Transaction[txn-local-20201001-354400050]: Exit[Transaction thread [7]] marker[21454] ``` Requirements diff --git a/src/ska/logging/transactions.py b/src/ska/logging/transactions.py index e6c1a1068e576b449774ecd970fbc71d97c64db8..8e5d1ccc9af4145f582cad765cdfe71b07830ff3 100644 --- a/src/ska/logging/transactions.py +++ b/src/ska/logging/transactions.py @@ -49,7 +49,7 @@ class TransactionIDTagsFilter(logging.Filter): # From Python 3.8 we should rather use `stacklevel` frame = self.get_frame() if frame: - if record.filename.startswith("transactions.py") and record.funcName in [ + if record.pathname == __file__ and record.funcName in [ "__enter__", "__exit__", ]: @@ -99,11 +99,11 @@ class Transaction: Log message formats: On Entry: - Transaction[id]: Enter [name] with parameters [arguments] + Transaction[id]: Enter[name] with parameters [arguments] marker[marker] On Exit: - Transaction[id]: Exit [name] + Transaction[id]: Exit[name] marker[marker] On exception: - Transaction[id]: Exception [name] + Transaction[id]: Exception[name] marker[marker] Stacktrace """