<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spring Cloud Sleuth &#8211; Ling&#039;s Note</title>
	<atom:link href="https://www.chunho-ling.com/category/computing/programming/applicationframework/spring-applicationframework/spring-cloud/spring-cloud-sleuth/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.chunho-ling.com</link>
	<description>Everything related IT, and me.</description>
	<lastBuildDate>Wed, 10 Feb 2021 08:10:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">104401516</site>	<item>
		<title>[Spring] Enable logging to ELK with Log4J2 and Sprint cloud sleuth</title>
		<link>https://www.chunho-ling.com/spring-enable-logging-to-elk-with-log4j2-and-sprint-cloud-sleuth/</link>
					<comments>https://www.chunho-ling.com/spring-enable-logging-to-elk-with-log4j2-and-sprint-cloud-sleuth/#respond</comments>
		
		<dc:creator><![CDATA[C.H. Ling]]></dc:creator>
		<pubDate>Wed, 10 Feb 2021 08:10:10 +0000</pubDate>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[ELK Slack]]></category>
		<category><![CDATA[Kibana]]></category>
		<category><![CDATA[Logstash]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Cloud]]></category>
		<category><![CDATA[Spring Cloud Sleuth]]></category>
		<guid isPermaLink="false">https://www.chunho-ling.com/?p=1334</guid>

					<description><![CDATA[To enable ELK logging, mostly it will use logback as logging library. However, when migrate existing application, eliminate dependency change is one of the factor to ensure application stability. In this demo, we will use <a class="mh-excerpt-more" href="https://www.chunho-ling.com/spring-enable-logging-to-elk-with-log4j2-and-sprint-cloud-sleuth/" title="[Spring] Enable logging to ELK with Log4J2 and Sprint cloud sleuth">[...]</a>]]></description>
										<content:encoded><![CDATA[<p>To enable ELK logging, mostly it will use logback as logging library. However, when migrate existing application, eliminate dependency change is one of the factor to ensure application stability.<br />
In this demo, we will use Log4J2, a common Java logging library to write log to ELK.<span id="more-1334"></span></p>
<h1 id="Prerequisites" data-renderer-start-pos="1">Prerequisites<button class="sc-bsVVwV TEiOV"></button></h1>
<ol class="ak-ol" data-indent-level="1">
<li>
<p data-renderer-start-pos="18">Project migrated to gradle;</p>
</li>
<li>
<p data-renderer-start-pos="49">Using Log4J2 as logging library;</p>
</li>
</ol>
<h1 id="Steps" data-renderer-start-pos="85">Steps<button class="sc-bsVVwV TEiOV"></button></h1>
<ol class="ak-ol" data-indent-level="1">
<li>
<p data-renderer-start-pos="94">Add Spring Cloud Sleuch dependency to project.<br />
In build.gradle, add dependency as below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-sleuth:2.2.6.RELEASE'
}<code></code></pre>
</li>
<li>
<p data-renderer-start-pos="286">Deploy project to target server.</p>
</li>
<li>
<p data-renderer-start-pos="322">Add Socket appender to write log via Socket call.<br />
In log4j2.xml, add socket appender and enable it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="xml">&lt;Configuration status="WARN" monitorInterval="30"&gt;
    &lt;Properties&gt;
        &lt;Property name="LOG_PATTERN"&gt;%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%t] %notEmpty{ [%X{x-apigw-requestId}]} -  %logger{36} - %msg%n&lt;/Property&gt;
        &lt;Property name="LOG_FILE_NAME"&gt;user-management&lt;/Property&gt;
        &lt;Property name="LOG_PATH"&gt;c:\log\user-management&lt;/Property&gt;
    &lt;/Properties&gt;
    &lt;Appenders&gt;
        &lt;Socket name="socket" host="10.3.11.114" port="19722" protocol="TCP"&gt;
            &lt;JsonLayout properties="true" compact="true" eventEol="true" /&gt;
            &lt;PatternLayout pattern="${LOG_PATTERN}" /&gt;
        &lt;/Socket&gt;
        &lt;Async name="async"&gt;
            &lt;AppenderRef ref="socket" /&gt;
        &lt;/Async&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;root level="info"&gt;
            &lt;AppenderRef ref="async" /&gt;
        &lt;/root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;</pre>
</li>
<li>
<p data-renderer-start-pos="1277">Create Logstash pipeline.<br />
In logstash server, create config file in /etc/logstash/conf.d/ and add content below.<br />
Command</p>
<div class="code-block sc-mLCjK hjHAXD">
<div>
<div>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">## Sample service name: User management service.
sudo vi /etc/logstash/conf.d/user-management-service-logstash.conf</pre>
</div>
</div>
</div>
<p data-renderer-start-pos="1516">Content</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">## This sample use port 19721 to collect log from socket call in JSON format.
input {
  tcp {
      port =&gt; 19721
      codec =&gt; json
  }
  stdin {}
}

output {
  stdout {
    codec =&gt; rubydebug
  }
  elasticsearch {
    action =&gt; "index"
    hosts =&gt; [ "10.3.11.114:9200" ]
    index =&gt; "user-management-service-%{+YYYY.MM.dd}"
  }
}</pre>
</li>
<li>
<p data-renderer-start-pos="1864">Add index pattern in Kibana.<br />
In ELK main menu, click <strong data-renderer-mark="true">Stack Management</strong> &gt; <strong data-renderer-mark="true">Index Patterns</strong> &gt; <strong data-renderer-mark="true">Create index pattern</strong>.</p>
<div class="rich-media-item mediaSingleView-content-wrap image-center sc-fHlXLc iKDrDO sc-idjmjb fovMks" data-layout="center" data-node-type="mediaSingle">
<div class="sc-evWYkj iBxsHX">
<div class="sc-ftesFE cmtGYJ" data-context-id="2108260370" data-type="file" data-node-type="media" data-width="1714" data-height="797" data-id="623d7474-f6b2-429d-a06c-9bd0af53118a" data-collection="contentId-2108260370" data-file-name="image-20210210-073158.png" data-file-size="110061" data-file-mime-type="image/png">
<div class="sc-kREsUy HHPbb" data-testid="media-card-view">
<div class="media-file-card-view sc-OzIbW dEVzPi" data-testid="media-file-card-view" data-test-media-name="image-20210210-073158.png" data-test-status="complete" data-test-progress="1"><img data-recalc-dims="1" decoding="async" class="alignnone size-medium wp-image-1335" src="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?resize=300%2C139&#038;ssl=1" alt="" width="300" height="139" srcset="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?resize=300%2C139&amp;ssl=1 300w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?resize=1024%2C476&amp;ssl=1 1024w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?resize=768%2C357&amp;ssl=1 768w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?resize=1536%2C714&amp;ssl=1 1536w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?w=1714&amp;ssl=1 1714w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_31_41-Index-patterns-Elastic-Brave.png?w=1356&amp;ssl=1 1356w" sizes="(max-width: 300px) 100vw, 300px" /></div>
</div>
</div>
</div>
</div>
</li>
<li>
<p data-renderer-start-pos="1981">Configure new index pattern<br />
In <strong data-renderer-mark="true">Index pattern name</strong>, input user-management-service* and un-select <strong data-renderer-mark="true">include system and hidden indices</strong>. After click <strong data-renderer-mark="true">Net step</strong>, select @timestamp in <strong data-renderer-mark="true">Time field</strong> and click <strong data-renderer-mark="true">Create index pattern</strong>.</p>
<div class="rich-media-item mediaSingleView-content-wrap image-center sc-fHlXLc iKDrDO sc-idjmjb bPlSwu" data-layout="center" data-node-type="mediaSingle">
<div class="sc-evWYkj fGMRNA">
<div class="sc-ftesFE cmtGYJ" data-context-id="2108260370" data-type="file" data-node-type="media" data-width="1226" data-height="561" data-id="3c1c2949-0ba1-49c3-93a4-09dd293832ea" data-collection="contentId-2108260370" data-file-name="image-20210210-073315.png" data-file-size="61313" data-file-mime-type="image/png"></div>
</div>
</div>
<div class="rich-media-item mediaSingleView-content-wrap image-center sc-fHlXLc iKDrDO sc-idjmjb dXRDJq" data-layout="center" data-node-type="mediaSingle">
<div class="sc-evWYkj kFcTbK">
<div class="sc-ftesFE cmtGYJ" data-context-id="2108260370" data-type="file" data-node-type="media" data-width="1228" data-height="580" data-id="fbf10e1d-9b29-4330-a078-74ae22307410" data-collection="contentId-2108260370" data-file-name="image-20210210-073439.png" data-file-size="58267" data-file-mime-type="image/png">
<div class="sc-kREsUy eKxqiM" data-testid="media-card-view">
<div class="media-file-card-view sc-OzIbW dEVzPi" data-testid="media-file-card-view" data-test-media-name="image-20210210-073439.png" data-test-status="complete" data-test-progress="1"><img data-recalc-dims="1" decoding="async" class="alignnone size-medium wp-image-1336" src="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_32_41-Create-index-pattern-Elastic-Brave.png?resize=300%2C137&#038;ssl=1" alt="" width="300" height="137" srcset="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_32_41-Create-index-pattern-Elastic-Brave.png?resize=300%2C137&amp;ssl=1 300w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_32_41-Create-index-pattern-Elastic-Brave.png?resize=1024%2C469&amp;ssl=1 1024w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_32_41-Create-index-pattern-Elastic-Brave.png?resize=768%2C351&amp;ssl=1 768w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_32_41-Create-index-pattern-Elastic-Brave.png?w=1226&amp;ssl=1 1226w" sizes="(max-width: 300px) 100vw, 300px" /><br />
<img data-recalc-dims="1" decoding="async" class="alignnone size-medium wp-image-1337" src="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_34_22-Create-index-pattern-Elastic-Brave.png?resize=300%2C142&#038;ssl=1" alt="" width="300" height="142" srcset="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_34_22-Create-index-pattern-Elastic-Brave.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_34_22-Create-index-pattern-Elastic-Brave.png?resize=1024%2C484&amp;ssl=1 1024w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_34_22-Create-index-pattern-Elastic-Brave.png?resize=768%2C363&amp;ssl=1 768w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_34_22-Create-index-pattern-Elastic-Brave.png?w=1228&amp;ssl=1 1228w" sizes="(max-width: 300px) 100vw, 300px" /></div>
</div>
</div>
</div>
</div>
</li>
<li>
<p data-renderer-start-pos="2207">Add index pattern to log.<br />
In ELK main screen, click Logs &gt; Settings<br />
In Log indices, append index pattern created in previous step.</p>
<div class="rich-media-item mediaSingleView-content-wrap image-center sc-fHlXLc iKDrDO sc-idjmjb hOxUJY" data-layout="center" data-node-type="mediaSingle">
<div class="sc-evWYkj chFcQE">
<div class="sc-ftesFE cmtGYJ" data-context-id="2108260370" data-type="file" data-node-type="media" data-width="1183" data-height="605" data-id="a911b6ec-f959-4b84-822f-82bee5803115" data-collection="contentId-2108260370" data-file-name="image-20210210-073935.png" data-file-size="55479" data-file-mime-type="image/png">
<div class="sc-kREsUy bHfldN" data-testid="media-card-view">
<div>
<div class="media-file-card-view sc-OzIbW dEVzPi" data-testid="media-file-card-view" data-test-media-name="image-20210210-073935.png" data-test-status="complete" data-test-progress="1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-medium wp-image-1339" src="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_39_25-Logs-Kibana-Brave.png?resize=300%2C153&#038;ssl=1" alt="" width="300" height="153" srcset="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_39_25-Logs-Kibana-Brave.png?resize=300%2C153&amp;ssl=1 300w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_39_25-Logs-Kibana-Brave.png?resize=1024%2C524&amp;ssl=1 1024w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_39_25-Logs-Kibana-Brave.png?resize=768%2C393&amp;ssl=1 768w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_39_25-Logs-Kibana-Brave.png?w=1183&amp;ssl=1 1183w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div>
</div>
</div>
</div>
</div>
</div>
</li>
<li>
<p data-renderer-start-pos="2345">Test.<br />
In ELK main screen, click Log and select log in filter, if result shown, means integration success.</p>
<div class="rich-media-item mediaSingleView-content-wrap image-center sc-fHlXLc iKDrDO sc-idjmjb imJrUo" data-layout="center" data-node-type="mediaSingle">
<div class="sc-evWYkj kdwZsN">
<div class="sc-ftesFE cmtGYJ" data-context-id="2108260370" data-type="file" data-node-type="media" data-width="1917" data-height="941" data-id="f27713bb-b6a9-41a8-b6f0-73b4ae51bb6a" data-collection="contentId-2108260370" data-file-name="image-20210210-074149.png" data-file-size="176572" data-file-mime-type="image/png">
<div class="sc-kREsUy wSaWA" data-testid="media-card-view">
<div class="media-file-card-view sc-OzIbW dEVzPi" data-testid="media-file-card-view" data-test-media-name="image-20210210-074149.png" data-test-status="complete" data-test-progress="1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-medium wp-image-1340" src="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?resize=300%2C147&#038;ssl=1" alt="" width="300" height="147" srcset="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?resize=300%2C147&amp;ssl=1 300w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?resize=1024%2C503&amp;ssl=1 1024w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?resize=768%2C377&amp;ssl=1 768w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?resize=1536%2C754&amp;ssl=1 1536w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?w=1917&amp;ssl=1 1917w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-10-15_40_45-Window.png?w=1356&amp;ssl=1 1356w" sizes="auto, (max-width: 300px) 100vw, 300px" /></div>
</div>
</div>
</div>
</div>
</li>
</ol>
]]></content:encoded>
					
					<wfw:commentRss>https://www.chunho-ling.com/spring-enable-logging-to-elk-with-log4j2-and-sprint-cloud-sleuth/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1334</post-id>	</item>
		<item>
		<title>[Spring] Resolve issue on no trace Id and spin Id logged</title>
		<link>https://www.chunho-ling.com/spring-resolve-issue-on-no-trace-id-and-spin-id-logged/</link>
					<comments>https://www.chunho-ling.com/spring-resolve-issue-on-no-trace-id-and-spin-id-logged/#respond</comments>
		
		<dc:creator><![CDATA[C.H. Ling]]></dc:creator>
		<pubDate>Fri, 05 Feb 2021 09:15:11 +0000</pubDate>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Cloud]]></category>
		<category><![CDATA[Spring Cloud Sleuth]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Springboot]]></category>
		<guid isPermaLink="false">https://www.chunho-ling.com/?p=1329</guid>

					<description><![CDATA[Spring Cloud Sleuth is one of the spring cloud component which used to corelate log between different service. It is useful when tracking problem and it can draw the full picture from start to end. <a class="mh-excerpt-more" href="https://www.chunho-ling.com/spring-resolve-issue-on-no-trace-id-and-spin-id-logged/" title="[Spring] Resolve issue on no trace Id and spin Id logged">[...]</a>]]></description>
										<content:encoded><![CDATA[<p>Spring Cloud Sleuth is one of the spring cloud component which used to corelate log between different service. It is useful when tracking problem and it can draw the full picture from start to end.</p>
<p>However, it only apply for controller to diagnosis. spin and trace id will miss when exception thrown. This article will status how to resolve this problem.</p>
<p><span id="more-1329"></span></p>
<p>In Springboot MVC project, it simply add new ControllerAdvice to capture exception and it can be resolve.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java">import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class ControllerExceptionHandler {
    private static final Logger logger= LogManager.getLogger(ControllerExceptionHandler.class);

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public void handleInternalError(Exception exception) {
        logger.error(exception.getMessage(),exception);
    }
}</pre>
<p>Screenshot of test result, trace Id and spin Id shown in log.<br />
<img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-medium wp-image-1330" src="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-05-17_06_07-Window.png?resize=300%2C91&#038;ssl=1" alt="" width="300" height="91" srcset="https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-05-17_06_07-Window.png?resize=300%2C91&amp;ssl=1 300w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-05-17_06_07-Window.png?resize=1024%2C312&amp;ssl=1 1024w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-05-17_06_07-Window.png?resize=768%2C234&amp;ssl=1 768w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-05-17_06_07-Window.png?w=1483&amp;ssl=1 1483w, https://i0.wp.com/www.chunho-ling.com/wp-content/uploads/2021/02/2021-02-05-17_06_07-Window.png?w=1356&amp;ssl=1 1356w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.chunho-ling.com/spring-resolve-issue-on-no-trace-id-and-spin-id-logged/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1329</post-id>	</item>
	</channel>
</rss>
