Skip to main content
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)

Log Output Formats

Development Format (Pretty-printed)

Production Format (JSON)

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:
  1. Check LOG_LEVEL: Ensure itโ€™s set to debug or trace
  2. Check NODE_ENV: Development mode enables debug by default
  3. Restart Server: Environment changes require restart

Performance Issues

If logging is impacting performance:
  1. Increase Log Level: Use info or warn in production
  2. Remove Debug Logs: Clean up excessive debug statements
  3. 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.