日志诊断的提示工程 — Gemini 实际有效的方法
Source: Dev.to

所有测试均在一台 8 年旧的 MacBook Air 上运行。
“Analyze this log” 会产生一个通用答案。
“You are an Android specialist. Identify the root cause and the specific fix.” 则会给出有用的信息。
提示设计的重要性往往被低估。下面是我为 HiyokoLogcat 的诊断功能所做的迭代过程。
Version 1: Too vague
Analyze this Android logcat output and tell me what's wrong.
[logs]
结果:对 logcat 是什么的长篇解释,关于 NullPointerException 的通用建议,没有指出具体行。
Version 2: Role + constraint
You are an Android development specialist.
Analyze the following logcat output.
Identify the root cause and suggest a fix.
Be concise — 3‑5 sentences maximum.
[logs]
效果更好。输出简洁,但在出现多个异常时仍会有时聚焦在错误的异常上。
Version 3: Focus on the target line
let prompt = format!(
"You are an Android development specialist.\n\
The following logcat output contains an error.\n\
The KEY ERROR LINE is marked with >>.\n\
Identify the root cause of this specific error and suggest a fix.\n\
Be concise and specific. Point to the relevant line numbers if possible.\n\n\
Logcat:\n{}",
context_with_marker
);
在上下文中标记目标行:
pub fn build_context_with_marker(
lines: &[LogLine],
target_idx: usize,
) -> String {
lines
.iter()
.enumerate()
.map(|(i, line)| {
if i == target_idx {
format!(">> {}", line.raw)
} else {
line.raw.clone()
}
})
.collect::>()
.join("\n")
}
>> 标记让 Gemini 明确知道要关注哪一条错误。质量显著提升。
Version 4: Language‑aware (current)
该应用支持日语和英语。系统提示会根据 UI 语言切换:
let system = match lang {
Lang::Japanese =>
"あなたはAndroid開発のスペシャリストです。\
以下のlogcatからエラー原因と解決策を日本語で簡潔に答えてください。\
KEY ERROR LINEが対象のエラーです。",
Lang::English =>
"You are an Android development specialist. \
Identify the root cause of the KEY ERROR LINE \
and suggest a fix. Be concise and specific.",
};
日语提示 → 日语回复。无需后处理。
What I learned
最关键的三点:
- 角色分配 – “You are a specialist” 确实能提升输出质量。
- 目标标记 – 明确告诉模型要关注的行。
- 长度约束 – “3‑5 sentences” 能防止出现长篇大论。
其他的都是噪音。
HiyokoLogcat 是免费且开源的 →
关注我的 X → @hiyoyok