This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/skywalking-python.git
The following commit(s) were added to refs/heads/master by this push: new bea712c Add support printing TID to logs (#323) bea712c is described below commit bea712c7a87583432764ec5e176bca49b9595bb3 Author: Starry <codeprince2...@163.com> AuthorDate: Sat Nov 4 11:29:01 2023 +0800 Add support printing TID to logs (#323) --- CHANGELOG.md | 1 + docs/en/setup/advanced/LogReporter.md | 8 +++++++- skywalking/agent/__init__.py | 2 -- skywalking/log/sw_logging.py | 3 +++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 849e94e..c775273 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Feature: - Users now can specify the `SW_AGENT_ASYNCIO_ENHANCEMENT` environment variable to enable the performance enhancement with asyncio (#316) + - Support printing Trace IDs (TID) to collected application logs (#323) - Plugins: - Add neo4j plugin.(#312) diff --git a/docs/en/setup/advanced/LogReporter.md b/docs/en/setup/advanced/LogReporter.md index c1982d9..e08e65f 100644 --- a/docs/en/setup/advanced/LogReporter.md +++ b/docs/en/setup/advanced/LogReporter.md @@ -103,4 +103,10 @@ Will result in: } ] } -``` \ No newline at end of file +``` + +## Print trace ID in your logs +To print out the trace IDs in the logs, simply add `%(tid)s` to the `agent_log_reporter_layout`. + +You can take advantage of this feature to print out the trace IDs on any channel you desire, not limited to reporting logs to OAP, +this can be achieved by using any formatter you prefer in your own application logic. \ No newline at end of file diff --git a/skywalking/agent/__init__.py b/skywalking/agent/__init__.py index fc7f9e7..9ee4d92 100644 --- a/skywalking/agent/__init__.py +++ b/skywalking/agent/__init__.py @@ -268,7 +268,6 @@ class SkyWalkingAgent(Singleton): logger.info(f'SkyWalking sync agent instance {config.agent_instance_name} starting in pid-{os.getpid()}.') # Install log reporter core - # TODO - Add support for printing traceID/ context in logs if config.agent_log_reporter_active: from skywalking import log log.install() @@ -502,7 +501,6 @@ class SkyWalkingAgentAsync(Singleton): self._finished = asyncio.Event() # Install log reporter core - # TODO - Add support for printing traceID/ context in logs if config.agent_log_reporter_active: from skywalking import log log.install() diff --git a/skywalking/log/sw_logging.py b/skywalking/log/sw_logging.py index 801befe..e192368 100644 --- a/skywalking/log/sw_logging.py +++ b/skywalking/log/sw_logging.py @@ -75,6 +75,9 @@ def install(): context = get_context() + if '%(tid)s' in layout: + record.tid = str(context.segment.related_traces[0]) + active_span_id = -1 primary_endpoint_name = ''