feat: integrate file logger into launcher and update log scripts

Se integra shell/logger en cmd/launcher para que cada agente escriba
sus logs en logs/<agent-id>/YYYY-MM-DD.jsonl. Nuevo flag --log-dir
(default: "logs", "stdout" para consola). El launcher y cada agente
tienen su propio logger con cleanup automático al apagar. Se actualiza
dev-scripts/logs.sh para leer desde la nueva estructura de directorios.
Se corrige .gitignore: "launcher" → "/launcher" para no ignorar cmd/launcher/,
y se añade logs/.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 17:27:24 +00:00
parent 71079962ca
commit 750e896c08
3 changed files with 95 additions and 20 deletions
+3 -1
View File
@@ -3,10 +3,12 @@
*.log
data/
bin/
launcher
/launcher
run/*.pid
run/*.log
logs/
/agentctl
/dashboard
/verify
+48 -9
View File
@@ -27,6 +27,7 @@ import (
"github.com/enmanuel/agents/pkg/decision"
"github.com/enmanuel/agents/pkg/orchestration"
"github.com/enmanuel/agents/shell/bus"
agentlog "github.com/enmanuel/agents/shell/logger"
orchshell "github.com/enmanuel/agents/shell/orchestration"
)
@@ -41,6 +42,7 @@ func main() {
var (
configPaths []string
logLevel string
logDir string
)
root := &cobra.Command{
@@ -54,7 +56,27 @@ func main() {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
logger := newLogger(logLevel)
lvl := parseLogLevel(logLevel)
// ── Launcher-level logger ──
logger, launcherCleanup, err := agentlog.NewAgentLogger(agentlog.LoggerConfig{
BaseDir: logDir,
AgentID: "launcher",
Level: lvl,
})
if err != nil {
// Fallback to stdout if file logger fails.
logger = newLogger(logLevel)
logger.Warn("could not create file logger, falling back to stdout", "err", err)
launcherCleanup = func() {}
}
var cleanups []func()
cleanups = append(cleanups, launcherCleanup)
defer func() {
for _, fn := range cleanups {
fn()
}
}()
if len(configPaths) == 0 {
logger.Warn("no agent configs found — nothing to start")
@@ -94,7 +116,19 @@ func main() {
}
rules := rulesFor(cfg.Agent.ID, logger)
agentLogger := logger.With("agent", cfg.Agent.ID)
// Per-agent logger → writes to logs/<agent-id>/YYYY-MM-DD.jsonl
agentLogger, agentCleanup, aErr := agentlog.NewAgentLogger(agentlog.LoggerConfig{
BaseDir: logDir,
AgentID: cfg.Agent.ID,
Level: lvl,
})
if aErr != nil {
logger.Warn("agent file logger failed, using launcher logger", "agent", cfg.Agent.ID, "err", aErr)
agentLogger = logger.With("agent", cfg.Agent.ID)
agentCleanup = func() {}
}
cleanups = append(cleanups, agentCleanup)
a, err := agents.New(cfg, rules, agentLogger)
if err != nil {
@@ -151,6 +185,8 @@ func main() {
"Agent config file(s). If omitted, discovers all agents/*/config.yaml")
root.Flags().StringVar(&logLevel, "log-level", "info",
"Log level: debug | info | warn | error")
root.Flags().StringVar(&logDir, "log-dir", "logs",
`Log directory (logs/<agent>/YYYY-MM-DD.jsonl). Use "stdout" for console only`)
if err := root.Execute(); err != nil {
os.Exit(1)
@@ -197,17 +233,20 @@ func rulesFor(agentID string, logger *slog.Logger) []decision.Rule {
return factory()
}
func newLogger(level string) *slog.Logger {
var lvl slog.Level
func parseLogLevel(level string) slog.Level {
switch level {
case "debug":
lvl = slog.LevelDebug
return slog.LevelDebug
case "warn":
lvl = slog.LevelWarn
return slog.LevelWarn
case "error":
lvl = slog.LevelError
return slog.LevelError
default:
lvl = slog.LevelInfo
return slog.LevelInfo
}
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: lvl}))
}
// newLogger creates a stdout-only JSON logger (fallback when file logger fails).
func newLogger(level string) *slog.Logger {
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: parseLogLevel(level)}))
}
+44 -10
View File
@@ -1,21 +1,55 @@
#!/usr/bin/env bash
# logs.sh — sigue los logs del launcher unificado
# logs.sh — sigue los logs de agentes (logs/<agent>/YYYY-MM-DD.jsonl)
#
# Uso:
# ./dev-scripts/logs.sh # tail -f del launcher log
# ./dev-scripts/logs.sh 100 # últimas 100 líneas
# ./dev-scripts/logs.sh # tail -f de todos los agentes (hoy)
# ./dev-scripts/logs.sh assistant-bot # tail -f de un agente específico
# ./dev-scripts/logs.sh assistant-bot 100 # últimas 100 líneas
source "$(dirname "$0")/_common.sh"
LINES="${1:-50}"
LOG="$(launcher_log_file)"
LOG_DIR="logs"
AGENT_ID="${1:-}"
LINES="${2:-50}"
if [[ ! -f "$LOG" ]]; then
if [[ ! -d "$LOG_DIR" ]]; then
fail "No hay logs todavía — inicia el launcher primero"
fi
info "Siguiendo logs: $LOG"
dim " Ctrl+C para salir"
echo ""
TODAY="$(date -u +%Y-%m-%d)"
tail -n "$LINES" -f "$LOG"
if [[ -n "$AGENT_ID" ]]; then
# Logs de un agente específico
LOG_FILE="$LOG_DIR/$AGENT_ID/$TODAY.jsonl"
if [[ ! -f "$LOG_FILE" ]]; then
# Try to find the latest log file for this agent
LATEST="$(ls -t "$LOG_DIR/$AGENT_ID/"*.jsonl 2>/dev/null | head -1)"
if [[ -z "$LATEST" ]]; then
fail "No hay logs para $AGENT_ID"
fi
LOG_FILE="$LATEST"
fi
info "Siguiendo logs: $LOG_FILE"
dim " Ctrl+C para salir"
echo ""
tail -n "$LINES" -f "$LOG_FILE"
else
# Logs de todos los agentes (archivos de hoy)
FILES=$(find "$LOG_DIR" -name "$TODAY.jsonl" 2>/dev/null)
if [[ -z "$FILES" ]]; then
# Fallback: latest file from each agent
FILES=""
for d in "$LOG_DIR"/*/; do
LATEST="$(ls -t "$d"*.jsonl 2>/dev/null | head -1)"
[[ -n "$LATEST" ]] && FILES="$FILES $LATEST"
done
fi
if [[ -z "$FILES" ]]; then
fail "No hay logs todavía — inicia el launcher primero"
fi
info "Siguiendo logs de todos los agentes (hoy: $TODAY)"
dim " Ctrl+C para salir"
echo ""
# shellcheck disable=SC2086
tail -n "$LINES" -f $FILES
fi