comparison piecrust/main.py @ 97:00a9b24ca944

Add `--log-debug` option.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 07 Sep 2014 23:49:09 -0700
parents cb1ed436642c
children 5959a117a943
comparison
equal deleted inserted replaced
96:0445a2232de7 97:00a9b24ca944
63 return 1 63 return 1
64 64
65 65
66 class PreParsedChefArgs(object): 66 class PreParsedChefArgs(object):
67 def __init__(self, root=None, cache=True, debug=False, quiet=False, 67 def __init__(self, root=None, cache=True, debug=False, quiet=False,
68 log_file=None, config_variant=None): 68 log_file=None, log_debug=False, config_variant=None):
69 self.root = root 69 self.root = root
70 self.cache = cache 70 self.cache = cache
71 self.debug = debug 71 self.debug = debug
72 self.quiet = quiet 72 self.quiet = quiet
73 self.log_file = log_file 73 self.log_file = log_file
74 self.log_debug = log_debug
74 self.config_variant = config_variant 75 self.config_variant = config_variant
75 76
76 77
77 def _pre_parse_chef_args(argv): 78 def _pre_parse_chef_args(argv):
78 # We need to parse some arguments before we can build the actual argument 79 # We need to parse some arguments before we can build the actual argument
94 res.config_variant = argv[i + 1] 95 res.config_variant = argv[i + 1]
95 ++i 96 ++i
96 elif arg == '--log': 97 elif arg == '--log':
97 res.log_file = argv[i + 1] 98 res.log_file = argv[i + 1]
98 ++i 99 ++i
100 elif arg == '--log-debug':
101 res.log_debug = True
99 elif arg == '--no-cache': 102 elif arg == '--no-cache':
100 res.cache = False 103 res.cache = False
101 elif arg == '--debug': 104 elif arg == '--debug':
102 res.debug = True 105 res.debug = True
103 elif arg == '--quiet': 106 elif arg == '--quiet':
113 raise Exception("You can't specify both --debug and --quiet.") 116 raise Exception("You can't specify both --debug and --quiet.")
114 117
115 colorama.init() 118 colorama.init()
116 root_logger = logging.getLogger() 119 root_logger = logging.getLogger()
117 root_logger.setLevel(logging.INFO) 120 root_logger.setLevel(logging.INFO)
121 if res.debug or res.log_debug:
122 root_logger.setLevel(logging.DEBUG)
123
118 log_handler = logging.StreamHandler(sys.stdout) 124 log_handler = logging.StreamHandler(sys.stdout)
119 if res.debug: 125 if res.debug:
120 root_logger.setLevel(logging.DEBUG) 126 log_handler.setLevel(logging.DEBUG)
121 log_handler.setFormatter(ColoredFormatter("[%(name)s] %(message)s")) 127 log_handler.setFormatter(ColoredFormatter("[%(name)s] %(message)s"))
122 else: 128 else:
123 if res.quiet: 129 if res.quiet:
124 root_logger.setLevel(logging.WARNING) 130 log_handler.setLevel(logging.WARNING)
131 else:
132 log_handler.setLevel(logging.INFO)
125 log_handler.setFormatter(ColoredFormatter("%(message)s")) 133 log_handler.setFormatter(ColoredFormatter("%(message)s"))
126 root_logger.addHandler(log_handler) 134 root_logger.addHandler(log_handler)
135
127 if res.log_file: 136 if res.log_file:
128 root_logger.addHandler(logging.FileHandler(res.log_file, mode='w')) 137 file_handler = logging.FileHandler(res.log_file, mode='w')
138 root_logger.addHandler(file_handler)
139 if res.log_debug:
140 file_handler.setLevel(logging.DEBUG)
129 141
130 return res 142 return res
131 143
132 144
133 def _run_chef(pre_args): 145 def _run_chef(pre_args):
160 parser.add_argument('--config', help="The configuration variant to use for this command.") 172 parser.add_argument('--config', help="The configuration variant to use for this command.")
161 parser.add_argument('--debug', help="Show debug information.", action='store_true') 173 parser.add_argument('--debug', help="Show debug information.", action='store_true')
162 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') 174 parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true')
163 parser.add_argument('--quiet', help="Print only important information.", action='store_true') 175 parser.add_argument('--quiet', help="Print only important information.", action='store_true')
164 parser.add_argument('--log', help="Send log messages to the specified file.") 176 parser.add_argument('--log', help="Send log messages to the specified file.")
177 parser.add_argument('--log-debug', help="Log debug messages to the log file.", action='store_true')
165 178
166 commands = sorted(app.plugin_loader.getCommands(), 179 commands = sorted(app.plugin_loader.getCommands(),
167 key=lambda c: c.name) 180 key=lambda c: c.name)
168 subparsers = parser.add_subparsers() 181 subparsers = parser.add_subparsers()
169 for c in commands: 182 for c in commands: