Documentation
¶
Overview ¶
Package logging provides structured logging for the Entire CLI using slog.
Usage:
// Initialize logger for a session (typically at session start)
if err := logging.Init(sessionID); err != nil {
// handle error
}
defer logging.Close()
// Add context values
ctx = logging.WithComponent(ctx, "hooks")
ctx = logging.WithAgent(ctx, agentName)
// Log with context - component/agent extracted automatically
logging.Info(ctx, "hook invoked",
slog.String("hook", hookName),
slog.String("branch", branch),
)
Index ¶
- Constants
- func Close()
- func Debug(ctx context.Context, msg string, attrs ...any)
- func EnsureInitialized(ctx context.Context) func()
- func Error(ctx context.Context, msg string, attrs ...any)
- func Info(ctx context.Context, msg string, attrs ...any)
- func Init(ctx context.Context, sessionID string) error
- func SetLogLevelGetter(getter func() string)
- func Warn(ctx context.Context, msg string, attrs ...any)
- func WithAgent(ctx context.Context, agentName types.AgentName) context.Context
- func WithComponent(ctx context.Context, component string) context.Context
Constants ¶
const LogLevelEnvVar = "ENTIRE_LOG_LEVEL"
LogLevelEnvVar is the environment variable that controls log level.
const LogsDir = ".entire/logs"
LogsDir is the directory where log files are stored (relative to repo root).
Variables ¶
This section is empty.
Functions ¶
func Close ¶
func Close()
Close closes the log file if one is open. Flushes any buffered data before closing. Safe to call multiple times.
It also drops the logger itself, so a caller that keeps logging afterwards falls back to slog.Default() rather than writing into a handler bound to the buffer we just closed. That handler holds the *bufio.Writer by value (see createLogger), so leaving it in place would silently swallow every later line: short writes sit in an orphaned buffer nobody flushes, and once it fills the underlying file is already closed. Commands that Close mid-run and keep working should call EnsureInitialized again rather than rely on the fallback.
func EnsureInitialized ¶ added in v0.10.1
EnsureInitialized routes logging to .entire/logs/ unless it is already initialized, and returns a function that tears down only what it set up.
It exists for code that emits logging.* calls from a plain command rather than a hook. Without an initialized logger, log() falls back to slog.Default(), which prints internal structured lines straight onto the user's terminal in the middle of command output — see the same hazard called out at the Init call sites in setup.go and investigate/cmd.go.
Unlike Init it is a no-op when a logger already exists, so a caller reachable from a hook cannot close the hook's log file out from under it. Like Init, it CREATES .entire/logs/, so call it only once the command is committed to doing work in an Entire-enabled repository.
Scope the returned cleanup to everything the command still has to log, not just the call that needed logging in the first place: it closes the file, so anything logged after it runs goes back to the terminal via slog.Default().
func Init ¶
Init initializes the logger for a session, writing JSON logs to .entire/logs/entire.log.
If sessionID is non-empty, it is stored as an slog attribute on every log line for filtering. If the log file cannot be created, falls back to stderr. Log level is controlled by ENTIRE_LOG_LEVEL environment variable.
func SetLogLevelGetter ¶
func SetLogLevelGetter(getter func() string)
SetLogLevelGetter sets a callback function to get the log level from settings. This allows the logging package to read settings without a circular dependency. The callback is only used if ENTIRE_LOG_LEVEL env var is not set.
Types ¶
This section is empty.