Skip to content

Commit bea712c

Browse files
Add support printing TID to logs (#323)
1 parent 9b1acaa commit bea712c

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Feature:
66
- Users now can specify the `SW_AGENT_ASYNCIO_ENHANCEMENT` environment variable to enable the performance enhancement with asyncio (#316)
7+
- Support printing Trace IDs (TID) to collected application logs (#323)
78

89
- Plugins:
910
- Add neo4j plugin.(#312)

docs/en/setup/advanced/LogReporter.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,10 @@ Will result in:
103103
}
104104
]
105105
}
106-
```
106+
```
107+
108+
## Print trace ID in your logs
109+
To print out the trace IDs in the logs, simply add `%(tid)s` to the `agent_log_reporter_layout`.
110+
111+
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,
112+
this can be achieved by using any formatter you prefer in your own application logic.

skywalking/agent/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ def start(self) -> None:
268268
logger.info(f'SkyWalking sync agent instance {config.agent_instance_name} starting in pid-{os.getpid()}.')
269269

270270
# Install log reporter core
271-
# TODO - Add support for printing traceID/ context in logs
272271
if config.agent_log_reporter_active:
273272
from skywalking import log
274273
log.install()
@@ -502,7 +501,6 @@ async def __start_event_loop_async(self) -> None:
502501
self._finished = asyncio.Event()
503502

504503
# Install log reporter core
505-
# TODO - Add support for printing traceID/ context in logs
506504
if config.agent_log_reporter_active:
507505
from skywalking import log
508506
log.install()

skywalking/log/sw_logging.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def build_log_tags() -> LogTags:
7575

7676
context = get_context()
7777

78+
if '%(tid)s' in layout:
79+
record.tid = str(context.segment.related_traces[0])
80+
7881
active_span_id = -1
7982
primary_endpoint_name = ''
8083

0 commit comments

Comments
 (0)