Set WARNING as default log level + update documentation

This commit is contained in:
Yann Sionneau 2015-01-30 12:02:27 +01:00 committed by Sebastien Bourdeauducq
parent 37feaf1cea
commit 526887140d
3 changed files with 11 additions and 10 deletions

View File

@ -33,7 +33,7 @@ def get_argparser():
parser.add_argument("-t", "--times", parser.add_argument("-t", "--times",
default="np.arange(5)*1e-6", default="np.arange(5)*1e-6",
help="sample times (s) [%(default)s]") help="sample times (s) [%(default)s]")
parser.add_argument("-v", "--voltages", parser.add_argument("-u", "--voltages",
default="(1-np.cos(t/t[-1]*2*np.pi))/2", default="(1-np.cos(t/t[-1]*2*np.pi))/2",
help="sample voltages (V) [%(default)s]") help="sample voltages (V) [%(default)s]")
parser.add_argument("-o", "--order", default=3, type=int, parser.add_argument("-o", "--order", default=3, type=int,

View File

@ -37,4 +37,4 @@ def verbosity_args(parser):
def init_logger(args): def init_logger(args):
logging.basicConfig(level=logging.INFO + args.quiet*10 - args.verbose*10) logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10)

View File

@ -115,32 +115,33 @@ Logging and error handling in controllers
Unrecoverable errors (such as the hardware being unplugged) should cause timely termination of the controller, in order to notify the controller manager which may try to restart the controller later according to its policy. Throwing an exception and letting it propagate is the preferred way of reporting an unrecoverable error. Unrecoverable errors (such as the hardware being unplugged) should cause timely termination of the controller, in order to notify the controller manager which may try to restart the controller later according to its policy. Throwing an exception and letting it propagate is the preferred way of reporting an unrecoverable error.
For the debug, information and warning messages, use the ``logging`` Python module and print the log on the standard error output (the default setting). The logging level should be configurable with a command line option called ``--log`` that takes a string (debug, info, ...) representing the logging level. For the debug, information and warning messages, use the ``logging`` Python module and print the log on the standard error output (the default setting). The logging level is by default "WARNING", meaning that only warning messages and more critical messages will get printed (and no debug nor information messages). By calling the ``verbosity_args()`` with the parser as argument, you add support for the ``--verbose`` (``-v``) and ``--quiet`` (``-q``) arguments in the parser. Each occurence of ``-v`` (resp. ``-q``) in the arguments will increase (resp. decrease) the log level of the logging module. For instance, if only one ``-v`` is present in the arguments, then more messages (info, warning and above) will get printed. If only one ``-q`` is present in the arguments, then only errors and critical messages will get printed. If ``-qq`` is present in the arguments, then only critical messages will get printed, but no debug/info/warning/error.
The program below exemplifies how to use logging: :: The program below exemplifies how to use logging: ::
import argparse import argparse
import logging import logging
from artiq.tools import verbosity_args, init_logger
def get_argparser(): def get_argparser():
parser = argparse.ArgumentParser(description="Logging example") parser = argparse.ArgumentParser(description="Logging example")
parser.add_argument("--log", type=str, default="WARNING", parser.add_argument("--someargument",
help="set log level") help="some argument")
# [...]
verbosity_args(parser) # This adds the -q and -v handling
return parser return parser
def main(): def main():
args = get_argparser().parse_args() args = get_argparser().parse_args()
init_logger(args) # This initializes logging system log level according to -v/-q args
numeric_level = getattr(logging, args.log.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: " + args.log)
logging.basicConfig(level=numeric_level)
logging.debug("this is a debug message") logging.debug("this is a debug message")
logging.info("this is an info message") logging.info("this is an info message")
logging.warning("this is a warning message") logging.warning("this is a warning message")
logging.error("this is an error message")
logging.critical("this is a critical message")
if __name__ == "__main__": if __name__ == "__main__":
main() main()