Advertisement
Open Source Projects by Phil Schwartz

Building a Python Log Conversion Service

Operational logs rarely arrive in a consistent shape. One system may emit newline-delimited JSON, another produce comma-separated values, and a third combine timestamps, key-value pairs, and free text. Converting these records can become repetitive when every script handles quoting, missing values, encoding, and timestamps differently.

A Python service that converts between different log formats, such as JSON to CSV, can centralise that work. It gives developers one interface for transforming exported logs while retaining the flexibility to handle application-specific fields and target systems.

The project fits naturally alongside Phil Schwartz’s open-source tools, including DenyHosts, Kodos, and Scratchy. A focused converter could remain small and dependable, use permissive licensing, and provide both a command-line program and an API for local or containerised use.

Australian deployments add practical considerations. Teams in Sydney, Melbourne, and Brisbane often exchange logs across remote environments, while daylight-saving changes create ambiguous local timestamps. Privacy rules, sector-specific obligations, and the expectation of transparent documentation also shape how such a service should be designed.

Why format conversion remains useful

Log analysis tools tend to specialise. A notebook is comfortable with JSON, a spreadsheet expects tabular data, and many command-line utilities prefer line-oriented text. Instead of writing another one-off importer, developers can send records through a shared conversion layer with predictable rules.

The service should support common inputs such as JSON, JSON Lines, CSV, and key-value logs, while producing JSON, JSON Lines, or CSV. Field selection, renaming, filtering, and ordering can be handled through an explicit mapping. This avoids relying on dictionary insertion order or silently discarding fields when destination formats cannot represent nested structures.

Designing a predictable data contract

Every conversion request should define its source format, target format, and field mapping. The API can accept a request such as POST /v1/convert, while the command-line interface could expose equivalent options for scripts. Automatic detection may be offered later, but explicit selection produces fewer surprises.

Nested objects require a documented policy. They can be flattened using dotted paths, retained as JSON strings, or rejected when the chosen target cannot preserve them. Arrays need similar treatment: joining them into a delimited string is convenient, whereas repeating rows may be more appropriate for certain CSV exports. Null values should remain distinct from empty strings.

Controlled vocabularies also deserve careful handling. Domain labels, such as those shown in roulette betting categories, must not be renamed or reinterpreted without an explicit rule. A stable mapping preserves meaning while allowing operational fields to be standardised.

Implementing the Python pipeline

A workable implementation can begin with Python’s json, csv, and datetime modules. FastAPI or another lightweight framework can provide the HTTP layer, while Pydantic can validate requests. Streaming iterators keep memory use bounded when a customer uploads a large export or connects a live log source.

Each record should pass through parsing, validation, mapping, transformation, and serialisation stages. Validation errors should identify the source record and field without exposing the original payload. Conversion should then be deterministic: the same input and mapping should always produce the same field order, timestamp representation, and CSV dialect.

Security needs attention at the output stage. Quoted CSV cells can still execute formulas in spreadsheet software if they begin with =, +, -, or @. A safe-cell option can prefix such values or reject them. Unicode support, byte-order-mark handling, line endings, and configurable delimiters should also be covered by the service contract.

Security and Australian operating conditions

Log files often contain IP addresses, usernames, customer identifiers, or request URLs. Field allowlists and redaction rules can prevent sensitive values from reaching less protected destinations. Access to an API should use authentication, request-size limits, rate limits, and audit events that record who requested each conversion.

For Australian organisations, the Privacy Act 1988 and the Notifiable Data Breaches scheme are relevant whenever logs contain personal information. An incident affecting log data may require assessment against the scheme’s criteria. The service should minimise retention, offer configurable deletion periods, and avoid writing source records to application logs by default.

Regulatory context varies by sector. APRA-regulated financial organisations may need to align with CPS 230, while Australian government suppliers commonly reference the ASD Essential Eight. Timestamp handling should preserve the source offset, include an ISO 8601 representation, and allow conversion to UTC without confusing Australia’s multiple time zones or daylight-saving periods.

Testing and releasing an open-source tool

A small fixture suite should cover ordinary records as well as empty arrays, nulls, duplicate columns, malformed JSON, embedded newlines, and non-ASCII text. Golden files make output changes visible during reviews, while property-based testing can check invariants such as record counts and consistent field mappings. Integration tests should exercise both the CLI and HTTP interface.

Distribution can follow the pattern used by other portfolio projects: source archives, a tagged release, installation instructions, dependency status, usage statistics, and an MIT licence. Container images and a Python package index entry would make adoption easier, but the core tool should remain usable without a server.

Release readiness depends on clear behaviour and dependable packaging: