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

Feature/log lvl arg parsed #51

Closed
wants to merge 5 commits into from
Closed

Feature/log lvl arg parsed #51

wants to merge 5 commits into from

Conversation

vdg0
Copy link
Contributor

@vdg0 vdg0 commented Jan 7, 2020

Well, i tested manually.
I put log = helpers.getLogger() before of setting it for the chance to raise Exception of bad argument on --log or -l.

I can write my firsts tests if i get some direction in how to test this...

log = helpers.prepareLogger("APP", os.path.join(logDir, "tinydecred.log"), logLvl=0)
log = helpers.getLogger("APP")
Copy link
Member

Choose a reason for hiding this comment

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

Not quite sure what you're trying to accomplish here. These functions do very different things. prepareLogger must be called before getLogger, and prepareLogger should only be called once for the application. Look at helpers.py to see how those functions work.

Comment on lines +579 to +583
for logLvlModule in cfg.logLvls:
try:
parts = logLvlModule.split(":")
module = parts[0].upper()
logLvl = helpers.getLogLevel(parts[1])
Copy link
Member

Choose a reason for hiding this comment

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

This parsing should be done during configuration parsing (config.go). Make it a function so you can test it. You also don't appear to be handling the default case, --log=DEBUG, where no module is specified. That log level should be passed to helpers.prepareLogger to set the root logging level.

Read up on the logging module and how child loggers work. I would move all logger configuration to config.py, including the call to perpareLogger, and then just retrieve the loggers from the individual modules, without resetting the level. This will require modification of the getLogger function so that setLevel is not run during retrieval.

l.setLevel(logLvl)

module = parts[0].upper()
logLvl = helpers.getLogLevel(parts[1])

helpers.prepareLogger(
Copy link
Member

Choose a reason for hiding this comment

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

See previous comment. This is the wrong way to use this function.

"-l",
action="append",
type=str,
help="set logging level [module:lvl]",
Copy link
Member

Choose a reason for hiding this comment

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

This help string isn't quite clear. Mention multiple instances and maybe give an actual example, like --log=DEBUG --log=DCRDATA:TRACE.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is correct.
TRACE is not an valid option(:P) but yes, examples are totally right.

"--log",
"-l",
action="append",
type=str,
Copy link
Member

Choose a reason for hiding this comment

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

You can drop the type conversion, I think. Everything is a string by default.

args = parser.parse_args()
self.logLvls = args.log
Copy link
Member

Choose a reason for hiding this comment

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

Here's where you should parse the arguments, including looking for a default argument. Then prepare the root logger and any specified loggers.


Return: logging.LogLvl
"""
logNameToLevel = {
Copy link
Member

Choose a reason for hiding this comment

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

This dict is read-only. Create it once globally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, now is before of first function definition.

}

try:
lvl = int(logLvl)
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about adding an assert logging.NOTSET <= lvl <= logging.CRITICAL here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds right

Copy link
Member

@buck54321 buck54321 Jan 12, 2020

Choose a reason for hiding this comment

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

My suggestion was bad, as per #47 (comment). Instead, do

if lvl > logging.CRITICAL or lvl < logging.NOTSET: 
    raise AssertionError("...")

Comment on lines +250 to +253
if name in logNameToLevel.keys():
return logNameToLevel[name]
else:
return logging.NOTSET
Copy link
Member

@buck54321 buck54321 Jan 8, 2020

Choose a reason for hiding this comment

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

return logNameToLevel[name] if name in logNameToLevel else logging.NOTSET

or maybe a better way would be

if name not in logNameToLevel:
    raise AssertionError("unknown module specified: " + name)
return logNameToLevel[name]

*edited

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is good 👍

@buck54321
Copy link
Member

@vdg0 still working on this?

@vdg0
Copy link
Contributor Author

vdg0 commented Jan 15, 2020

@vdg0 still working on this?

@buck54321 Yes, sorry the delay, i'm having complications with payed jobs and it's requesting more time than normal, but yes, i'm progressing with this.

@buck54321
Copy link
Member

This is going to be a messy rebase, and there's been no progress here anyway. Closing. @vdg0: you can reopen if you get this sorted out.

@buck54321 buck54321 closed this Jan 24, 2020
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.

2 participants