spakky-logging¶
구조화된 로깅 — Aspect 기반 자동 로깅, 컨텍스트 주입
Aspect¶
spakky.plugins.logging.aspects.logging_aspect
¶
Logging aspects for automatic method call logging with masking and timing.
Provides sync and async AOP aspects that intercept methods annotated
with @Logging and emit structured log messages including:
- Method name and arguments (with sensitive data masking)
- Return value (truncated to
max_result_length) - Execution duration
- Slow-call warnings when
slow_threshold_msis exceeded
AsyncLoggingAspect(config=None)
¶
Bases: IAsyncAspect
Aspect for logging async method calls with execution time and data masking.
Initialize the async logging aspect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LoggingConfig | None
|
Optional logging configuration. Uses defaults if None. |
None
|
Source code in plugins/spakky-logging/src/spakky/plugins/logging/aspects/logging_aspect.py
around_async(joinpoint, *args, **kwargs)
async
¶
Log async method execution with timing and masking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
joinpoint
|
AsyncFunc
|
The async method being intercepted. |
required |
*args
|
Any
|
Positional arguments to the method. |
()
|
**kwargs
|
Any
|
Keyword arguments to the method. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result of the method execution. |
Raises:
| Type | Description |
|---|---|
Exception
|
Re-raises any exception after logging it. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/aspects/logging_aspect.py
LoggingAspect(config=None)
¶
Bases: IAspect
Aspect for logging synchronous method calls with execution time and data masking.
Initialize the sync logging aspect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LoggingConfig | None
|
Optional logging configuration. Uses defaults if None. |
None
|
Source code in plugins/spakky-logging/src/spakky/plugins/logging/aspects/logging_aspect.py
around(joinpoint, *args, **kwargs)
¶
Log sync method execution with timing and masking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
joinpoint
|
Func
|
The sync method being intercepted. |
required |
*args
|
Any
|
Positional arguments to the method. |
()
|
**kwargs
|
Any
|
Keyword arguments to the method. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result of the method execution. |
Raises:
| Type | Description |
|---|---|
Exception
|
Re-raises any exception after logging it. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/aspects/logging_aspect.py
options: show_root_heading: false
어노테이션¶
spakky.plugins.logging.annotation
¶
Logging annotation for automatic method call logging.
Logged(enable_masking=True, masking_keys=list(), slow_threshold_ms=None, max_result_length=None, log_args=True, log_result=True)
dataclass
¶
Bases: FunctionAnnotation
Annotation for enabling automatic method logging.
Methods decorated with @logged() will have their calls, arguments,
return values, and execution time automatically logged.
Attributes:
| Name | Type | Description |
|---|---|---|
enable_masking |
bool
|
Whether to mask sensitive data in logs. |
masking_keys |
list[str]
|
List of keys whose values should be masked.
When empty, the global :attr: |
slow_threshold_ms |
float | None
|
Per-method slow-call warning threshold.
|
max_result_length |
int | None
|
Maximum repr length for the return value.
|
log_args |
bool
|
Whether to include arguments in the log message. |
log_result |
bool
|
Whether to include the return value in the log message. |
enable_masking = True
class-attribute
instance-attribute
¶
Whether to mask sensitive data in logs.
masking_keys = field(default_factory=list)
class-attribute
instance-attribute
¶
Keys whose values should be masked. Empty = use global config.
slow_threshold_ms = None
class-attribute
instance-attribute
¶
Per-method slow-call threshold (ms). None = use global config.
max_result_length = None
class-attribute
instance-attribute
¶
Max repr length for return values. None = use global config.
log_args = True
class-attribute
instance-attribute
¶
Whether to include arguments in log output.
log_result = True
class-attribute
instance-attribute
¶
Whether to include the return value in log output.
logged(enable_masking=True, masking_keys=None, slow_threshold_ms=None, max_result_length=None, log_args=True, log_result=True)
¶
Decorator for enabling automatic method logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enable_masking
|
bool
|
Whether to mask sensitive data in logs. |
True
|
masking_keys
|
list[str] | None
|
List of keys whose values should be masked.
When empty, the global :attr: |
None
|
slow_threshold_ms
|
float | None
|
Per-method slow-call warning threshold.
|
None
|
max_result_length
|
int | None
|
Maximum repr length for the return value.
|
None
|
log_args
|
bool
|
Whether to include arguments in the log message. |
True
|
log_result
|
bool
|
Whether to include the return value in the log message. |
True
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[P, R]], Callable[P, R]]
|
A decorator that applies the logging annotation to a method. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/annotation.py
options: show_root_heading: false
설정¶
spakky.plugins.logging.config
¶
Logging configuration for Spakky Framework.
Provides a Configuration that controls how the framework's logging system is set up: format, levels, masking, and slow-call thresholds.
LogFormat
¶
Bases: StrEnum
Supported log output formats.
LoggingConfig()
¶
Bases: BaseSettings
Configuration for the Spakky logging system.
Attributes:
| Name | Type | Description |
|---|---|---|
level |
int
|
Root logger level (e.g. |
format |
LogFormat
|
Output format — |
date_format |
str
|
|
package_levels |
dict[str, int]
|
Per-logger level overrides, keyed by logger name. |
mask_keys |
list[str]
|
Global list of sensitive keys to mask in |
mask_replacement |
str
|
Replacement text for masked values. |
slow_threshold_ms |
float
|
Millisecond threshold for slow-call warnings in |
max_result_length |
int
|
Maximum character length for result |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/config.py
options: show_root_heading: false
컨텍스트¶
spakky.plugins.logging.context
¶
Log context management using contextvars.
Provides a thread-safe and async-safe mechanism for binding contextual key-value pairs to log records. Bound values are automatically available to all loggers within the same execution context (asyncio task or thread).
LogContext
¶
Manages contextual key-value pairs for structured logging.
Values bound via :meth:bind are propagated through contextvars
and injected into every log record by :class:ContextInjectingFilter.
bind(**kwargs)
classmethod
¶
Add key-value pairs to the current log context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
str
|
Key-value pairs to bind. |
{}
|
Source code in plugins/spakky-logging/src/spakky/plugins/logging/context.py
unbind(*keys)
classmethod
¶
Remove keys from the current log context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
str
|
Keys to remove. |
()
|
Source code in plugins/spakky-logging/src/spakky/plugins/logging/context.py
clear()
classmethod
¶
get()
classmethod
¶
Return a copy of the current log context.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Current context key-value pairs. |
scope(**kwargs)
classmethod
¶
Temporarily bind values for the duration of a with block.
Previous context is restored when the block exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
str
|
Key-value pairs to bind within the scope. |
{}
|
Yields:
| Type | Description |
|---|---|
Generator[None]
|
None |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/context.py
options: show_root_heading: false
Filters¶
spakky.plugins.logging.filters
¶
Logging filter that injects LogContext values into log records.
Attaches all key-value pairs from the current :class:LogContext
as attributes on every :class:logging.LogRecord, making them
available to formatters.
ContextInjectingFilter
¶
Bases: Filter
Filter that injects :class:LogContext values into log records.
When added to a logger or handler, every record will carry the
current context as extra attributes and a consolidated context
dict attribute.
filter(record)
¶
Inject context values into the log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to augment. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/filters.py
options: show_root_heading: false
Formatters¶
spakky.plugins.logging.formatters
¶
Log formatters for Spakky Framework.
Provides three output formats: - Text: Traditional human-readable single-line logs. - JSON: Machine-parseable structured logs (one JSON object per line). - Pretty: Coloured multi-column layout for local development.
SpakkyTextFormatter(datefmt=DEFAULT_DATE_FORMAT)
¶
Bases: Formatter
Human-readable single-line formatter.
Format::
2026-03-15T14:30:00+09:00 | INFO | myapp.service | message
Initialize the text formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datefmt
|
str
|
Date format string for timestamps. |
DEFAULT_DATE_FORMAT
|
Source code in plugins/spakky-logging/src/spakky/plugins/logging/formatters.py
format(record)
¶
Format the log record as a pipe-separated text line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted log string. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/formatters.py
SpakkyJsonFormatter(datefmt=DEFAULT_DATE_FORMAT)
¶
Bases: Formatter
Structured JSON formatter (one object per line).
Output::
{"timestamp":"...","level":"INFO","logger":"...","message":"...","context_id":"..."}
Initialize the JSON formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datefmt
|
str
|
Date format string for timestamps. |
DEFAULT_DATE_FORMAT
|
Source code in plugins/spakky-logging/src/spakky/plugins/logging/formatters.py
format(record)
¶
Format the log record as a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
JSON-encoded log string. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/formatters.py
SpakkyPrettyFormatter
¶
Bases: Formatter
Coloured multi-column formatter for local development.
Format::
14:30:00.123 | INFO | myapp.service | req-abc123 |
message text here
format(record)
¶
Format the log record with colours and alignment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Colourised, aligned log string. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/formatters.py
options: show_root_heading: false
후처리기¶
spakky.plugins.logging.post_processor
¶
Post-processor that configures Python logging on application start.
Reads :class:LoggingConfig from the container and applies:
- Root logger level and handler with the selected formatter
- Per-package level overrides
- :class:
ContextInjectingFilteron the root logger
LoggingSetupPostProcessor()
¶
Bases: IPostProcessor, IContainerAware
Post-processor that configures Python logging from LoggingConfig.
Runs once on the first post_process call. Subsequent calls
are no-ops for the setup logic; the pod itself is returned unchanged.
Source code in plugins/spakky-logging/src/spakky/plugins/logging/post_processor.py
post_process(pod)
¶
Configure logging on first invocation, then pass through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pod
|
object
|
The Pod instance being processed. |
required |
Returns:
| Type | Description |
|---|---|
object
|
The unmodified Pod instance. |
Source code in plugins/spakky-logging/src/spakky/plugins/logging/post_processor.py
options: show_root_heading: false
추가 모듈¶
LogContextBinder — Pod adapter for LogContext.
LogContextBinder
¶
Bases: ILogContextBinder
Pod-managed adapter that delegates to LogContext classmethods.
Registered as the ILogContextBinder implementation so that other
plugins can depend on the core interface without importing
LogContext directly.
Plugin initialization entry point.
initialize(app)
¶
Initialize the spakky-logging plugin.
Registers the logging configuration, setup post-processor, sync/async logging aspects, and the log context binder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
SpakkyApplication
|
The SpakkyApplication instance. |
required |