<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gajanan</title>
    <description>The latest articles on DEV Community by Gajanan (@grhegde09).</description>
    <link>https://dev.to/grhegde09</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F13224%2FmKP7IEy-.jpeg</url>
      <title>DEV Community: Gajanan</title>
      <link>https://dev.to/grhegde09</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/grhegde09"/>
    <language>en</language>
    <item>
      <title>Logging done right, makes your life bright! </title>
      <dc:creator>Gajanan</dc:creator>
      <pubDate>Mon, 27 Mar 2017 09:36:35 +0000</pubDate>
      <link>https://dev.to/grhegde09/logging-done-right-makes-your-life-bright</link>
      <guid>https://dev.to/grhegde09/logging-done-right-makes-your-life-bright</guid>
      <description>&lt;p&gt;I have been working in application development on embedded devices for a few years now. All the embedded platforms that we work on do not have a good development environment. Some platforms don't even have &lt;a href="https://en.wikipedia.org/wiki/GNU_Debugger" rel="noopener noreferrer"&gt;GDB&lt;/a&gt;. So we have to fully rely on logs to analyse the issues.  &lt;/p&gt;

&lt;p&gt;When the only way to debug your code is through logs,not everyone logs judiciously. Instead, a lot of information is logged, irrespective of the importance of the information or the frequency of logs. When you work in a team with many developers the logs can easily bloat up. In a recent project, we had so many logs that the logging rate was ~2 Mega Bytes per minute. It is a nightmare to comb through thousands of lines of logs to find the logs you are interested in and analyse issues. &lt;/p&gt;

&lt;p&gt;Following are some ways in which we improved our situation. &lt;/p&gt;

&lt;h2&gt;
  
  
  A better logger
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Multiple log levels(duh!):
&lt;/h3&gt;

&lt;p&gt;One of the most important(and common) feature for any logger is the ability to distinguish logs based on their importance. We defined the following four levels for logs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Error - For fatal error scenarios. &lt;/li&gt;
&lt;li&gt;Warning - For non-fatal errors. &lt;/li&gt;
&lt;li&gt;Info - For important information which can aid in finding problems easily. &lt;/li&gt;
&lt;li&gt;Debug - For everything else. Including function entry, exit logs to a developer's sad story! &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We added ability to select the logging level through a config file in the logger module for each run of the application. This helped us a lot. For example, we could just turn off all the logs in debug level and we would have much less logs to go through.&lt;/p&gt;

&lt;h3&gt;
  
  
  Module level logging:
&lt;/h3&gt;

&lt;p&gt;Though multiple log levels were very helpful, they were not sufficient. What we found is that the ability to log based on modules is also very important. So we created an identifier for each module(say each library we created). We added the ability to select the log level for each of these modules via a config file. This was a boon! Now we could customise the logs however we wanted. For example, if I was debugging Module A which depended on Module B, I could configure the logger module to enable debug level logs in Module A, info level logs in Module B and error level logs for all other modules.&lt;/p&gt;

&lt;h3&gt;
  
  
  A log with all the information:
&lt;/h3&gt;

&lt;p&gt;Every line in our logger had a lot of information. It included, time at which the log was written, log level, thread name which generated the log, actual log, fully qualified name of the function which generated the log, etc. Having all these fields in each log has helped us a lot in debugging the issues. &lt;/p&gt;

&lt;h3&gt;
  
  
  Runtime control:
&lt;/h3&gt;

&lt;p&gt;After we had the above features only thing left to build the ultimate logger was to add the ability to do these customisations during run time. The typical behaviour we had in our systems was that once the application is in some  problematic state, it would remain in that state. So it would be very helpful to have the ability to customise log levels for modules in run time. So we used a file watcher to watch the config file for changes and whenever a change was detected, reloaded the logger module config in memory. &lt;strong&gt;Voila! We had the ultimate logger!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Better awareness about logging
&lt;/h2&gt;

&lt;p&gt;Having a good logger is just a part of the equation. Having logs placed judiciously with appropriate information throughout the code is also very important. Not having logs in important parts of the code is as bad as having unnecessary logs in the code. Following are some thumb-rules we followed to improve out logging.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a module generates too many logs, make the logs periodic instead of event based. For example, if a video encoder is logging all the information for every frame, it will generate too many logs in a second. Instead all the information can be accumulated and logged at an interval say 1 second.&lt;/li&gt;
&lt;li&gt;Avoid redundancy in logs. i.e. Do not log function name, thread id as part of the log message as it already gets added to log by the logger.&lt;/li&gt;
&lt;li&gt;Follow the logging levels strictly.&lt;/li&gt;
&lt;li&gt;Have all the input arguments logged in Info level in the APIs.&lt;/li&gt;
&lt;li&gt;Have function Entry, Exit logs in Info level in the APIs(as an exception to the other rule.)&lt;/li&gt;
&lt;li&gt;Default log level for all utilities should be Error. This change can be done after the utilities have been thoroughly tested and not expected to have any bugs.&lt;/li&gt;
&lt;li&gt;Add a log for every

&lt;code&gt;else&lt;/code&gt;

in your code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Something extra wouldn't hurt
&lt;/h2&gt;

&lt;h3&gt;
  
  
  LogAnalyzer plugin for sublime text
&lt;/h3&gt;

&lt;p&gt;I was a windows user for first 3 years of my career. I loved using applications like Notepad++ with &lt;a href="https://sourceforge.net/projects/analyseplugin/" rel="noopener noreferrer"&gt;Analyse Plugin&lt;/a&gt; and &lt;a href="http://www.log-expert.de/" rel="noopener noreferrer"&gt;LogExpert&lt;/a&gt;. I used to love these tools as they helped me in analysing the logs quickly. Since then I have switched to macOs and have struggled to get a good tool to analyse logs. Notepad++ on wine is OK, but key bindings are a pain. TextWrangler is a good tool. But I wish it did more than what it does.&lt;/p&gt;

&lt;p&gt;So I decided to write a small plugin on Sublime text to help me analyse the logs. I wrote some dirty code over a weekend to get the basic functionality I wanted and it has pretty much stuck at that state. Please feel free to use/improve the plugin &lt;a href="https://gitlab.com/grhegde09/LogAnalyzer" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.meme.am%2Finstances%2F500x%2F76282612%2Fspock-log-well-live-well.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.meme.am%2Finstances%2F500x%2F76282612%2Fspock-log-well-live-well.jpg" title="Log well. Live well!" alt="Log well. Live well!"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>debugging</category>
      <category>logging</category>
      <category>c</category>
    </item>
  </channel>
</rss>
