-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-app.js
More file actions
86 lines (73 loc) · 3.11 KB
/
Copy pathexpress-app.js
File metadata and controls
86 lines (73 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Example Express app using the Log Ingestor SDK.
*
* Install deps: npm install express winston winston-transport
* Run: STREAMLOGIA_API_KEY=... STREAMLOGIA_PROJECT_ID=... node express-app.js
*/
import express from "express";
import winston from "winston";
import Transport from "winston-transport";
import { LogIngestorClient } from "@streamlogia/javascript-sdk";
// ── 1. Create the client ─────────────────────────────────────────────────────
const logs = new LogIngestorClient({
apiKey: process.env.STREAMLOGIA_API_KEY,
projectId: process.env.STREAMLOGIA_PROJECT_ID,
source: "payment-service",
});
// ── 2. Set up the logger ─────────────────────────────────────────────────────
// createWinstonTransport mirrors NewSlogHandler from the Go SDK — it adapts
// the ingestor client to Winston so you use one logger everywhere.
//
// Adding winston.transports.Console alongside it mirrors MultiHandler in Go:
// logs go to both stdout (captured by systemd / journalctl) AND the ingestor.
const LogTransport = logs.createWinstonTransport(Transport);
const logger = winston.createLogger({
level: "debug",
transports: [
new winston.transports.Console({ format: winston.format.simple() }),
new LogTransport(),
],
});
// Flush on shutdown — important so buffered logs aren't lost.
process.on("SIGTERM", async () => {
await logs.close();
process.exit(0);
});
// ── 3. Mount the middleware once at the top ───────────────────────────────────
const app = express();
app.use(express.json());
// Every request → one log entry with method, path, status, duration, and
// response size. No per-route code needed.
app.use(logs.expressMiddleware());
// ── 4. Route handlers — use the winston logger for business events ────────────
app.post("/payments", async (req, res) => {
const { customerId, amount, currency } = req.body;
try {
// Simulate payment processing...
const paymentId = `pay_${Math.random().toString(36).slice(2)}`;
// Business event: use the winston logger so the entry goes to both stdout
// and the ingestor (mirrors MultiHandler usage in the Go SDK examples).
logger.info("payment processed", {
paymentId,
amount,
currency,
customerId,
});
res.status(201).json({ paymentId });
} catch (err) {
logger.error("payment failed", { customerId, error: err.message });
res.status(500).json({ error: "payment failed" });
}
});
app.get("/payments/:id", async (req, res) => {
const { id } = req.params;
// The middleware already logs GET /payments/:id — no manual log needed unless
// you want to add business context.
res.json({ id, status: "completed" });
});
const server = app.listen(4000, () => {
logger.info("server started", { port: 4000 });
});
server.on("error", (err) => {
console.error("Server error:", err);
});