Skip to content

Commit

Permalink
Add support printing TID to logs (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodePrometheus authored Nov 4, 2023
1 parent 9b1acaa commit bea712c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion docs/en/setup/advanced/LogReporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,10 @@ Will result in:
}
]
}
```
```

## 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.
2 changes: 0 additions & 2 deletions skywalking/agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def start(self) -> None:
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()
Expand Down Expand Up @@ -502,7 +501,6 @@ async def __start_event_loop_async(self) -> None:
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()
Expand Down
3 changes: 3 additions & 0 deletions skywalking/log/sw_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def build_log_tags() -> LogTags:

context = get_context()

if '%(tid)s' in layout:
record.tid = str(context.segment.related_traces[0])

active_span_id = -1
primary_endpoint_name = ''

Expand Down

0 comments on commit bea712c

Please sign in to comment.