<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Logging on Tech Philosopher</title><link>https://blog.bktus.com/en/tags/logging/</link><description>Recent content in Logging on Tech Philosopher</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Fri, 20 Jan 2023 23:01:41 +0800</lastBuildDate><atom:link href="https://blog.bktus.com/en/tags/logging/index.xml" rel="self" type="application/rss+xml"/><item><title>Weekly Technical Report for the First Week of January 2023</title><link>https://blog.bktus.com/en/archives/ocgdc4/</link><pubDate>Fri, 20 Jan 2023 23:01:41 +0800</pubDate><guid>https://blog.bktus.com/en/archives/ocgdc4/</guid><description>&lt;p>As we enter 2023, the year ahead is expected to be challenging.&lt;/p>
&lt;p>One major task is migrating all services from physical servers to the cloud. In
parallel, several new team members need to be onboarded quickly and brought up
to a level where they can independently handle production issues and optimize
services. This will allow me to gradually transfer routine work and focus on
more long-term and critical goals.&lt;/p>
&lt;p>At a personal level, I also feel that I have reached a stage where my technical
growth will significantly influence my direction over the next 7–8 years.&lt;/p></description><content:encoded><![CDATA[<p>As we enter 2023, the year ahead is expected to be challenging.</p>
<p>One major task is migrating all services from physical servers to the cloud. In
parallel, several new team members need to be onboarded quickly and brought up
to a level where they can independently handle production issues and optimize
services. This will allow me to gradually transfer routine work and focus on
more long-term and critical goals.</p>
<p>At a personal level, I also feel that I have reached a stage where my technical
growth will significantly influence my direction over the next 7–8 years.</p>
<p>This week, my main focus was designing and standardizing logging and
traceability across multiple services.</p>
<p>To enable cross-service log tracing, the first step is to unify the TraceId.
However, the existing services used inconsistent formats—some used <code>Long</code>,
others used <code>String</code>. In addition, the technology stacks varied across services,
making it difficult to directly adopt a standard distributed tracing framework
across the board.</p>
<p>Given these constraints, I chose a pragmatic approach: design a custom TraceId
that is simple, compatible, and easy to roll out incrementally.</p>
<p>The TraceId is defined as a 16-digit <code>Long</code>, structured as follows:</p>
<ul>
<li>Prefix (2 digits): <code>99</code>, indicating the unified TraceId format</li>
<li>Service identifier (2 digits): identifies the originating service</li>
<li>Timestamp (4 digits): current microseconds (partial)</li>
<li>Random component (8 digits): two concatenated 4-digit random numbers</li>
</ul>
<p>Although this design does not guarantee absolute uniqueness, it is sufficient
for practical tracing purposes in the current system.</p>
<p>For Java services, special care is required when generating TraceIds under
concurrency. To avoid contention, a per-thread random number generator is
recommended (e.g., using <code>ThreadLocal</code>).</p>
<p>When handling incoming requests:</p>
<ul>
<li>Extract the TraceId from the request if present</li>
<li>If it already follows the unified format (prefix <code>99</code>), reuse it</li>
<li>Otherwise, generate a new TraceId and store it in the MDC</li>
</ul>
<p>For asynchronous execution, the MDC context must be explicitly propagated to
child threads; otherwise, trace information will be lost.</p>
<p>When invoking downstream services, the TraceId must be included in the request
to maintain trace continuity across services.</p>
<p>Finally, after the request is completed, the MDC should be cleared to avoid
leaking context into subsequent requests.</p>
<p>This design provides a lightweight and compatible tracing mechanism that can be
gradually adopted across heterogeneous services.</p>
]]></content:encoded></item></channel></rss>