360 Secure SkillHub
360 安全技能中心

log-timestamp-annotator

解析日志和 JSON 中的 unix 秒、毫秒、RFC3339、MySQL 日期时间等混合时间戳,按行独立识别格式并写入 ts_utc 的 UTC ISO-8601 字段,不覆盖原字段;无识别时间戳的行保持不变,支持约 10 万行并在大文件时分块处理。假设时间戳为 UTC 或带显式时区,不对无时区本地时间进行猜测。

laiguihao
laiguihao
热度 11.03
最近更新 2026-08-21 20:00:01
分类:效率依赖:代码执行 / 读写文件应用场景:数据处理 / 自动化脚本 / 工作流

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add log-timestamp-annotator
Metadata
name
log-timestamp-annotator
description
Annotate and reformat timestamp fields inside log lines and JSON payloads
version
1.0.0

log-timestamp-annotator

Parses mixed-format log streams and normalizes every timestamp (unix seconds, unix millis, RFC3339, and MySQL datetime) into a single canonical UTC ISO-8601 field, preserving the original line structure so downstream dashboards and alert rules keep working.

When to use

Use when a service emits timestamps in more than one format and a consumer (Grafana, Elasticsearch pipeline, or a hand-written script) needs one consistent field to sort, group, or bucket by. Also useful before loading old log dumps into a schema that expects a fixed datetime type.

Behavior

  • Detects the format of each timestamp independently per line.
  • Writes the normalized value into a new ts_utc field; never overwrites the original timestamp column.
  • Leaves lines without a recognizable timestamp untouched.
  • Handles up to 100k lines per invocation; larger inputs are processed in chunks with a progress counter on stderr.

Example

$ log-annotate --input app.log --output app.norm.log
  line 12:   1630000000            -> ts_utc=2021-08-26T21:46:40Z
  line 13:   1630000000123         -> ts_utc=2021-08-26T21:46:40.123Z
  line 14:   2021-08-26 21:46:40   -> ts_utc=2021-08-26T21:46:40Z

Exit codes

0 on success, 1 if any line failed to parse, 2 on invalid flags.

Limitations

Assumes timestamps are in UTC or carry an explicit offset. Naive local timestamps are reported to stderr instead of being guessed.