도 역시 _flush()
을 대체 할 방법을 찾지 못했습니다. 나는 일반적으로 제 3자를 제외하고 어떤 수업에서든 개인적인 방법을 대체하는 것은 위험한 습관이라고 생각합니다. 다음은 Fabric _flush()
메서드를 꾸미고 원시 Python time.asctime
메서드를 사용하여 타임 스탬프의 형식을 지정하는 솔루션입니다.
> fab uptime -H 10.0.1.3,10.0.1.2
[10.0.1.3] Executing task 'uptime'
[10.0.1.3] run: uptime
[Thu Dec 15 19:34:35 2016] [10.0.1.3] out: 19:34:35 up 69 days, 4:22, 1 user, load average: 0.05, 0.03, 0.05
[Thu Dec 15 19:34:35 2016] [10.0.1.3] out:
[10.0.1.2] Executing task 'uptime'
[10.0.1.2] run: uptime
[Thu Dec 15 19:34:35 2016] [10.0.1.2] out: 19:34:35 up 70 days, 1:12, 1 user, load average: 0.00, 0.01, 0.05
[Thu Dec 15 19:34:35 2016] [10.0.1.2] out:
Done.
Disconnecting from [email protected] done.
Disconnecting from [email protected] done.
:
def time_decorator(msg):
"""
Decorates `msg` with current timestamp
Args:
msg(str): The log message from fabric
Returns:
str: Original message prepended with current date time
"""
if "\n" not in msg and msg.strip():
return "[%s] %s" % (time.asctime(), msg)
return msg
# Compose original method inside of decorator
_original_flush = OutputLooper._flush
OutputLooper._flush = lambda self, msg: {
_original_flush(self, time_decorator(msg))
}
@task
def uptime():
run('uptime')
테스트 그것을, 당신의 출력은 같은 유사합니다
출처
2016-12-15 19:44:42
Ben