question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Changing variable from command line

See original GitHub issue

1. Briefly

I don’t understand, how I can change one variable from command line.

2. Settings

1. Environment

  • Windows 10 64-bit Enterprise EN,
  • Python 3.6.3,
  • Clize 4.0.2.

2. File

For example, I have a file, where I use Logbook logging.

import logbook
import sys

logbook.StreamHandler(sys.stdout,
                        level=logbook.WARNING).push_application()
log = logbook.Logger("Sasha Logbook")

log.debug("Debug message")
log.warning("Warning message")
log.error("Error message")

If I run this file in console, I get output:

D:\SashaPythonista>python SashaLogbook.py
[2018-01-01 17:55:47.063625] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:55:47.064624] ERROR: Sasha Logbook: Error message

Output, if logbook.StreamHandler(sys.stdout, level=logbook.DEBUG).push_application():

[2018-01-01 17:55:46.994054] DEBUG: Sasha Logbook: Debug message
[2018-01-01 17:57:26.982679] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:57:26.983677] ERROR: Sasha Logbook: Error message

And so on.

2. Expected behavior

If:

D:\SashaPythonista>python SashaLogbook.py level=DEBUG
[2018-01-01 17:55:46.994054] DEBUG: Sasha Logbook: Debug message
[2018-01-01 17:55:47.063625] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:55:47.064624] ERROR: Sasha Logbook: Error message

If:

D:\SashaPythonista>python SashaLogbook.py level=WARNING
[2018-01-01 17:57:26.982679] WARNING: Sasha Logbook: Warning message
[2018-01-01 17:57:26.983677] ERROR: Sasha Logbook: Error message

And so on.

3. Actual behavior

I try to add Clize:

import logbook
import sys

from clize import run

log = logbook.Logger("Sasha Logbook")


def test(level="WARNING"):

    if level == "DEBUG":
        logbook.StreamHandler(sys.stdout,
                              level=logbook.DEBUG).push_application()
    if level == "WARNING":
        logbook.StreamHandler(sys.stdout,
                              level=logbook.WARNING).push_application()
    if level == "ERROR":
        logbook.StreamHandler(sys.stdout,
                              level=logbook.ERROR).push_application()


run(test)

log.debug("Debug message")
log.warning("Warning message")
log.error("Error message")

If I replace run(test) to test(), Logbook messages print to console. But if I use run(test), I haven’t output in console.

D:\SashaPythonista>python SashaLogbook.py --help
Usage: SashaLogbook.py [level]

Arguments:
  level         (default: WARNING)

Other actions:
  -h, --help   Show the help

D:\SashaPythonista>python SashaLogbook.py level=DEBUG

D:\SashaPythonista>python SashaLogbook.py level=ERROR

4. Did not help

  1. I see example for logging, but it hard for me. I want only control logging levels from command line.

5. Do not offer

  1. I want to use Logbook, not default Python logging module.

Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Kristinitacommented, Jan 6, 2018

@epsy , thank you!

Working example:

import logbook
import sys

log = logbook.Logger("Sasha Logbook")


def clize_log_level(*, logbook_level: 'll'="WARNING"):
    """Change log levels via command line.

    User select, which logging messages to see. See about 6 log levels here:
    https://logbook.readthedocs.io/en/stable/quickstart.html

    :param logbook_level: user select logging level
    """

     if logbook_level == "DEBUG":
         logbook.StreamHandler(sys.stdout,
                               level=logbook.DEBUG).push_application()
     if logbook_level == "WARNING":
         logbook.StreamHandler(sys.stdout,
                               level=logbook.WARNING).push_application()
     if logbook_level == "ERROR":
            logbook.StreamHandler(sys.stdout,
                                  level=logbook.ERROR).push_application()

run(clize_log_level, exit=False)

log.debug("Debug message")
log.warning("Warning message")
log.error("Error message")

1reaction
epsycommented, Jan 2, 2018

Ah, I just spotted another problem:

In your 3. example, you use run(test) and then print the log messages. By default, run() exits the program with an appropriate exit code. You have to either move the log.X("...") calls into def test, or you can tell Clize not to exit: run(test, exit=False)

Read more comments on GitHub >

github_iconTop Results From Across the Web

loop a command with a changing variable in Windows ...
bat file I'm creating (sorts out movies into another alphabetized folder) so that it will have far fewer lines. I want to get...
Read more >
How to replace bash variable with command line arguments
1. Replace that with abc=abc ./bash.sh to set abc as an environment variable instead of a positional argument (which nothing in your script ......
Read more >
Changing Variable at the command line of a bash script
This would set the value of the variable value to whatever the first command line argument was, unless it was not provided, in...
Read more >
How to change the value of a variable in a section of a file ...
I am looking for help with an example usage of the 'sed' command. i want to change value of variable in section in...
Read more >
How do I show a changing variable value in command ...
My concern is, I want to display the result ONLY IN ONE ROW, that is, "Iteration =" does not show again, ONLY the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found