<?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: Mohamed El Sayad (Anubis)</title>
    <description>The latest articles on DEV Community by Mohamed El Sayad (Anubis) (@melsayad).</description>
    <link>https://dev.to/melsayad</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%2F356222%2F2eec84ef-3b14-46e1-ad09-ded82d8fb84b.png</url>
      <title>DEV Community: Mohamed El Sayad (Anubis)</title>
      <link>https://dev.to/melsayad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/melsayad"/>
    <language>en</language>
    <item>
      <title>winston-sugar makes your winston logger configurations more easy</title>
      <dc:creator>Mohamed El Sayad (Anubis)</dc:creator>
      <pubDate>Mon, 06 Apr 2020 18:28:08 +0000</pubDate>
      <link>https://dev.to/melsayad/winston-sugar-makes-your-winston-logger-configurations-more-easy-42n9</link>
      <guid>https://dev.to/melsayad/winston-sugar-makes-your-winston-logger-configurations-more-easy-42n9</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.npmjs.com/package/winston-sugar"&gt;winston-sugar&lt;/a&gt;&lt;/strong&gt; is a syntactical sugar on top of &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/winston"&gt;winston&lt;/a&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/winston-sugar"&gt;winston-sugar&lt;/a&gt;&lt;/strong&gt; makes your &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/winston"&gt;winston&lt;/a&gt;&lt;/strong&gt; configurations more easy by using a single .json configurations file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;strong&gt;npm&lt;/strong&gt; command  - &lt;code&gt;npm i winston-sugar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.npmjs.com/package/winston-sugar"&gt;winston-sugar&lt;/a&gt;&lt;/strong&gt; will create a folder name &lt;code&gt;config&lt;/code&gt; if not exist and generate &lt;code&gt;winston.json&lt;/code&gt; file template, this .json file will me the single point of all of your configurations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Edit your json configurations file template.&lt;/li&gt;
&lt;li&gt;Require &lt;code&gt;winston-sugar&lt;/code&gt; to get a class &lt;code&gt;WinstonLoader&lt;/code&gt; instance.&lt;/li&gt;
&lt;li&gt;Load your &lt;code&gt;winston.Logger' configurations by passing the .json path to the&lt;/code&gt;config` function.&lt;/li&gt;
&lt;li&gt;Get your &lt;code&gt;winston.Logger' instance by calling &lt;/code&gt;getLogger()&lt;code&gt; function, you can add &lt;/code&gt;category` as string parameter if you need to have a child logger for a specific module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; you need to call &lt;code&gt;config&lt;/code&gt; only one time from the entry point of your application. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After that your can only do like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;const logger = require('winston-sugar').getLogger('app');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Import winson-sugar when you need to have a winston instance
const winstonLoader = require('winston-sugar');

// This should be called in the application entry point only.
winstonLoader.config('../config/winston.json');

// Get winston logger
const log = winstonLoader.getLogger('app');

// Enjoy and log using winston
log.info("It's me winson-sugar :) "); 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Dependances&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="mailto:winston@3.x.x"&gt;winston@3.x.x&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build your .json winston configurations file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The .json configuration schema has 7 main parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;level&lt;/strong&gt;  - (optional) Including the main logging level value (info,debug,warn ...etc.).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ex. "level": "info"&lt;/code&gt;  - Also it's the default value in winston-sugar&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;levels&lt;/strong&gt; - (optional) Including 2 child parameters (values &amp;amp; colors) if you need to customize your levels along with colors.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ex. Customize logger levels and it's clolors

"levels":{
     "values": {
       "trace": 6,
       "debug": 5,
       "info": 4,
       "warn": 3,
       "error": 2,
       "fatal": 1,
       "mark": 0
     },
     "colors": {
       "trace": "blue",
       "debug": "cyan",
       "info": "green",
       "warn": "yellow",
       "error": "red",
       "fatal": "magenta",
       "mark": "grey"
     }
   }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;silent&lt;/strong&gt; - (optional) Use &lt;code&gt;silent&lt;/code&gt; if you need to stop the winston logger by change it's value to &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ex. "silent": false&lt;/code&gt;  - Also it's the default value in winston-sugar&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;exitOnError&lt;/strong&gt; - (optional) Use &lt;code&gt;exitOnError&lt;/code&gt; if you need to stop the winston when error occurred by change it's value to &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;"exitOnError": false&lt;/code&gt; - Also it's the default value in winston-sugar&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;format&lt;/strong&gt; - (mandatory) Array of &lt;code&gt;winston.format&lt;/code&gt; functions, just put the exact function name without (), also you can add it's options as the following example. (please check &lt;code&gt;winston.format&lt;/code&gt; from winston format section).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Extra Feature:&lt;/strong&gt; Specially for &lt;code&gt;winston.printf&lt;/code&gt; format function, I created a special syntax using templates, template is a callback function defined as string parameter, this function will be passed to &lt;code&gt;winston.printf&lt;/code&gt; function in the runtime.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ex. Adding two winston.format functions, winston-sugar will combine all format functions on behalf of you :)

"format": [
    {
      "type": "timestamp",  
      "options": {
        "format": "YYYY-MM-DDThh:mm:ss.SSS"
      }
    },
    {
      "type": "printf", 
      "options": {
        "template": "custom" // the name of the custom template.
      }
    }
  ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;transports&lt;/strong&gt; - (mandatory) Array of &lt;code&gt;winston.transports&lt;/code&gt; functions, just put the exact transport name, also you can add it's options as the following example. (please check &lt;code&gt;winston.transports&lt;/code&gt; from winston transports section).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Extra Feature:&lt;/strong&gt; &lt;br&gt;
Now you can restrict your transport for a specific levels using &lt;code&gt;filters&lt;/code&gt;, I created a special syntax using templates, let's say you have 2 &lt;code&gt;winston.transports.File&lt;/code&gt; you can configure the first to log &lt;code&gt;info&lt;/code&gt; messages only, and the last for &lt;code&gt;error&lt;/code&gt; and &lt;code&gt;fatal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also using the new introduced &lt;code&gt;env&lt;/code&gt; parameter, you can easily configure each transport to be run only for a specific environment. for this option &lt;code&gt;winston-sugar&lt;/code&gt; will read define the application running environment from &lt;code&gt;NODE_ENV&lt;/code&gt; global parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ex. Adding 3 types of transports, one is type of Console for development, and the rest of type File for production.

"transports": [
    {
      "type": "Console",
      "name": "dev-logger",
      "env": "development",
      "options": {
        "stderrLevels ": [
          "fatal",
          "error"
        ],
        "consoleWarnLevels": [
          "debug",
          "warn"
        ],
        "handleExceptions": true,
        "format": [ // Override the deafult logging format.
          {
            "type": "printf",
            "options": {
              "template": "custom-colored" // the name of the custom template.
            }
          }
        ]
      }
    },
    {
      "type": "File",
      "name": "info-logger",
      "env": "production",
      "options": {
        "filename": "log/app.log",
        "maxsize": "100m",
        "maxFiles": 3,
        "tailable": true,
        "maxRetries": 3,
        "zippedArchive": true,
        "handleExceptions": true,
        "filters": [ // Override the logging level.
          "info",    // log level info and mark only.
          "mark"     
        ]
      }
    },
    {
      "type": "File",
      "name": "error-logger",
      "env": "production",
      "options": {
        "filename": "log/fatal.log",
        "maxsize": "100m",
        "maxFiles": 3,
        "tailable": true,
        "maxRetries": 3,
        "zippedArchive": true,
        "handleExceptions": true,
        "filters": [  // Override the logging level.
          "error",    // log level error and fatal only.
          "fatal"
        ]
      }
    }
  ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;printf&lt;/strong&gt; - (mandatory) Only if you are using &lt;code&gt;printf&lt;/code&gt; as a type of &lt;code&gt;winston.format&lt;/code&gt; function, you can easy defined you logging template as callback function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Check the syntax of your callback function before converting it to a string format. wrong syntax will throw an &lt;code&gt;error&lt;/code&gt; in the runtime.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ex. Three callback funcation templates

"printf": {
    "templates": {
      "custom": "(({level, message, category, timestamp})=&amp;gt; { return `[${timestamp}] [${level}] ${(category)?category:'winston'} - ${message}`;})",
      "custom-colored": "(({level, message, category, timestamp})=&amp;gt; { return winston.format.colorize({all:true}).colorize(level,`[${timestamp}] [${level.toUpperCase()}] ${(category)?category:'winston'}`) + ` - ${message}`;})",
      "custom-all": "((info) =&amp;gt; {let message = `${new Date(Date.now()).toUTCString()} | ${info.level.toUpperCase()} | ${info.message} | `; message = info.obj ? message + `data:${JSON.stringify(info.obj)} | ` : message; message = this.log_data ? message + `log_data:${JSON.stringify(this.log_data)} | ` : message; return message;})"
    }
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Configurations .json file template&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once you installed &lt;code&gt;winston-sugar&lt;/code&gt; check the &lt;code&gt;winston.json&lt;/code&gt; file under &lt;code&gt;config&lt;/code&gt; directory, and change it based on your needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Licenses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MIT License&lt;/p&gt;

&lt;p&gt;Copyright (c) 2020 Mohamed El Sayad&lt;/p&gt;

</description>
      <category>node</category>
      <category>logger</category>
      <category>winston</category>
      <category>configurations</category>
    </item>
  </channel>
</rss>
