You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GCP Logs Explorer中设置日志时间戳为12小时制?

在GCP Logs Explorer中切换时间戳为12小时制

目前GCP Logs Explorer的原生UI设置里,确实没有直接切换12小时制的选项,但可以通过以下两种方法实现需求:

方法一:用LogQL自定义输出格式

通过日志查询语言(LogQL)格式化时间字段,直接在查询结果中展示12小时制时间:

  • 打开Logs Explorer,在查询框中输入类似以下的语句:
    fetch *
    | format timestamp("%I:%M:%S %p", timestamp) as formatted_time
    | display formatted_time, jsonPayload, severity
    
    其中%I对应12小时制小时(01-12),%M是分钟,%S是秒,%p会显示AM/PM标识。如果需要包含日期,可以调整格式为"%Y-%m-%d %I:%M:%S %p"
  • 执行查询后,结果列表里就会出现你自定义的12小时制时间列。

方法二:用浏览器脚本强制转换原生时间显示(以Chrome为例)

如果希望让Logs Explorer默认的时间列直接显示12小时制,可以用Tampermonkey脚本实现:

  • 安装Tampermonkey浏览器扩展
  • 创建新脚本,粘贴以下代码:
    // ==UserScript==
    // @name         GCP Logs Explorer 12小时制时间转换
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  把GCP日志时间戳转成12小时格式
    // @match        https://console.cloud.google.com/logs/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
        function convertTime(timestampStr) {
            const date = new Date(timestampStr);
            return date.toLocaleTimeString('en-US', {hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true});
        }
        // 监听页面加载和动态更新,自动转换时间
        const observer = new MutationObserver((mutations) => {
            mutations.forEach(mutation => {
                if (mutation.addedNodes.length) {
                    document.querySelectorAll('.log-viewer-timestamp-column span').forEach(el => {
                        el.textContent = convertTime(el.textContent.trim());
                    });
                }
            });
        });
        observer.observe(document.body, {childList: true, subtree: true});
    })();
    
  • 保存脚本后刷新Logs Explorer页面,原生时间列就会自动变成12小时制显示。

内容的提问来源于stack exchange,提问作者Joy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:42:03