I have set Fabric up to log all SSH/Paramiko-related output with a level of INFO or higher using:
logging.basicConfig()
logging.getLogger('ssh.transport').setLevel(logging.INFO)
This results in a log looking like this:
[host1] Executing task 'task1'
[host1] Run: ls
...
Is it possible to change the formatter for the ssh.transport logger so that each line also has the date and time printed next to it?
As alecxe said, the format is hardcoded in Fabric 1.x (as of when I'm posting this, the only version available.) There was a rejected pull request that could have remedied this.
So instead we need a work around. This is a rather hacky solution that I wrote, but it relies on undocumented portions of Fabric, which means it may break in future versions.
from fabric.io import OutputLooper
from datetime import datetime
def newFlush(self, text):
stamp = datetime.now().strftime("%a %b %d %H:%M:%S - ")
print(stamp + text)
OutputLooper._flush = newFlush
From this point forward, any output from your remote machine will have timestamps.
For example, without this code the output from sudo('echo "test"')
would be:
[InteractSL-DT1.usma.ibm.com] sudo: echo "test"
[InteractSL-DT1.usma.ibm.com] out: test
[InteractSL-DT1.usma.ibm.com] out:
'test'
But after adding that, you'll now get this:
[InteractSL-DT1.usma.ibm.com] sudo: echo "test"
Fri Jan 02 12:54:49 - [InteractSL-DT1.usma.ibm.com] out:
Fri Jan 02 12:54:49 - test
Fri Jan 02 12:54:49 - [InteractSL-DT1.usma.ibm.com] out:
Fri Jan 02 12:54:49 -
'test'
You can play around with this basic idea to clean it up. The sudo
line at the start of the output comes from fabric.operations._run_command
, around line 900. I'm not sure of any simple way you could modify it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With