Skip to content

Structured logging with operation context #618

@gijzelaerr

Description

@gijzelaerr

Summary

Enhance logging with structured context (PLC address, operation type, area, DB number) for better observability in multi-PLC environments.

Background

The library currently uses standard Python logging. In production environments with multiple PLC connections, it can be hard to correlate log messages to specific PLCs or operations. Structured logging would make filtering and analysis much easier.

What needs to be done

  • Add structured context to log messages (PLC IP, rack/slot, operation, area, DB number, offset, size)
  • Use logging.LoggerAdapter or similar pattern to inject context automatically
  • Make it optional and backward-compatible (standard logging still works)
  • Consider compatibility with structured logging libraries (structlog, python-json-logger)
  • Add connection ID for tracking specific sessions
  • Log operation timing (duration of reads/writes) at DEBUG level
  • Add documentation on logging configuration

Example

import logging
import snap7

# Standard logging still works
logging.basicConfig(level=logging.DEBUG)

client = snap7.Client()
client.connect("192.168.1.10", 0, 1)
client.db_read(1, 0, 4)
# DEBUG snap7.client [192.168.1.10:0/1] db_read db=1 offset=0 size=4 duration=12ms

Notes

  • Should not add any new dependencies — use stdlib logging
  • Structured logging library integration should be possible but not required
  • Performance overhead of logging should be minimal (lazy formatting)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions