<?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>Logging &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/logging/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 04:00:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Logging &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>GCP Logging Agent: Converting Unstructured to Structured Logging</title>
		<link>https://myshittycode.com/2020/06/20/gcp-logging-agent-converting-unstructured-to-structured-logging/</link>
					<comments>https://myshittycode.com/2020/06/20/gcp-logging-agent-converting-unstructured-to-structured-logging/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sun, 21 Jun 2020 01:56:35 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Google Cloud Platform]]></category>
		<category><![CDATA[Logging]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1158</guid>

					<description><![CDATA[<p>BACKGROUND The GCP logging agent uses modified fluentd, which allows us to do either unstructured logging or structured logging. The structured logging relies on JSON payload while the unstructured logging can be any texts. The advantage of structured logging is we can leverage log features in GCP Log Viewer. UNSTRUCTURED LOGGING Installing the unstructured logging [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2020/06/20/gcp-logging-agent-converting-unstructured-to-structured-logging/">GCP Logging Agent: Converting Unstructured to Structured Logging</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">BACKGROUND</h2>



<p>The GCP logging agent uses modified <a href="https://docs.fluentd.org/" target="_blank" rel="noopener">fluentd</a>, which allows us to do either unstructured logging or structured logging. The structured logging relies on JSON payload while the unstructured logging can be any texts. The advantage of structured logging is we can leverage log features in GCP Log Viewer.</p>



<h2 class="wp-block-heading">UNSTRUCTURED LOGGING</h2>



<p>Installing the unstructured logging is straightforward:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
curl -sSO https://dl.google.com/cloudagents/add-logging-agent-repo.sh
sudo bash add-logging-agent-repo.sh
sudo yum install -y google-fluentd
sudo yum install -y google-fluentd-catch-all-config
</pre></div>


<p>To capture a specific log file, create a config file under <code>/etc/google-fluentd/config.d</code> dir, ex:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
# /etc/google-fluentd/config.d/test.conf

&amp;lt;source&gt;
  @type tail
  format none
  path /tmp/test.log
  pos_file /var/lib/google-fluentd/pos/test.pos
  tag test
  read_from_head true
&amp;lt;/source&gt;
</pre></div>


<p>Finally, start the service.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo systemctl restart google-fluentd
</pre></div>


<p><code>format none</code> indicates no additional processing will be done. So, the logs appear as &#8220;flat&#8221; texts.</p>



<p>Using unstructured logging, the severity indicator never gets set, so we lose the color coding and filtering capabilities in GCP Log Viewer, ex:-</p>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="1766" height="1118" src="https://myshittycode.com/wp-content/uploads/2020/06/gcp-unstructured-logging-1.png?x45560" alt="gcp-unstructured-logging" class="wp-image-1162" srcset="https://myshittycode.com/wp-content/uploads/2020/06/gcp-unstructured-logging-1.png 1766w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-unstructured-logging-1-300x190.png 300w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-unstructured-logging-1-1024x648.png 1024w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-unstructured-logging-1-768x486.png 768w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-unstructured-logging-1-1536x972.png 1536w" sizes="(max-width: 1766px) 100vw, 1766px" /></figure>



<h2 class="wp-block-heading">JOURNEY TO STRUCTURED LOGGING</h2>



<p>Installing structured logging is very similar to unstructured logging with one small change:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; highlight: [4]; title: ; notranslate">
curl -sSO https://dl.google.com/cloudagents/add-logging-agent-repo.sh
sudo bash add-logging-agent-repo.sh
sudo yum install -y google-fluentd
sudo yum install -y google-fluentd-catch-all-config-structured
</pre></div>


<p>Create a config file, but the content is slightly more complicated depending on how granular we want to capture the structured log data:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [5,6,7,8]; title: ; notranslate">
# /etc/google-fluentd/config.d/test.conf

&amp;lt;source&gt;
  @type tail
  format multiline
  format_firstline /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/
  format1 /^(?&amp;lt;time&gt;&#x5B;^ ]*) \&#x5B;(?&amp;lt;severity&gt;&#x5B;^\] ]*).*?\] - (?&amp;lt;message&gt;.*)$/
  time_format %Y-%m-%dT%H:%M:%S.%NZ
  path /tmp/test.log
  pos_file /var/lib/google-fluentd/pos/test.pos
  tag test
  read_from_head true
&amp;lt;/source&gt;
</pre></div>


<p>Given the unstructured logs may span multiple lines, <a href="https://docs.fluentd.org/v/0.12/parser/multiline" target="_blank" rel="noopener">multiline</a> parser is ideal. Here&#8217;s an example of logs that span multiple lines:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [1,2,3]; title: ; notranslate">
2020-06-20T20:00:00.107Z &#x5B;INFO ] - Storage garbage collector report:
Number of binaries:      0
Total execution time:    4 millis
2020-06-20T20:14:25.525Z &#x5B;INFO ] - Starting to cleanup incomplete Release Bundles
2020-06-20T20:14:25.533Z &#x5B;WARN ] - Finished incomplete Release Bundles cleanup
2020-06-20T20:31:00.167Z &#x5B;ERROR ] - Start cleaning expired sessions
</pre></div>


<p><code>format_firstline</code> indicates when to stop parsing when scanning through multiple lines.</p>



<p>There can be multiple <code>format[N]</code>, depending on how granular we want to capture the structured log data.</p>



<p>Under <code>format1</code>, there are several reserved captured group names used in this example. <code>time</code> indicates log time, <code>severity</code> activates color coding in GCP Log Viewer and <code>message</code> indicates the log message.</p>



<p><code>time_format</code> formats values from <code>time</code> so that GCP Log Viewer can convert the UTC timezone to the local timezone.</p>



<p>Custom captured group names can also be used, and they will appear in under <code>jsonPayload</code> in GCP Log Viewer, ex:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; highlight: [4,5,6]; title: ; notranslate">
{
  insertId: &quot;6un3n1flz5dd3y2pv&quot;
  jsonPayload: {
    class_line_num: &quot;o.j.x.c.h.XrayHeartbeatImpl:55&quot;
    thread: &quot;http-nio-8081-exec-8&quot;
    trace_id: &quot;42998b6c3c8d8a8&quot;
  }
  labels: {…}
  logName: &quot;projects/ml-mps-cpl-ajfrog-p-ea94/logs/jfrog_artifactory%2Fartifactory_service_log&quot;
  receiveTimestamp: &quot;2020-06-20T23:46:45.123156405Z&quot;
  resource: {…}
  severity: &quot;INFO&quot;
  timestamp: &quot;2020-06-20T23:46:43.188Z&quot;
}
</pre></div>


<p>Here&#8217;s an example of a fully converted structured logging in GCP Log Viewer:-</p>



<figure class="wp-block-image"><img decoding="async" width="1776" height="1122" src="https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-1.png?x45560" alt="gcp-structured-logging" class="wp-image-1161" srcset="https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-1.png 1776w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-1-300x190.png 300w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-1-1024x647.png 1024w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-1-768x485.png 768w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-1-1536x970.png 1536w" sizes="(max-width: 1776px) 100vw, 1776px" /></figure>



<p>Now, we can easily filter the logs by severity:-</p>



<figure class="wp-block-image"><img decoding="async" width="1768" height="1116" src="https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-filtering-1.png?x45560" alt="gcp-structured-logging-filtering" class="wp-image-1160" srcset="https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-filtering-1.png 1768w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-filtering-1-300x189.png 300w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-filtering-1-1024x646.png 1024w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-filtering-1-768x485.png 768w, https://myshittycode.com/wp-content/uploads/2020/06/gcp-structured-logging-filtering-1-1536x970.png 1536w" sizes="(max-width: 1768px) 100vw, 1768px" /></figure>



<h2 class="wp-block-heading">A COUPLE OF HELPFUL TIPS</h2>



<h3 class="wp-block-heading">Building Regular Expression</h3>



<p>Building a robust regular expression is very painful, but you can use tools such as <a href="https://regex101.com/" target="_blank" rel="noopener">https://regex101.com</a> to construct and test the regular expressions first before pasting in the config file.</p>



<h3 class="wp-block-heading">Checking for Errors</h3>



<p>When tweaking the config file, the quickest way to verify the correctness of it is to restart the service. Then, run this command, check for any errors, rinse and repeat:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
tail /var/log/google-fluentd/google-fluentd.log
</pre></div>


<h3 class="wp-block-heading">Speed Up Testing</h3>



<p>Instead of waiting for the app to push logs into the log file, you can manually append the mock data to the log file, ex:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
cat &lt;&lt;EOF &gt;&gt;/tmp/myshittycode_structured.log
2020-06-20T20:00:00.107Z &#x5B;INFO ] - Storage garbage collector report:
Number of binaries:      0
Total execution time:    4 millis
2020-06-20T20:14:25.525Z &#x5B;INFO ] - Starting to cleanup incomplete Release Bundles
2020-06-20T20:14:25.533Z &#x5B;WARN ] - Finished incomplete Release Bundles cleanup
2020-06-20T20:31:00.167Z &#x5B;ERROR ] - Start cleaning expired sessions
EOF
</pre></div>


<p>Then, run this command to ensure there are no &#8220;pattern not match&#8221; errors:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
tail /var/log/google-fluentd/google-fluentd.log
</pre></div>


<p>If there are no errors, the logs will eventually appear in GCP Log Viewer.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2020/06/20/gcp-logging-agent-converting-unstructured-to-structured-logging/">GCP Logging Agent: Converting Unstructured to Structured Logging</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2020/06/20/gcp-logging-agent-converting-unstructured-to-structured-logging/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1158</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-20 06:29:47 by W3 Total Cache
-->