发布于 2015-10-05 04:12:11 | 591 次阅读 | 评论: 0 | 来源: 网络整理
The fab
tool is very verbose by default and prints out almost everything it can, including the remote end’s stderr and stdout streams, the command strings being executed, and so forth. While this is necessary in many cases in order to know just what’s going on, any nontrivial Fabric task will quickly become difficult to follow as it runs.
To aid in organizing task output, Fabric output is grouped into a number of non-overlapping levels or groups, each of which may be turned on or off independently. This provides flexible control over what is displayed to the user.
注解
All levels, save for debug
and exceptions
, are on by default.
The standard, atomic output levels/groups are as follows:
grep
to test existence of text in a file. If paired with setting env.warn_only
to True, this can result in fully silent warnings when remote programs fail. As with aborts
, this setting does not control actual warning behavior, only whether warning messages are printed or hidden.[myserver] run: ls /var/www
. Also controls printing of tasks being run, e.g. [myserver] Executing task 'foo'
.fastprint
or puts
functions.在 0.9.2 版更改: Added “Executing task” lines to the running
output level.
在 0.9.2 版更改: Added the user
output level.
There are two more atomic output levels for use when troubleshooting: debug
, which behaves slightly differently from the rest, and exceptions
, whose behavior is included in debug
but may be enabled separately.
debug: Turn on debugging (which is off by default.) Currently, this is largely used to view the “full” commands being run; take for example this run
call:
run('ls "/home/username/Folder Name With Spaces/"')
Normally, the running
line will show exactly what is passed into run
, like so:
[hostname] run: ls "/home/username/Folder Name With Spaces/"
With debug
on, and assuming you’ve left shell set to True
, you will see the literal, full string as passed to the remote server:
[hostname] run: /bin/bash -l -c "ls \"/home/username/Folder Name With Spaces\""
Enabling debug
output will also display full Python tracebacks during aborts (as if exceptions
output was enabled).
注解
Where modifying other pieces of output (such as in the above example where it modifies the ‘running’ line to show the shell and any escape characters), this setting takes precedence over the others; so if running
is False but debug
is True, you will still be shown the ‘running’ line in its debugging form.
exceptions: Enables display of tracebacks when exceptions occur; intended for use when debug
is set to False
but one is still interested in detailed error info.
在 1.0 版更改: Debug output now includes full Python tracebacks during aborts.
在 1.11 版更改: Added the exceptions
output level.
In addition to the atomic/standalone levels above, Fabric also provides a couple of convenience aliases which map to multiple other levels. These may be referenced anywhere the other levels are referenced, and will effectively toggle all of the levels they are mapped to.
stdout
and stderr
. Useful for when you only care to see the ‘running’ lines and your own print statements (and warnings).warnings
, running
, user
and output
(see above.) Thus, when turning off everything
, you will only see a bare minimum of output (just status
and debug
if it’s on), along with your own print statements.stdout
and running
. Good for hiding non-erroring commands entirely, while still displaying any stderr output.在 1.4 版更改: Added the commands
output alias.
You may toggle any of Fabric’s output levels in a number of ways; for examples, please see the API docs linked in each bullet point:
Direct modification of fabric.state.output: fabric.state.output
is a dictionary subclass (similar to env) whose keys are the output level names, and whose values are either True (show that particular type of output) or False (hide it.)
fabric.state.output
is the lowest-level implementation of output levels and is what Fabric’s internals reference when deciding whether or not to print their output.
Context managers: hide
and show
are twin context managers that take one or more output level names as strings, and either hide or show them within the wrapped block. As with Fabric’s other context managers, the prior values are restored when the block exits.
Command-line arguments: You may use the --hide
and/or --show
arguments to fab 选项和参数, which behave exactly like the context managers of the same names (but are, naturally, globally applied) and take comma-separated strings as input.