Snakepit.Logger (Snakepit v0.8.7)

View Source

Centralized, silent-by-default logging for Snakepit.

Configuration

# Silent (default) - only errors
config :snakepit, log_level: :error

# Warnings and errors
config :snakepit, log_level: :warning

# Verbose - info, warnings, errors
config :snakepit, log_level: :info

# Debug - everything
config :snakepit, log_level: :debug

# Completely silent (not even errors)
config :snakepit, log_level: :none

Categories

Enable specific categories for targeted debugging:

config :snakepit, log_categories: [:lifecycle, :grpc]

Process-Level Isolation (for Testing)

Log levels can be set per-process to avoid race conditions in async tests:

# Set log level for current process only
Snakepit.Logger.set_process_level(:debug)

# Execute with temporary log level
Snakepit.Logger.with_level(:warning, fn ->
  # ... code that should log at warning level
end)

# Clear process-level override
Snakepit.Logger.clear_process_level()

The resolution order is:

  1. Process-level override (via set_process_level/1)
  2. Elixir Logger process level (via Logger.put_process_level/2)
  3. Application config (via config :snakepit, log_level: ...)

Summary

Functions

Clear the process-level log level override.

Log at debug level if configured log level allows it.

Log at error level if configured log level allows it.

Get the effective log level for the current process.

Log at info level if configured log level allows it.

Set the log level for the current process only.

Check if logging at the given level is enabled.

Check if logging at the given level/category is enabled.

Log at warning level if configured log level allows it.

Execute a function with a temporary log level for the current process.

Types

category()

@type category() ::
  :lifecycle
  | :pool
  | :grpc
  | :bridge
  | :worker
  | :startup
  | :shutdown
  | :telemetry
  | :general

level()

@type level() :: :debug | :info | :warning | :error | :none

Functions

clear_process_level()

@spec clear_process_level() :: :ok

Clear the process-level log level override.

After calling this, the process will use the global Application config.

debug(message)

debug(category, message)

debug(category, message, metadata)

Log at debug level if configured log level allows it.

error(message)

error(category, message)

error(category, message, metadata)

Log at error level if configured log level allows it.

get_process_level()

@spec get_process_level() :: level()

Get the effective log level for the current process.

Returns the log level in priority order:

  1. Process-level override (set via set_process_level/1)
  2. Elixir Logger process level
  3. Application config

info(message)

info(category, message)

info(category, message, metadata)

Log at info level if configured log level allows it.

set_process_level(level)

@spec set_process_level(level()) :: :ok

Set the log level for the current process only.

This is useful for test isolation - each test process can have its own log level without affecting other concurrent tests.

Examples

Snakepit.Logger.set_process_level(:debug)
# All logging in this process now uses :debug level

Snakepit.Logger.set_process_level(:none)
# All logging in this process is now suppressed

should_log?(level)

Check if logging at the given level is enabled.

should_log?(level, category)

Check if logging at the given level/category is enabled.

warning(message)

warning(category, message)

warning(category, message, metadata)

Log at warning level if configured log level allows it.

with_level(level, fun)

@spec with_level(level(), (-> result)) :: result when result: term()

Execute a function with a temporary log level for the current process.

The previous log level is restored after the function completes, even if it raises an exception.

Examples

Snakepit.Logger.with_level(:debug, fn ->
  # Debug logs are enabled here
  Snakepit.Logger.debug(:pool, "detailed info")
end)
# Previous log level is restored