Logging — Complete Guide
Logging — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MEAN Stack Tutorial on Toolliyo Academy.
On this page
MEAN Stack Tutorial · Lesson 78 of 100
Logging
Stack ✓ → Projects
Projects · 2 — Apps · ~10 min · MEAN — Performance & Testing
What is this?
Structured logging records JSON events — level, message, correlationId, userId — from MeanVerse Express and Angular errors.
Why should you care?
grep plain text in prod is hopeless; structured logs feed Elasticsearch and alerts.
See it live — copy this example
Paste into your MeanVerse project (Angular + Express + MongoDB), then run with ng serve / node / mongosh as noted.
import pino from 'pino';
import pinoHttp from 'pino-http';
const logger = pino({ level: process.env.LOG_LEVEL ?? 'info' });
app.use(pinoHttp({ logger, genReqId: req => req.headers['x-request-id'] ?? crypto.randomUUID() }));
logger.info({ event: 'transfer_posted', transferId, tenantId, amountCents }, 'Transfer completed');
// Angular global error handler
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
handleError(error: unknown) {
this.log.error({ source: 'angular', error: String(error) });
}
}
What happened?
- pino outputs JSON one line per log.
- pinoHttp adds req id per request.
- Include domain fields for search.
- Angular reports client errors to same pipeline.
Practice next
- Replace console.log with pino in API.
- Pass x-request-id from Nginx through stack.
- Redact passwords and tokens in serializers.
- Add child logger with tenantId bound per request.
- Log slow queries over 200ms threshold.
Remember
Structured JSON logs searchable at scale. Correlation id ties Angular to API lines. Redact secrets and sensitive fields.
Production incident
Transfers fail for one tenant only.
Outcome: Filter tenantId in Kibana finds validation bug in 3 minutes.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!