The DeployStack backend uses Pino logger with Fastify for high-performance, structured logging. This guide covers everything you need to know about configuring and using log levels effectively.
Overview
DeployStackโs logging system is built on industry best practices:
- Pino Logger: Ultra-fast JSON logger for Node.js
- Fastify Integration: Native logging support with request correlation
- Environment-based Configuration: Automatic log level adjustment based on NODE_ENV
- Structured Logging: JSON output for production, pretty-printed for development
Available Log Levels
Log levels are ordered by severity (lowest to highest):
Configuration
Environment Variables
Set the log level using the LOG_LEVEL environment variable:
Default Behavior
The logger automatically adjusts based on your environment:
Default Levels:
- Development:
debug (shows debug, info, warn, error, fatal)
- Production:
info (shows info, warn, error, fatal)
Logger Parameter Injection Pattern
DeployStack follows a consistent pattern for passing logger instances to services and utilities. This ensures proper structured logging throughout the application while maintaining the Fastify logger chain for request correlation.
โ
DO: Pass Logger as Parameter to Services
โ
DO: Pass Logger from Calling Context
โ
DO: Use Child Loggers for Persistent Context
โ DONโT: Create Separate Logger Utilities
โ DONโT: Use console.* in Services
Developer Best Practices
โ
DO: Use Proper Log Levels
โ DONโT: Use Manual Prefixes
โ
DO: Use the Fastify Logger
โ
DO: Add Context Objects
Context objects make your logs searchable, filterable, and much more useful for debugging. Always include relevant context that helps identify what happened, where, and to whom.
Best Practices for Context Objects:
- Always include
operation: A consistent field that describes what operation was being performed
- Add identifiers: Include relevant IDs (userId, orderId, sessionId, etc.) for easy filtering
- Include request context: IP addresses, user agents, request IDs for web requests
- Add timing information: Duration, timestamps, or performance metrics when relevant
- Use consistent naming: Stick to camelCase and consistent field names across your application
Examples of Good Context Properties:
operation: What was happening (e.g., โsend_emailโ, โuser_loginโ, โdatabase_queryโ)
userId, sessionId, requestId: Identifiers for tracking
duration, responseTime: Performance metrics
statusCode, method, endpoint: HTTP-related context
table, queryType: Database-related context
recipient, template, messageId: Email-related context
โ
DO: Use Child Loggers for Context
Common Logging Patterns
Database Operations
Authentication & Security
API Requests
Plugin System
Fixing Console.log Issues
Important: Replace all console.log statements with proper Pino logger calls to ensure consistent formatting and log level filtering.
Problem: Inconsistent Log Output
Solution: Use Proper Logger
Passing Logger to Classes
Environment-Specific Configuration
Development Environment
Features:
- Pretty-printed, colorized output
- Shows debug and trace information
- Includes timestamps and emojis
- Easier to read during development
Production Environment
Features:
- Structured JSON output
- Optimized for log aggregation
- Excludes debug information
- Better performance
Testing Environment
Features:
- Minimal log output during tests
- Only shows errors and fatal messages
- Reduces test noise
Troubleshooting
Debug Mode Not Working
If debug logs arenโt showing:
- Check LOG_LEVEL: Ensure itโs set to
debug or trace
- Check NODE_ENV: Development mode enables debug by default
- Restart Server: Environment changes require restart
If logging is impacting performance:
- Increase Log Level: Use
info or warn in production
- Remove Debug Logs: Clean up excessive debug statements
- Use Async Logging: Pino handles this automatically
Log Aggregation
For production log management:
Migration Guide
From Manual Prefixes
From Console.log
Summary
- Use proper log levels instead of manual prefixes
- Replace console.log with server.log for consistency
- Add structured context to make logs searchable
- Configure LOG_LEVEL via environment variables
- Use child loggers for persistent context
- Follow the patterns shown in this guide
With proper log level configuration, youโll have a production-ready logging system that scales from development to enterprise deployments.