Repository: serverless/serverless (JavaScript)
Task: Feature (2019-07-08 16:34:27+00:00)

Issue:
Allow warnings to be hidden
<!--
1. If you have a question and not a feature request please ask first at http://forum.serverless.com
2. Please check if an issue already exists. This feature may have already been requested
3. Check out and follow our Guidelines: https://github.com/serverless/serverless/blob/master/CONTRIBUTING.md
4. Fill out the whole template so we have a good overview on the issue
5. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
6. Please follow the template, otherwise we'll have to ask you to update it
-->

# This is a Feature Proposal

## Description

I am working on a project at work which makes use of **Serverless**.

I have some SSM parameters that can be intentionally left undefined, and this behavior produces a warning for each undefined parameter.

I'd like to hide these warnings.

I already have a PR for it: https://github.com/serverless/serverless/pull/6345



Discussion:

Patch:
diff --git a/lib/classes/CLI.js b/lib/classes/CLI.js
index 80299ddf43a..4e26e801733 100644
--- a/lib/classes/CLI.js
+++ b/lib/classes/CLI.js
@@ -209,6 +209,12 @@ class CLI {
     this.consoleLog(chalk.yellow.underline('Framework'));
     this.consoleLog(chalk.dim('* Documentation: https://serverless.com/framework/docs/'));
 
+    this.consoleLog('');
+
+    this.consoleLog(chalk.yellow.underline('Environment Variables'));
+    this.consoleLog(chalk.dim('* Set SLS_DEBUG=* to see debugging logs'));
+    this.consoleLog(chalk.dim('* Set SLS_WARNING_DISABLE=* to hide warnings from the output'));
+
     this.consoleLog('');
     if (!_.isEmpty(this.loadedCommands)) {
       _.forEach(this.loadedCommands, (details, command) => {
diff --git a/lib/classes/Error.js b/lib/classes/Error.js
index c5f2e3ca1a8..75f8664ea32 100644
--- a/lib/classes/Error.js
+++ b/lib/classes/Error.js
@@ -97,6 +97,10 @@ module.exports.logError = e => {
 };
 
 module.exports.logWarning = message => {
+  if (process.env.SLS_WARNING_DISABLE) {
+    return;
+  }
+
   writeMessage('Serverless Warning', message);
 };
 


Test patch:
diff --git a/lib/classes/Error.test.js b/lib/classes/Error.test.js
index 43b10139415..47938e8b14b 100644
--- a/lib/classes/Error.test.js
+++ b/lib/classes/Error.test.js
@@ -110,6 +110,20 @@ describe('Error', () => {
       expect(message).to.have.string('SLS_DEBUG=*');
     });
 
+    it('should hide warnings if SLS_WARNING_DISABLE is defined', () => {
+      process.env.SLS_WARNING_DISABLE = '*';
+
+      logWarning('This is a warning');
+      logWarning('This is another warning');
+      logError(new Error('an error'));
+
+      const message = consoleLogSpy.args.join('\n');
+
+      expect(consoleLogSpy.called).to.equal(true);
+      expect(message).to.have.string('an error');
+      expect(message).not.to.have.string('This is a warning');
+    });
+
     it('should print stack trace with SLS_DEBUG', () => {
       process.env.SLS_DEBUG = '1';
       const error = new ServerlessError('a message');