如何让gcloud app日志读取尾部模式从最近的日志开始?

时间:2016-12-07 14:40:02

标签: google-app-engine logging gcloud

我们正在使用GAE,我们希望现场观看日志(几秒延迟是可以接受的)。 gcloud app logs read命令只返回最后一个日志行。

我们编写了一个小脚本,每隔3秒调用gcloud app logs read并仅打印新行。

是否可以在某种“尾部”模式下运行命令,以便它们保持实时连接并在它们到达时立即打印新行?

由于

----编辑----

@gregology,这是我的python脚本:

#!/usr/bin/env python
import subprocess
from time import sleep

def run(extra_args = None, min_cache_size = 50, max_cache_size = 2000):
    cached_lines = None
    cached_order = None
    cache_size = min_cache_size

    if extra_args is None:
        extra_args = []
    while True:
        while True:
            sleep(0.1)
            lines = subprocess.check_output(["gcloud", "app", "logs", "read", "--limit", str(cache_size)] + list(extra_args)).splitlines()
            if cached_lines is None:
                cached_lines = set(lines)
                cached_order = list(lines)
                continue
            if all(line not in cached_lines for line in lines) and (cache_size * 2) < max_cache_size:
                # None of the lines is in the cache - retry with a larger cache_size
                cache_size *= 2
                print "... google-logs-tail increasing cache size to %d lines" % (cache_size,)
                continue
            break

        new_lines = [line for line in lines if line not in cached_lines]

        if len(new_lines) == len(lines):
            print "... google-logs-tail log is broken - some lines may be missing ..."
            cache_size = min_cache_size

        for line in new_lines:
            print line
            cached_order.append(line)
            cached_lines.add(line)

        if (len(new_lines) < cache_size / 4) and (cache_size >= min_cache_size * 2):
            cache_size /= 2
            print "... google-logs-tail decreasing cache size to %d lines" % (cache_size,)

        if len(cached_order) > max_cache_size * 8:
            # much more lines than we need
            # keep only max_cache_size * 4 lines
            drop_lines = cached_order[:len(cached_order) - (max_cache_size * 4)]
            for line in drop_lines:
                cached_lines.discard(line)
            del cached_order[:len(drop_lines)]
        sleep(3)

if __name__ == '__main__':
    import sys
    run(sys.argv[1:])

1 个答案:

答案 0 :(得分:0)

我认为,

gcloud app logs tail -s default做你想做的事。 gcloud app deploy表明了这一点。