logging-best-practices by boristane/agent-skills
npx skills add https://github.com/boristane/agent-skills --skill logging-best-practices版本:1.0.0
本技能提供了在应用程序中实现有效日志记录的指南。它侧重于 宽事件(也称为规范日志行)——一种模式,即每个服务每个请求发出一个单一的、上下文丰富的事件,从而实现强大的调试和分析功能。
在以下情况下应用这些指南:
console.log、logger.info 或类似语句时每个服务每个请求发出 一个上下文丰富的事件。与其在处理器中分散记录多行日志,不如将所有内容整合成一个单一的结构化事件,在请求完成时发出。
const wideEvent: Record<string, unknown> = {
method: 'POST',
path: '/checkout',
requestId: c.get('requestId'),
timestamp: new Date().toISOString(),
};
try {
const user = await getUser(c.get('userId'));
wideEvent.user = { id: user.id, subscription: user.subscription };
const cart = await getCart(user.id);
wideEvent.cart = { total_cents: cart.total, item_count: cart.items.length };
wideEvent.status_code = 200;
wideEvent.outcome = 'success';
return c.json({ success: true });
} catch (error) {
wideEvent.status_code = 500;
wideEvent.outcome = 'error';
wideEvent.error = { message: error.message, type: error.name };
throw error;
} finally {
wideEvent.duration_ms = Date.now() - startTime;
logger.info(wideEvent);
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
包含具有高基数(用户 ID、请求 ID - 数百万个唯一值)和高维度(每个事件包含许多字段)的字段。这使得能够按特定用户进行查询,并回答您尚未预料到的问题。
始终包含业务上下文:用户订阅等级、购物车价值、功能标志、账户年龄。目标是知道“一个高级客户无法完成一笔 2,499 美元的购买”,而不仅仅是“结账失败”。
在每个事件中包含环境和部署信息:提交哈希、服务版本、区域、实例 ID。这使得能够将问题与部署关联起来,并识别特定区域的问题。
使用一个在启动时配置的记录器实例,并在各处导入它。这确保了格式的一致性和自动的环境上下文。
使用中间件来处理宽事件基础设施(计时、状态、环境、发出)。处理器应仅添加业务上下文。
info 和 errorconsole.log() 调用console.log('something happened') 而不是结构化数据rules/wide-events.md)finally 块中请求完成时发出rules/context.md)user_id、request_id)commit_hash、version、region)rules/structure.md)info 和 error 级别rules/pitfalls.md)参考文献:
每周安装量
2.1K
代码仓库
GitHub 星标数
62
首次出现
2026 年 1 月 21 日
安全审计
安装于
opencode1.5K
codex1.4K
claude-code1.4K
gemini-cli1.3K
github-copilot1.3K
cursor1.2K
Version: 1.0.0
This skill provides guidelines for implementing effective logging in applications. It focuses on wide events (also called canonical log lines) - a pattern where you emit a single, context-rich event per request per service, enabling powerful debugging and analytics.
Apply these guidelines when:
Emit one context-rich event per request per service. Instead of scattering log lines throughout your handler, consolidate everything into a single structured event emitted at request completion.
const wideEvent: Record<string, unknown> = {
method: 'POST',
path: '/checkout',
requestId: c.get('requestId'),
timestamp: new Date().toISOString(),
};
try {
const user = await getUser(c.get('userId'));
wideEvent.user = { id: user.id, subscription: user.subscription };
const cart = await getCart(user.id);
wideEvent.cart = { total_cents: cart.total, item_count: cart.items.length };
wideEvent.status_code = 200;
wideEvent.outcome = 'success';
return c.json({ success: true });
} catch (error) {
wideEvent.status_code = 500;
wideEvent.outcome = 'error';
wideEvent.error = { message: error.message, type: error.name };
throw error;
} finally {
wideEvent.duration_ms = Date.now() - startTime;
logger.info(wideEvent);
}
Include fields with high cardinality (user IDs, request IDs - millions of unique values) and high dimensionality (many fields per event). This enables querying by specific users and answering questions you haven't anticipated yet.
Always include business context: user subscription tier, cart value, feature flags, account age. The goal is to know "a premium customer couldn't complete a $2,499 purchase" not just "checkout failed."
Include environment and deployment info in every event: commit hash, service version, region, instance ID. This enables correlating issues with deployments and identifying region-specific problems.
Use one logger instance configured at startup and import it everywhere. This ensures consistent formatting and automatic environment context.
Use middleware to handle wide event infrastructure (timing, status, environment, emission). Handlers should only add business context.
info and errorconsole.log('something happened') instead of structured datarules/wide-events.md)rules/context.md)rules/structure.md)rules/pitfalls.md)References:
Weekly Installs
2.1K
Repository
GitHub Stars
62
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubFailSocketFailSnykPass
Installed on
opencode1.5K
codex1.4K
claude-code1.4K
gemini-cli1.3K
github-copilot1.3K
cursor1.2K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装