Skip to content

Commit 7d5ca6c

Browse files
acn-ericlawclaude
andcommitted
feat: log.format compact (JSONL) + sample resources/application.yml for the demo
log.format now carries the engines' three presentations: text (default), json (pretty-printed, matching the engine's pretty serializer) and compact (single-line JSONL for log aggregators). The demo app gains examples/resources/application.yml - the engines' resources convention, auto-loaded next to the app file - wiring the demo.health dependency and demonstrating the well-known keys. README quick start teaches the -D override syntax instead of --port. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent a674198 commit 7d5ca6c

7 files changed

Lines changed: 68 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
`mandatory.health.dependencies` / `optional.health.dependencies` and called through
1010
the event bus. `/health` answers `UP` (200) / `DOWN` (400); `/livenessprobe` follows
1111
the most recent health outcome.
12+
- `log.format` carries the engines' three presentations: `text` (default), `json`
13+
(pretty-printed) and `compact` (single-line JSONL for log aggregators). A sample
14+
`examples/resources/application.yml` demonstrates the resources convention and the
15+
well-known keys.
1216
- Primitive in-process event bus - the single dispatch pipeline: one FIFO mailbox per
1317
route consumed by `instances` worker tasks (the parameter is faithful); RPC deliveries
1418
are ttl-bounded with a dead-work skip; drop-n-forget returns the 202-shape ack. The

‎README.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Run it:
4444

4545
```bash
4646
pip install -e '.[dev]'
47-
mercury-serve app.py --port 8086
47+
mercury-serve app.py -Drest.server.port=8086
4848
```
4949

5050
Call it from a Mercury engine application with two configuration entries and no code —
@@ -108,12 +108,14 @@ The same conventions as the engines, so a polyglot installation stays uniform:
108108
|-----|---------|---------|
109109
| `application.name` | application identity in logs | `application` |
110110
| `rest.server.port` | Event API port | `8085` |
111-
| `log.format` | `text` or `json` | `text` |
111+
| `log.format` | `text`, `json` (pretty-printed) or `compact` (single-line JSONL) | `text` |
112112
| `log.level` | log level (`LOG_LEVEL` env var wins) | `INFO` |
113113

114114
Configuration lives in the `resources` folder, mirroring the engines:
115-
`resources/application.yml` (or `.yaml` / `.properties`), or an explicit `--config` path.
116-
Values support `${ENV_VAR:default}` substitution. Runtime parameter overrides use the
115+
`resources/application.yml` (or `.yaml` / `.properties`) in the working directory or next
116+
to the application file, or an explicit `--config` path — see
117+
[`examples/resources/application.yml`](examples/resources/application.yml) for a worked
118+
sample. Values support `${ENV_VAR:default}` substitution. Runtime parameter overrides use the
117119
same `-D` syntax as the Java engine and the Rust port — checked first on every read
118120
(`AppConfig.set(key, value)` does the same programmatically, the `f:setConfig` analog):
119121

‎examples/demo_app.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""
22
Demo polyglot functions.
33
4-
Run: mercury-serve examples/demo_app.py --port 8086
4+
Run: mercury-serve examples/demo_app.py
5+
6+
Configuration comes from examples/resources/application.yml (the engines'
7+
"resources" convention - port 8086, the demo.health dependency, log format);
8+
override any key with -Dkey=value, e.g. -Drest.server.port=8090.
59
610
Then map a route from a Mercury engine application (event-over-http.yaml):
711
@@ -63,8 +67,8 @@ async def chain(_headers: dict[str, str], body: Body):
6367
async def health_check(headers: dict[str, str], _body: Body):
6468
"""Health check speaking the engines' interface contract (type=info / type=health).
6569
66-
Activate it for the /health actuator endpoint:
67-
mercury-serve examples/demo_app.py -Dmandatory.health.dependencies=demo.health
70+
Activated for the /health actuator endpoint by mandatory.health.dependencies
71+
in examples/resources/application.yml (or a -D override).
6872
"""
6973
if headers.get("type") == "info":
7074
return {"service": "demo.service", "href": "http://127.0.0.1"}

‎examples/resources/application.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample configuration - the engines' "resources" convention.
2+
# mercury-serve loads resources/application.yml from the working directory, or from
3+
# the resources folder next to the application file (this one). Any key can be
4+
# overridden at run time with the engines' -D syntax, e.g.:
5+
# mercury-serve examples/demo_app.py -Drest.server.port=8090 -Dlog.format=compact
6+
7+
application.name: 'demo-app'
8+
info.app.description: 'Mercury Composable polyglot demo'
9+
10+
rest.server.port: 8086
11+
12+
# text (default) | json (pretty-printed) | compact (single-line JSONL)
13+
log.format: text
14+
log.level: INFO
15+
16+
# Actuator /health dependencies - routes of health check functions speaking the
17+
# engines' type=info / type=health contract (demo.health in demo_app.py).
18+
mandatory.health.dependencies: 'demo.health'
19+
20+
# Opt-in lists for the /env endpoint - nothing is ever dumped wholesale.
21+
show.env.variables: 'LOG_LEVEL'
22+
show.application.properties: 'application.name, rest.server.port'

‎src/mercury_composable/config.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
2323
- ``application.name`` — application identity used in logs.
2424
- ``rest.server.port`` — the Event API port (default 8085).
25-
- ``log.format`` — ``text`` (default) or ``json``.
25+
- ``log.format`` — ``text`` (default), ``json`` (pretty-printed) or
26+
``compact`` (single-line JSONL), the engines' three presentations.
2627
- ``log.level`` — default INFO; the ``LOG_LEVEL`` environment variable wins,
2728
mirroring the engines' log4j2 setting.
2829
"""

‎src/mercury_composable/log.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
- The level comes from the ``LOG_LEVEL`` environment variable when set
1414
(mirroring the engines), else the ``log.level`` configuration key,
1515
else INFO.
16-
- ``log.format=json`` switches to one JSON object per line with the same
17-
information (time, level, logger, message, and trace_id when a trace
18-
context is active).
16+
- ``log.format`` carries the engines' three presentations: ``text``
17+
(default), ``json`` (pretty-printed JSON with time, level, logger,
18+
message, and trace_id when a trace context is active) and ``compact``
19+
(the same object on a single line - JSONL - for log aggregators).
1920
"""
2021

2122
from __future__ import annotations
@@ -43,6 +44,12 @@ def format(self, record: logging.LogRecord) -> str:
4344

4445

4546
class EngineJsonFormatter(logging.Formatter):
47+
"""Engine JSON presentations: json = pretty-printed, compact = one line (JSONL)."""
48+
49+
def __init__(self, *, compact: bool = False):
50+
super().__init__()
51+
self._indent = None if compact else 2
52+
4653
def format(self, record: logging.LogRecord) -> str:
4754
ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(record.created))
4855
entry = {
@@ -58,7 +65,7 @@ def format(self, record: logging.LogRecord) -> str:
5865
entry["trace_id"] = info.trace_id
5966
if record.exc_info:
6067
entry["exception"] = self.formatException(record.exc_info)
61-
return json.dumps(entry, ensure_ascii=False)
68+
return json.dumps(entry, ensure_ascii=False, indent=self._indent)
6269

6370

6471
def _setup() -> None:
@@ -69,7 +76,10 @@ def _setup() -> None:
6976
level_name = os.environ.get("LOG_LEVEL") or str(config.get("log.level", "INFO"))
7077
log_format = str(config.get("log.format", "text")).lower()
7178
handler = logging.StreamHandler(sys.stdout)
72-
handler.setFormatter(EngineJsonFormatter() if log_format == "json" else EngineTextFormatter())
79+
if log_format in ("json", "compact"):
80+
handler.setFormatter(EngineJsonFormatter(compact=log_format == "compact"))
81+
else:
82+
handler.setFormatter(EngineTextFormatter())
7383
root = logging.getLogger()
7484
root.handlers.clear()
7585
root.addHandler(handler)

‎tests/test_log.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ def test_json_formatter_carries_exception():
3333
assert entry["logger"] == "unit.test:42"
3434
assert entry["message"] == "Async event demo.route failed"
3535
assert "RuntimeError: boom-x" in entry["exception"]
36+
37+
38+
def test_json_is_pretty_and_compact_is_jsonl():
39+
import json
40+
41+
record = _record_with_exception()
42+
pretty = EngineJsonFormatter().format(record)
43+
compact = EngineJsonFormatter(compact=True).format(record)
44+
# engine presentations: json = pretty-printed, compact = single line (JSONL)
45+
assert "\n" in pretty
46+
assert "\n" not in compact
47+
assert json.loads(pretty) == json.loads(compact) # same information, two renderings

0 commit comments

Comments
 (0)