Description
Use case description
Currently Framework CLI output lacks consistency and in many areas is unnecessarily verbose.
There are no clearly defined rules on how to present messages of different levels, and while there are some means to control the produced output (via e.g. SLS_DEBUG) they're limited and not consistently used. There's no pre-prepared logging interface that follows well established patterns we can observe in some other popular CLIs (just this use case is subject of #1720)
There's no clear guidelines and dedicated logging interface provided to plugins, which results with additional CLI output inconsistency when service is powered by multiple plugins
Currently we do not have possibility to control style of the output per environment in which given Framework logic is run, so e.g. on programmatic usage Framework does not print logs to console, or so we rely on different log reporting means if some of the internals are used in e.g. electron or browser environments.
Additionally in scope of Serverless Inc. we work on different CLI engines, ideally if all output logs share same style and use same engine, which should be provided as decoupled utility.
Proposed solution
General idea (strategy)
- Establish unified consistent style guide for Serverless Framework logs to which all Framework and its plugins CLI output ideally should adapt (that's being covered internally by @mnapoli, with feedback dedicated issue opened at v3 CLI redesign: feedback wanted #9883)
- Introduce a new programmatical interface through which all CLI output writing will be established. Interface should expose:
- Methods that allow to inform of certain events, marked with different importance level:
debug,infohidden by default, andnotice,warnanderrorshown by default - Method through which substantial (one that is not subject to log levels filtering) command output should be generated , e.g. configuration content on
sls print, or deployment summary onsls deploy. - Methods through which we can inform on the command progress, through dynamically changing content in terminal footer. I think it'll also be great here to support certain log level levels, but in that case I'd limit it to
debug,infohidden by default andnoticeshown by default. - Information on used log level. This one is controversial, and ideally if we do not have a need to rely on that, but we cannot seclude situation where design instruction will be to display different (exchangeably) log message when in verbose mode and when in not. It's simply to secure that in such event we're not blocked by technical limitations
- Methods that allow to inform of certain events, marked with different importance level:
- Introduce a dedicated interface for plugins, that exposes exactly same interface as we'd use in the Framework, but different instance of it for each plugin, so we can also easily differentiate output coming from different plugins
- (if we plan to control current plugins output) Ensure that for currently generated logs, internals of the Framework use different endpoint than one used by plugins (e.g. plugins rely on
serverless.cli.logfor that - if we want to filter its output we need to reconfigure Framework to not use it internally) - Configure new logs using new interface. They should be configured in a way so they can fully replace so far used logs, but in first implementation we will just add new logs (by default they will not produce any output) and still keep the old ones.
- Introduce a mechanism where after agreed flag is set, the old logs are silenced and new ones are presented (for development purposes we may also want to have mode where both old and new logs are printed)
- If agreed on, in same manner configure new logs in few of the most popular plugins. Here we need to be careful to not open door for situation where both log modes are printed (as given plugin version may be later used with v3 of a Framework)
- Separate log writing logic from log reporting logic. So we can easily adjust log reporting globally depending on the environment in which given logic is run, and up to user preferred output options
Implementation specification (tactics)
We need to secure following distinct logging interfaces:
- Interface for legacy logs which are meant to be hidden if we display just modern logs
- Interface for legacy Framework logs, which is meant to be shown unconditionally (have no counterpart in modern logs at given point). Additionally if we plan to control current plugins output, we need to ensure that Framework internals and plugins rely on different interface endpoints
- Modern logging interface, to be used by the Framework. Different instances of very same interfaces will be issued to each plugin individually.
Interface for legacy logs to be shown conditionally
In context of @serverless/utils/log expose following methods:
.legacy.write(data)- Under the hood simply callingprocess.stdout.write(data).legacy.consoleLog(msg)- Callinglegacy.write("${msg}\n").legacy.log(..args)- Working exactly as@serverless/utils/logdoes now, but relying internally on.legacy.write- (if we plan to control current plugins output)
.legacy.pluginWrite(data)- Under the hood simply callingprocess.stdout.write(data)
Additional notes:
- Muting of eventually generated output will be done in context of
@serverless/utils/log-reporters/nodespecified below - This interface is scheduled to be provided temporarily. After we refactor all Framework output , we should remove it from
@serverless/utils/logwith next major release.
Interface for unconditional legacy Framework logs
Changes in this area are needed only if we want to generically control output as currently produced by plugins, To achieve that we need to ensure that plugins rely on interface that Framework doesn't use.
Plugins typically rely on our serverless.cli.* interface. Therefore our internals should stop using it. This interface exposes two logging methods:
serverless.cli.log- Rich log, which currently falls back to@serverless/utils/logunder the hood. Any usage in Framework should simply be refactored to rely on@serverless/utils/logdirectly.serverless.cli.consoleLog- Basic log (technically aconsole.logcounterpart). To migrate those calls I propose to introduce.consoleLogmethod on@serverless/utils/log, and simply refactor Framework internals to use that instead.
In addition to be able to control plugins output serverless.cli.* methods will have to be updated to pass all output to .legacy.pluginWrite as proposed in previous section.
Additional notes:
- This interface (together with main
@serverless/utils/logfunction) is scheduled to be provided temporarily. After we refactor all Framework output , we should remove it from@serverless/utils/logwith next major release.
Modern logging interface
In context of @serverless/utils/log expose following interface to be used by Framework internals exclusively:
.log[debug,info,notice,warn,error,get]- To produce event logs of different importance level. An instance oflogutility - which we already use in our tests). It additionally allows to namespace logs (via.getmethod), which is very helpful for additional filtering of debug level logs which can turn to be very verbose. Those logs will be scheduled to be written tostderrstream and support print-f formatting. By defaultdebugandinfologs will be hidden (but that's in scope of logs reporter which will be configured at@serverless/utils/log-reporters/node).writeText(message[, ...messageN])- To produce substantial command output. In context of this module log intent will be registered as event, and all reporting (scheduled to be written tostdout) will remain configured externally at@serverless/utils/log-reporters/node.For convenience it'll support message injected in form of multiple strings where after each string we add\n(it's to provide aid for multi-line constructs), additionally arrays of strings could be injected as messages.
If we will run into use cases where we would not want ending new line postfix, we may introduce.writeText.rawfor direct not decorated write intent. NamedwriteText(and not more generically as e.g.writeOut) to establish that it's dedicated for human readable output (although at beginning we will handle through it all substantial output as produced by different commands, even those asgenerate-eventwhich by design produce processable output), with idea that eventually at later turn we will add alternative write methods aswriteJsondedicated for producing programmable processable output (e.g.sls printwill now usewriteTextto expose YAML configuration output , but when we introduce.writeJsonit may also produce JSON output when--jsonflag is present).progress- Progress bar interface, with single level namespacing and messaging of different importance level (by default onlynoticelevel visible). Object which exposes following methods:get(name)- Named progress instance (Progress instances of different names, handle different progress bar items). Returnsinfoandnoticehandles, on which methods through which progress content can be written or cleared:[info|notice](message[, ...messageN])- Update a dynamic status. First usage creates a progress bar item. If content is written to different levels, it's just last from level to be shown that is exposedremove()- Remove content (it's a signal that processing of given item have ended)
For plugins provide different instances of very same interface. For that in context of a @serverless/utils/log also expose:
getPluginWriters(pluginName)- Returns all above interfaces on single object, but namespaced to given plugin (invoking function multiple times with same plugin name should result with same logger instance)
Provide a backdoor means through which we can resolve currently used log level in internals. (ideally if it's not used, but we cannot seclude scenario when it'll be needed):
logLevelIndex- Index (per https://github.com/medikoo/log/blob/master/levels.json) of currently applied level. Defaults to2(notice) and in code fixed to2(to be updated by reporting interface below)isVerboseMode- Getter which internally resolves asthis.logLevelIndex > 2(true for any level lower than notice)
After having that implemented we may also start to pass writing interfaces to plugin instances at its construction
Reporting interface
Log writing interface (as specified above) is to be separate from functionality that reports logs (e.g. writes them to the terminal). It's to be able to control per environment how we report logs without a need of requiring unconditionally some environment specific logic in every environment in which internals can be run.
In context of this effort we will focus just on Node.js environment, of which reporting logic will be configured in @serverless/utils/log-reporters/node.js module. This is the reporter that will be initialized in our serverless CLI.
In case of direct programmatical usage of a Framework, no log reporting will be configured explicitly. It'll be up to user, whether to rely on same reporting logic as CLI does, come up with its own log reporting configuration, or simply do not have logs reported.
@serverless/utils/log-reporters/node.js internal scope:
It's a module to be loaded just once, and as one of the first things in the CLI program
Control log output mode via SLS_DEV_LOG_MODE environment variable, which should support bitwise value, combined of below settings
1- Show modern logs2- Hide legacy logs (those registered to be subject to this condition)4-(if we plan to control current plugins output) Hide plugin logs.
By default we assume SLS_DEV_LOG_MODE=0
- If there's an intent to hide Framework legacy logs. Override
@serverless/utils/log.legacy.writeso it's a no-op function - If there's an intent to hide current plugin logs. Override
@serverless/utils/log.legacy.pluginWriteso it's a no-op function - If there's an intent to show modern logs:
- Configure log reporter for event logs (registered via
@serverless/utils/log.log) - Goal is to resemble style on which we agreed internally. As implementation it can be extension of log-node reporter which we already use in our test suite. SupportSLS_LOG_LEVELandSLS_LOG_DEBUGto control the output. - Update
@serverless/utils/log.logLevelIndexto reflect the resolved index - After we have all logging refactored, add global support for
--verbose(triggersSLS_LOG_LEVEL=info) and--debug(triggersSLS_LOG_LEVEL=debug) flags. Let's not supportSLS_DEBUGwith modern logs - Write content of
.writeTextlogs tostdout - Configure progress output (registered via
@serverless/utils/log.progress) with respect to agreed design, and with help of cli-progress-footer (which allows to cleanly separate dynamic and permanent content). No progress data should be written in context of non TTY terminals
- Configure log reporter for event logs (registered via
Implementation roadmap
Address each step as individual PR
Note each introduced/changed interface should be accompanied with needed documentation addition and changes
- (
@serverless/utils) Implement.legacyinterface for logs to be shown conditionally
https://github.com/serverless/utils/pull/102 - (if we plan to control current plugins output) (
@serverless/utils) Implementsls.cli.consoleLogcounterpart on@serverless/utils/log.consoleLog - (
@serverless/utils) Setup modern logging interface (in a manner that same interface can be provided with different instance to plugins):- Setup event logs interface together with
logLevelIndexandisVerboseModeproperties
https://github.com/serverless/utils/pull/103 - Setup
.writeTextoutput registry and emitter
https://github.com/serverless/utils/pull/104 - Setup
.progressregistry and emitter
https://github.com/serverless/utils/pull/105 - Implement
.getPluginWriters
https://github.com/serverless/utils/pull/106
- Setup event logs interface together with
- (
@serverless/utils) Setup log reporting logic in@serverless/utils/log-reporters/node.js:- Implement
SLS_DEV_LOG_MODEresolution, together with support for2setting
https://github.com/serverless/utils/pull/107
- Implement
- (if we plan to control current plugins output) Implement
SLS_DEV_LOG_MODEresolution, together with support for4setting - Setup event logs reporter (initialized only when
1is set forSLS_DEV_LOG_MODE), with support forSLS_LOG_LEVELandSLS_LOG_DEBUGenv variables
https://github.com/serverless/utils/pull/107 - Setup output (
.writeText) reporter (initialized only when1is set forSLS_DEV_LOG_MODE)
https://github.com/serverless/utils/pull/108 - Setup progress reporter (initialized only when
1is set forSLS_DEV_LOG_MODE)
https://github.com/serverless/utils/pull/109 - (
serverless) (if we plan to control current plugins output):- Refactor all
serverless.cli.logusage, so@serverless/utils/logis used instead - Refactor all
serverless.cli.consoleLogusage so@serverless/utils/log.consoleLogis used instead
- Refactor all
- (
serverless) Ensure output writing interface is provided to plugin constructors, expose it as third argument with{ log, writeText, progress }object. - (
serverless) Introduce modern logs case by case (ach should be accomplished via (1) Reconfiguration of old logs to be hidden so@serverless/utils/log.legacyinterface is used (2) Introduction of modern logs via@serverless/utils/log[log, writeText, progress]interface):- 1st case (TBA)
- 2nd case (TBA)
- ...nth case (TBA)
- Configure support for global
--verbose(triggersSLS_LOG_LEVEL=info) and--debug(triggersSLS_LOG_LEVEL=debug) flags
Activity
medikoo commentedon Aug 26, 2021
@pgrzesik FYI I've updated slightly spec:
writeTextso it supports multiple string tokens (internally we will add new line after each automatically. It's to help constructing messages asserverless/lib/cli/interactive-setup/deploy.js
Lines 57 to 69 in 9a3f1e6
infoandnotice. Removeddebugas I'm not sure now of a use case to enrich progress at such low level, If we find it valuable we may add it afterwardsmedikoo commentedon Aug 31, 2021
@pgrzesik I've updated the spec:
logLevelIndexandisVerboseModeon@serverless/util/log, just in case we will need to configure same log messages differently depending on verbosity mode (hopefully that won't be the case)SLS_LOG_LEVELandSLS_LOG_DEBUG(fromLOG_LEVELandLOG_DEBUG). It's because we plan to expose only our internal and plugin logs, while log is also used by some of our lower level dependencies. PromotingLOG_LEVELandLOG_DEBUGwill suggest more generic approach, which we're not after at this point (in a future we may consider to provide means to expose also those logs, and thenLOG_LEVELandLOG_DEBUGcould be the right handle, but it's not the concern for a time-being)progress[info|notice].writeUpdatetoprogress[info|notice].update- I think herewritewas superfluous, as it's clear it's about writing update.Additional question towards
progressinterface.Currently proposed interface resembles pattern similar to one shared by event emitter, where first argument (
id) states the address of the message. Still after discussing things internally with @mnapoli I wonder whether it wouldn't be better to take similar approach as with log namespacing (wherelog.get('some-namespace')returns us namespased log interface which we then simply use further on without passing the namespace token with each written message)In case of progress we can provide similar interface:
While currently proposal is about:
WDYT?
pgrzesik commentedon Aug 31, 2021
I like the proposal where you can get a handle to namespaced
progressI think it will be really convenient interface for plugin authors and will help avoid making mistakes by simple typos. Big 👍 from me for this onemedikoo commentedon Aug 31, 2021
Thanks @pgrzesik I also fully agree. Big win is also better consistency with
loginterface.I've updated the spec, so it proposes a namespaced
progressnowmedikoo commentedon Sep 1, 2021
@pgrzesik when implementing
progressinterface, I've realized thatremovedoesn't make sense in scope of level interface (when processing of given item ended, we simply clear it from progress bar and in that context there's no point to differentiate depending on verbosity level),Hence it feels more accurate to put
removeaside ofinfoandnotice.Having that I've noticed that making
infoandnoticeas objects with singleupdatefunction, no longer feels sensible. It's probably better to makeinfoandnoticea directly update writing functions (same as it's in even logging interface)In light of above, I propose update where
progresswill have following methodsnotice- write progress updateinfo- write progress update specific to verbose moderemove- clear progress of this item.WDYT?
pgrzesik commentedon Sep 1, 2021
Agreed - I was thinking about having situation like
startwith infostartwith noticestartshould still be visible on infobut I don't see an actual use case for situation like that so I think simplifying the interface and making
info,noticeandremovejust simple functions makes sense to me 👍medikoo commentedon Sep 1, 2021
@pgrzesik interesting case. Still it'll mean we plan to display just fraction of progress of same item when in regular mode, and report for longer time progress of it when in verbose mode. It's hard for me to find good reasoning for such setup. However even if we find such case, it will still be possible to achieve, it'll just involve configuration with inspection of
isVerboseMode.We also still work in experimental mode, so if when configuring new logs we find that some interfaces are problematic we definitely should improve them.
I've updated the spec.
pgrzesik commentedon Sep 1, 2021
Sounds great - with this scenario I was only considering what might be not possible or harder to achieve given the proposed change, but I don't think such scenario is even realistic and as you mentioned we still would be able to have it. 👍
medikoo commentedon Sep 2, 2021
@pgrzesik FYI I've clarified a bit terminology in a spec:
I believe it's on par with terminology used in other similar tools, which separate log registry interface from reporter instances (those in popular loggers are called either transports or reporters - the latter feels for me more self-explanatory).
If you feel we can do better, please share.
pgrzesik commentedon Sep 2, 2021
Sounds good to me, seems pretty clear, thanks for clarification 👍
medikoo commentedon Oct 22, 2021
Closing as we can assume it's addressed.
It's still subject to improvements, but all logs were refactored