Skip to content

Logging/emitters refactoring#1103

Merged
KPrasch merged 8 commits intonucypher:sekanjabinfrom
fjarri:output-refactoring
Jul 25, 2019
Merged

Logging/emitters refactoring#1103
KPrasch merged 8 commits intonucypher:sekanjabinfrom
fjarri:output-refactoring

Conversation

@fjarri
Copy link
Contributor

@fjarri fjarri commented Jun 26, 2019

A reworked PR #1079, branched off the current croquetas, since there were a lot of changes introduced there.

Changes:

  • All options are now specified after commands. That is, if before one would call

      $ nucypher alice --debug init --federated-only
    

    after the PR the syntax would be

      $ nucypher alice init --debug --federated-only
    

    Pros:

    • helps avoid code duplication (for option definition and processing).
    • helps user discover these options in --help. That is, if before, in order to find out about --debug, one would have to call nucypher --help in addition to nucypher alice --help, now just nucypher alice --help will be enough - all the options will be there.
  • A console output emitter is created only once (in NucypherClickConfig) and propagated to all classes/functions that need it.

  • All click.echo() and click.secho() calls are replaced with emitter.echo() calls, so that it can control that output too.

  • Instead of dispatching by keyword arguments in emitter(), specialized methods are used (banner(), message(), ipc(), echo()).

  • Output trapping in emitters was removed, since it did not seem to be used anywhere.

  • Added --log-level option from PR Fixes debug docker problem. #1095

  • --verbosity and --quiet now govern console output only (not console observer for logs). The output shown with --quiet on should remain the same as before, --verbosity currently only sets the state of the emitter - actually using it is a matter for another PR. Probably enough to close issue Implement CLI Verbosity Levels and Toggle #1018

  • NOTE: before, when --quiet was on, logs were written to text file, but not to JSON file. Not sure if it was an omission or a conscious design decision. Now both JSON and text logs are turned on or off together, it can be rolled back.

  • Parsing boolean envvars in NucypherClickConfig more rigorously using strtobool().

  • Added specific options --console-logs/--no-console-logs, --file-logs/--no-file-logs and --sentry-logs/--no-sentry-logs to override environment variables without resorting to --debug. Also made option description slightly more detailed.

TODO (may be better to leave for another PR):

  • Add --no-fun for turning off banners?

  • Instead of specifying color in echo() and message(), use only semantic markup and leave colorizing to the emitter.

  • Go through emitter.message() and emitter.echo() calls and decide which verbosity levels they should have.

To decide (probably for another PR too):

  • What to do with a few remaining click.secho() and print() which lie beyond the scope of NucypherClickConfig

  • characters/control/controllers.py still creates independent emitters; need to figure out if it is really necessary, and if not, how to propagate the main emitter there

@fjarri fjarri added Enhancement New or improved features CLI This effects the nucypher CLI Logging Related to console or file logging labels Jun 26, 2019
@fjarri fjarri requested a review from KPrasch June 26, 2019 04:47
@codecov
Copy link

codecov bot commented Jun 26, 2019

Codecov Report

Merging #1103 into sekanjabin will increase coverage by 1.38%.
The diff coverage is 76.29%.

Impacted file tree graph

@@              Coverage Diff               @@
##           sekanjabin    #1103      +/-   ##
==============================================
+ Coverage          82%   83.39%   +1.38%     
==============================================
  Files              72       71       -1     
  Lines            9733     9704      -29     
==============================================
+ Hits             7982     8093     +111     
+ Misses           1751     1611     -140
Impacted Files Coverage Δ
nucypher/characters/chaotic.py 70.24% <ø> (+0.13%) ⬆️
nucypher/cli/characters/moe.py 64% <0%> (-2.67%) ⬇️
nucypher/cli/main.py 100% <100%> (+11.29%) ⬆️
nucypher/characters/control/interfaces.py 96.26% <100%> (ø) ⬆️
nucypher/cli/characters/enrico.py 71.05% <100%> (+1.82%) ⬆️
nucypher/cli/characters/bob.py 68.83% <50%> (-0.4%) ⬇️
nucypher/cli/status.py 78.57% <50%> (ø) ⬆️
nucypher/cli/stake.py 70.96% <55%> (-0.56%) ⬇️
nucypher/cli/characters/felix.py 69.56% <56.25%> (-1.09%) ⬇️
nucypher/characters/control/controllers.py 88% <57.14%> (ø) ⬆️
... and 26 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update df10a43...b40edc4. Read the comment docs.

@fjarri fjarri changed the title Logging/emitters refactoring [WIP] Logging/emitters refactoring Jun 26, 2019
@fjarri
Copy link
Contributor Author

fjarri commented Jun 26, 2019

(Sorry, forgot to set it as draft, please consider it to be one)

@fjarri fjarri requested a review from jMyles June 26, 2019 05:53
@fjarri fjarri changed the title [WIP] Logging/emitters refactoring Logging/emitters refactoring Jun 27, 2019
@fjarri fjarri force-pushed the output-refactoring branch from 952086a to ebb5840 Compare July 17, 2019 22:03
@fjarri fjarri changed the base branch from mimosa to master July 17, 2019 22:04
@fjarri fjarri force-pushed the output-refactoring branch from ebb5840 to ee12071 Compare July 17, 2019 22:09
@fjarri fjarri changed the base branch from master to sekanjabin July 17, 2019 22:14
@fjarri fjarri force-pushed the output-refactoring branch from 966757a to 289c716 Compare July 18, 2019 04:51
Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good refactor. I really like the emitter calls, they are much cleaner. Some comments mostly around the use of emitter.error.

I guess one catch was that we have to pass emitters to some functions. Is there a way around this? Even if not, I'm still pro this PR.

def select_client_account(blockchain, prompt: str = None, default=0) -> str:
enumerated_accounts = dict(enumerate(blockchain.client.accounts))
for index, account in enumerated_accounts.items():
click.secho(f"{index} | {account}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably, this line and similar lines in the other methods below should be using the emitter instead? I guess this change was part of a merge, after your changes...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are called outside of the scope of NucypherClickConfig, so I'm not sure what to do with them. I suppose an emitter can be created in deploy() and passed to them.

raise
else:
click.secho(str(e), fg='red', bold=True)
emitter.echo(str(e), color='red', bold=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason we don't want to simply log using emitter.error because of verbosity and color? There is an associated comment for the emitter.error method to allow colour to be specified as a parameter, would verbosity make sense as well? - default verbosity would be 1, but could be specified otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to overcomplicate the PR and only limit it to global changes. The next one would eliminate explicit color/boldness specification in emitter calls and use some semantic marking instead.


else:
raise ValueError('Either "response" dict or "message" str is required, but got neither.')
def error(self, e):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allow colour to be specified as a parameter for error logging? - red can be default. Should bold=True also be the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid that, see above (and TODO in the PR description). I would rather eliminate all explicit style specification from the code, since it is hard to synchronize it between people, and you can never be sure whether to use red, or green, or some other color in some specific case - semantic calls like error() or warning() are more clear.

raise
else:
click.secho(str(e), fg='red', bold=True)
emitter.echo(str(e), color='red', bold=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about emitter.error as above.

raise
else:
click.secho(str(e), fg='red', bold=True)
emitter.echo(str(e), color='red', bold=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use emitter.error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan for this PR was to just switch to explicit emitter usage without changing any related calls (to make sure I'm not changing the logging behavior - e.g. error() would log the error, while echo() doesn't). We can discuss any changes to logging strategies later when we get the blocking changes out of the way.

raise ValueError('Either "response" dict or "message" str is required, but got neither.')
def error(self, e):
if self.verbosity >= 1:
e_str = str(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add the class name of the error as well? i.e. e.__class__.__name__. Without it we are hoping that the message alone provides sufficient context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, this PR intends to change the emitter API while preserving the current behavior (because I'm still not sure what kind of logic is needed in different cases).

try:
blockchain.connect(fetch_registry=False, sync_now=sync)
except BlockchainDeployerInterface.ConnectionFailed as e:
emitter.echo(str(e), color='red', bold=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emitter.error?

@fjarri fjarri force-pushed the output-refactoring branch 2 times, most recently from b40edc4 to 8a16b37 Compare July 23, 2019 05:54
Copy link
Contributor

@michwill michwill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

fjarri added 7 commits July 25, 2019 09:14
…ties/logging

Also add a check that Sentry SDK is only initialized once
There's was not much happening between the beginning of start() and echo(),
so moving the status printing should not obscure anything.
…and add specific options for each type of logging

Now quiet/verbose govern console output (emitters)
@KPrasch KPrasch force-pushed the output-refactoring branch from 8a16b37 to cc93cce Compare July 25, 2019 16:32
@KPrasch KPrasch force-pushed the output-refactoring branch from cc93cce to 84e1f49 Compare July 25, 2019 17:04
@KPrasch KPrasch merged commit 7da9cde into nucypher:sekanjabin Jul 25, 2019
@fjarri fjarri deleted the output-refactoring branch July 27, 2019 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLI This effects the nucypher CLI Enhancement New or improved features Logging Related to console or file logging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants