Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging #316

Merged
merged 5 commits into from
Mar 6, 2019
Merged

Conversation

dmfigol
Copy link
Collaborator

@dmfigol dmfigol commented Jan 20, 2019

Fix #302, #230

  • Better interoperability with native Python logging module
  • No more duplicate logs
  • Replace format and f-strings in logs to % strings according to best practices
  • Improve some logging messages
  • InitNornir configure_logging parameter is deprecated, replaced with config.logging.enabled

TODO:

  • Write a separate logging section in docs

@dmfigol
Copy link
Collaborator Author

dmfigol commented Jan 20, 2019

There are two breaking changes:

  1. Removed 'loggers' from the logging config
  2. Renamed 'file' to 'file_path' in the logging config

Makefile Outdated
@@ -34,7 +34,7 @@ pytest:
.PHONY: black
black:
${DOCKER_COMPOSE} \
run nornir black --check .
Copy link
Contributor

Choose a reason for hiding this comment

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

why these changes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The current dockerfile is built in such a way that, it is mapping the whole project dir to the container, which causes the problems for my setup when I run tests... Decided to change to include directories explicitly when black should run.
I can change it back, but I'd strongly advocate to change volume mounting in docker-compose then.

Copy link
Contributor

Choose a reason for hiding this comment

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

if that's the case let's discuss it in a separate PR to avoid noise

description="Logging format",
)
to_console: bool = Schema(
default=False, description="Whether to log to console or not"
)
loggers: List[str] = Schema(default=["nornir"], description="Loggers to configure")
Copy link
Contributor

@dbarrosop dbarrosop Jan 23, 2019

Choose a reason for hiding this comment

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

I don't think it was a widely used feature but the idea was to allow users to enable debugging in paramiko/netmiko/others without having to change the code. How do you envision that being done now and why was it removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I remove it, because I didn't really understand what problem it solves. If you want to enable logging for other modules, you can do it with standard python logging (basicConfig, dictConfig or programmatically). I can put it back if you want, but I'd vote against it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, the point is that a developer builds a tool, deploys it in a management server and then the neteng folks (or other people) consume that tool, which is installed in the $PATH and don't have access to modify it, how would you enable netmiko's debug then because something is not going as expected? You could have the developer bake it in into its own application, and that'd be a valid answer, however, if there isn't a good reason to remove it at this point (like introducing an insurmountable amount of complexity) I'd gate it until nornir 3.0.0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You are saying that a neteng would change env var in this case? I don't think it will work for lists (may be I wrong here). Or you are saying that a neteng would need to change config.yaml? If that is the case, how different it is from changing the logging setup in the entrypoint script, if both config.yaml and the application code will be in VCS?
I do think you are onto something here and may be it is a valid use-case, however it needs to be described in our docs, because until you told it, I didn't even think about it.

format: str = Schema(
default="%(asctime)s - %(name)12s - %(levelname)8s - %(funcName)10s() - %(message)s",
default="[%(asctime)s] %(levelname)-8s {%(name)s:%(lineno)d} %(message)s",
Copy link
Contributor

Choose a reason for hiding this comment

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

breaking change, might break people's logging system

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How is it breaking? It's just the format of logs.

@dbarrosop
Copy link
Contributor

dbarrosop commented Jan 23, 2019

Replace format and f-strings in logs to % strings according to best practices

which best practices? Mostly want to look at them to remove my f-string comments or not :)

There is a third breaking change, which is the format of the log.

@dbarrosop
Copy link
Contributor

I am a bit concerned about this PR introducing so many breaking changes. I think we need to either have very good reasons for them, revert them or delay this to 3.0.0.

@ktbyers thoughts?

@dbarrosop
Copy link
Contributor

dbarrosop commented Jan 23, 2019

Ok, read a bit more about it and looks like the "recommended" method is just because it's a lazy evaluator for the string. I don't think we should be concerned about that as that's going to save us 0.0000000001us of execution time and I'd rather have readable strings but to avoid bikeshedding I will let ktbyers chime in and break the tie :)

P.S: Removing the f-string comments for the sake of brevity

@ktbyers
Copy link
Collaborator

ktbyers commented Jan 23, 2019

I vote f-strings versus the % strings.

I also vote if they are backwards incompatible that we defer until Nornir 3.0 (or maybe some way to specify new-logging format in Nornir-config that then becomes the default in Nornir 3.0)? Disclaimer: I have mostly been out of the loop on this and only loosely following it.

@dmfigol
Copy link
Collaborator Author

dmfigol commented Jan 23, 2019

Those are not really % strings as % strings are interpolated at declaration time, the syntax is "%s", var (difference: comma). f-strings in logs are not "best practice" and both pylint and pycharm will yell at you unless suppressed. But I also agree that they increase readability so I guess I will change it if my opinion didn't get consensus ;)

I am honestly not sure about "breaking changes" and 3.0:

  1. file -> file_path, we can add warning to "file" being set to something other nornir.log, still use it, but file_path would take precedence
  2. We can add "loggers" back, but honestly I think they are useless. We could add a warning that they are not used anymore. We could make them work back. My take in general is that it is not nornir's job to touch logging. So if we are touching it already, the impact should be minimum. Built-in Python logging module is very powerful.
  3. I don't consider new logging format a breaking change at all.

@ktbyers
Copy link
Collaborator

ktbyers commented Jan 23, 2019

Okay, so it really just arguments that are passed to the logging subsystem.

Here is more discussion on pylint doing this (which wasn't very well received):

pylint-dev/pylint#1788

Looks like we can just disable that warning in Pylint.

@dbarrosop
Copy link
Contributor

file -> file_path

Why that change? file is not masking any actual global variable.

so I guess I will change it if my opinion didn't get consensus

I still think readability wins over this supposedly perf improvement, specially when the perf improvements is going to be measured in microseconds while nornir's application are mostly going to be bound by IO and measured in ms. So whatever kirk says as he is the one breaking the tie :)

but honestly I think they are useless

I already commented on the reasoning, and I am fine deprecating it in nornir 3 if we decide the benefits don't overweight the complexity but without a very good reason we shouldn't introduce breaking changes as per semver rules

My take in general is that it is not nornir's job to touch logging

I agree, notice it's not nornir who is doing that, it's InitNornir, which is a convenient entrypoint to the framework for people that want convenience. If you want full control you can implement your own InitNornir and instantiate the objects yourself.

I don't consider new logging format a breaking change at all.

For people sending logs to a logging system and parsing the logs to build dashboards it's going to be breaking change.

requirements.txt Outdated
@@ -7,4 +7,4 @@ future
requests
ruamel.yaml
mypy_extensions
pydantic
pydantic==0.16.1
Copy link
Contributor

Choose a reason for hiding this comment

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

don't forget to rebase as new develop uses poetry :)

@dmfigol
Copy link
Collaborator Author

dmfigol commented Jan 24, 2019

Why that change? file is not masking any actual global variable.

To make it clear that it is not filename it is a path. I will return it back.

I agree, notice it's not nornir who is doing that, it's InitNornir, which is a convenient entrypoint to the framework for people that want convenience. If you want full control you can implement your own InitNornir and instantiate the objects yourself.

We have every single example with InitNornir, we don't even discuss (by discuss I mean docs) the possibility of creating Nornir objects using some other mechanism.

For people sending logs to a logging system and parsing the logs to build dashboards it's going to be breaking change.

The current setup does not add Syslog handler at all... Do you want me to remove it back anyway?
I guess for my projects I will always do logging.enabled = False.

Something that I didn't see in your comments so far. Could you please go through 4 conditions when nornir logging is not configured and confirm that they are all right? I think first two completely make sense, but for the last two where I decide based on the root logger config there can be some disagreement.

@dbarrosop
Copy link
Contributor

dbarrosop commented Jan 26, 2019

Could you please go through 4 conditions when nornir logging is not configured and confirm that they are all right?

Yeah, sorry, I was meant to comment on that and lost my train of thought. I don't want to add any logic here. I don't want to have to document under which circumstances the logging subsystem is configured automagically or have to answer support questions about that. So let's recover the old InitNornir flag and let the user decide instead, please. It's also a breaking change.

@dmfigol
Copy link
Collaborator Author

dmfigol commented Jan 26, 2019

So let's recover the old InitNornir flag and let the user decide instead, please. It's also a breaking change.

I can find a way to convert it to a non-breaking change

I don't want to add any logic here. I don't want to have to document under which circumstances the logging subsystem is configured automagically or have to answer support questions about that

If I remove that, what does this PR solve? One of my problems with nornir is that if you do Python logging (using basicConfig or dictConfig) and you don't disable nornir logging, the result will be a mess. It is not documented properly either. One of the things this PR solves is that if you do that, it will not result in a mess.

@dbarrosop
Copy link
Contributor

If I remove that, what does this PR solve?

My understanding is that, besides the breaking changes, you are doing some general cleaning and improvements as well. For instance, getting rid of the duplicates.

if you do Python logging (using basicConfig or dictConfig) and you don't disable nornir logging, the result will be a mess

I don't see that as a problem. You have the option today of using either way, we are not forcing anyone to use nornir's logging helper function.

It is not documented properly either

If it's not properly documented let's fix that.

One of the things this PR solves is that if you do that, it will not result in a mess

In general, instead of taking arbitrary decisions (which the user may or may not want), I'd suggest logging warning.

@bradh11
Copy link
Contributor

bradh11 commented Feb 11, 2019

Does this PR also address the duplicate log entries in the logfile that are poduced??

@dmfigol
Copy link
Collaborator Author

dmfigol commented Feb 11, 2019

@bradh11 yes

@dbarrosop
Copy link
Contributor

@dmfigol let us know when this is ready for review again. If we get this one sorted out this week I will release 2.1.

@dmfigol
Copy link
Collaborator Author

dmfigol commented Feb 26, 2019

@dmfigol @ktbyers this is ready for review

@dbarrosop
Copy link
Contributor

I will try to find some time during the weekend. Thanks for the work :)

@@ -73,34 +73,9 @@
},
Copy link
Contributor

Choose a reason for hiding this comment

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

did this break?

@dbarrosop
Copy link
Contributor

Ok, this looks awesome to me. The only comment is related to the notebook that seems to have lost the output. Can you confirm that's expected? Feels broken to me. Once we clear that out I will merge this :)

Awesome work.

Fix nornir-automation#302, nornir-automation#230

* Nornir logging is configured only when no changes have been done to Python logging
* No more duplicate logs
* Replace format and f-strings in logs to % strings according to best practices
* Improved messages for some logs
* Add .ipynb_checkpoints to sphinx ignored list
* Run black and pylama on specific folders
* Pin pydantic until 0.19.0 is released
  pydantic/pydantic#254
@dmfigol
Copy link
Collaborator Author

dmfigol commented Mar 5, 2019

@dbarrosop
I've rebased and fixed the notebook. You were right, for some reason this notebook didn't have an output and was not checked by nbval. I've stumbled upon an issue with \r\n and \n, which I didn't find the root cause for, so I had to use nbval sanitization, to make sure that \r\n is converted to \n everywhere.

@dbarrosop
Copy link
Contributor

so I had to use nbval sanitization

Good catch :)

@dbarrosop dbarrosop merged commit 344fa4e into nornir-automation:develop Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants