Logger
Logging is a foundational requirement for debugging, monitoring, and operating production-grade backend systems.
The Servercn Logger component provides two interchangeable logging strategies so you can choose what best fits your application:
- Winston — flexible, feature-rich, file-based logging
- Pino — ultra-fast, JSON-first logging optimized for performance
Both approaches follow the same philosophy:
- Centralized configuration
- Environment-aware behavior
- Structured log output
- Production-safe defaults
Installation Guide
Install the component using the Servercn CLI:
npx servercn-cli add loggerYou will be prompted to select a file upload provider:
The CLI will then automatically configure the component based on your selected library.
Choosing a Logger
Prerequisites
Ensure the following environment variables are defined before running the application:
src/configs/env.ts
Ensure the following configuration are defined:
Basic Implementation
1. Winston Logger ✔️
Winston is ideal if you want:
- File-based logs
- Daily rotation
- Human-readable console output
- Multiple log destinations
Winston Configuration
src/utils/logger.ts
Minimal Configuration
Advanced Configuration
Winston Usage Example
Winston Usage Output
Child Logger
2. Pino Logger ✅
Pino is designed for speed and structured logging. It is the recommended choice for:
- High-throughput APIs
- Containerized deployments
- Centralized log aggregation (ELK, Loki, Datadog)
Pino Configuration
src/utils/logger.ts
Minimal Configuration
Advanced Configuration
Pino Usage Example
Pino Usage Output
Child Logger