Structured Logging API Reference¶
Auto-generated API documentation for the structlog module.
structlog
¶
Zero-dependency structured logging with pretty console output.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Structured logging library inspired by structlog, with a loguru-style colored console renderer. Provides bound loggers with context propagation, a processor pipeline, and multiple output renderers (console, JSON, key-value).
Quick start (pretty console output, zero config)::
from structlog import get_logger
logger = get_logger()
logger.info("server started", host="0.0.0.0", port=8080)
Bound logger (context propagation)::
log = get_logger().bind(request_id="abc-123")
log.info("handling request")
log = log.bind(user_id=42)
log.info("authenticated")
One-call setup with stdlib integration::
from structlog import setup_logging
logger = setup_logging(level="DEBUG", renderer="json")
logger.info("structured", key="value")
Custom processor pipeline::
from structlog import configure, add_log_level, TimeStamper, JSONRenderer
configure(processors=[add_log_level, TimeStamper(), JSONRenderer()])
EventDict = dict[str, Any]
module-attribute
¶
A log event dictionary passed through the processor pipeline.
Processor = Callable[[Any, str, EventDict], EventDict | str]
module-attribute
¶
Signature: (logger, method_name, event_dict) -> event_dict or rendered str.
LoggerFactory = Callable[..., Any]
module-attribute
¶
Callable that creates an underlying logger instance.
DropEvent
¶
PrintLogger
¶
Minimal logger that writes to a file handle via print().
This is the default underlying logger. It has no filtering, no
handler chain -- just print() to the configured stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
IO[str] | None
|
Output stream. Defaults to |
None
|
Source code in structlog/structlog.py
PrintLoggerFactory
¶
Factory that creates :class:PrintLogger instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
IO[str] | None
|
Output stream passed to each |
None
|
Source code in structlog/structlog.py
StdlibLoggerFactory
¶
Factory that returns a :class:logging.Logger from the stdlib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Logger name passed to |
None
|
Source code in structlog/structlog.py
TimeStamper
¶
Processor that adds a timestamp to the event dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str | None
|
Timestamp format. |
'iso'
|
utc
|
bool
|
If True, use UTC; otherwise local time. |
True
|
key
|
str
|
Dict key for the timestamp. Defaults to |
'timestamp'
|
Source code in structlog/structlog.py
KeyValueRenderer
¶
Render the event dict as key=value pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_order
|
list[str] | None
|
Keys to render first, in this order. |
None
|
sort_keys
|
bool
|
Sort remaining keys alphabetically. |
False
|
drop_missing
|
bool
|
Skip key_order keys that are absent from the dict. |
True
|
Source code in structlog/structlog.py
JSONRenderer
¶
Render the event dict as a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serializer
|
Callable[..., str]
|
JSON serialization function. Defaults to
:func: |
dumps
|
**dumps_kw
|
Any
|
Extra keyword arguments passed to serializer. |
{}
|
Source code in structlog/structlog.py
ConsoleRenderer
¶
Render the event dict as colorized, loguru-style console output.
Output format::
2026-03-27 14:30:00.123 | INFO | event message key=val key=val
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colors
|
bool | None
|
Enable ANSI color codes. None auto-detects terminal support. |
None
|
pad_event
|
int
|
Pad the event field to this width for alignment. |
30
|
level_styles
|
dict[str, str] | None
|
Override per-level ANSI color strings. |
None
|
Source code in structlog/structlog.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |
BoundLogger
¶
A logger that carries bound context through a processor pipeline.
Do not instantiate directly; use :func:get_logger or
:func:wrap_logger instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Any
|
The underlying logger instance (e.g. |
required |
processors
|
list[Processor]
|
Ordered list of processors to run on each log event. |
required |
context
|
dict[str, Any]
|
Initial context dictionary. |
required |
Source code in structlog/structlog.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
add_log_level(logger, method_name, event_dict)
¶
Add level key derived from the log method name.
Example
logger.info(...) -> event_dict["level"] = "info"
Source code in structlog/structlog.py
add_logger_name(logger, method_name, event_dict)
¶
Add logger key from the underlying logger's name.
format_exc_info(logger, method_name, event_dict)
¶
Replace exc_info with a formatted exception string.
If exc_info is True, captures the current exception via
:func:sys.exc_info. If it is an exception tuple, formats it
directly. The exc_info key is removed and replaced with
exception.
Source code in structlog/structlog.py
configure(processors=None, wrapper_class=None, context_class=None, logger_factory=None, cache_logger_on_first_use=None)
¶
Override the global configuration.
Only non-None arguments are changed. Call :func:reset_defaults to
restore factory settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
processors
|
list[Processor] | None
|
Ordered processor list. |
None
|
wrapper_class
|
type[BoundLogger] | None
|
BoundLogger subclass to use. |
None
|
context_class
|
type[dict] | None
|
Dict-like class for context storage. |
None
|
logger_factory
|
LoggerFactory | None
|
Factory for the underlying logger. |
None
|
cache_logger_on_first_use
|
bool | None
|
Cache loggers returned by
:func: |
None
|
Source code in structlog/structlog.py
reset_defaults()
¶
get_config()
¶
get_logger(*args, **initial_values)
¶
Create a :class:BoundLogger using the global configuration.
Positional arguments are forwarded to the logger factory (e.g. a logger name). Keyword arguments become the initial bound context.
Returns:
| Type | Description |
|---|---|
BoundLogger
|
A configured :class: |
Source code in structlog/structlog.py
wrap_logger(logger, processors=None, **initial_values)
¶
Wrap an existing logger with a processor pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Any
|
Any object with |
required |
processors
|
list[Processor] | None
|
Processor list. Defaults to the global config. |
None
|
**initial_values
|
Any
|
Initial bound context. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
BoundLogger
|
class: |
Source code in structlog/structlog.py
setup_logging(level=logging.INFO, renderer='console', colors=None, processors=None, logger_name=None, stream=None)
¶
One-call logging setup with stdlib integration.
Configures both stdlib logging and the structlog processor
pipeline in a single call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str
|
Log level (name or int). Defaults to |
INFO
|
renderer
|
str
|
Output renderer: |
'console'
|
colors
|
bool | None
|
Enable ANSI colors. None auto-detects. |
None
|
processors
|
list[Processor] | None
|
Custom processor list. Overrides renderer if given. |
None
|
logger_name
|
str | None
|
stdlib logger name. |
None
|
stream
|
IO[str] | None
|
Output stream. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
BoundLogger
|
A ready-to-use :class: |
Source code in structlog/structlog.py
truncate_string(s, max_length, suffix='...')
¶
Truncate s to max_length, appending a count of remaining chars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string to truncate. |
required |
max_length
|
int
|
Maximum number of characters to keep. |
required |
suffix
|
str
|
Separator between the kept text and the count. |
'...'
|
Returns:
| Type | Description |
|---|---|
str
|
The original string if short enough, otherwise truncated with a |
str
|
|
Source code in structlog/structlog.py
truncate_base64(data_url, max_length=100)
¶
Truncate base64 data-URLs for cleaner logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_url
|
str
|
A |
required |
max_length
|
int
|
Maximum base64 payload chars to keep. |
100
|
Returns:
| Type | Description |
|---|---|
str
|
The truncated URL, or the original string if not a data-URL. |