Debug Logging in Content SDK Apps — A Developer’s Guide

When building headless applications with Sitecore Content SDK, debugging becomes crucial especially as your component structure and data dependencies grow. Instead of writing console.log everywhere, the Content SDK provides a much cleaner and controlled approach to logging.

The SDK uses the popular debug module for displaying debugging information. This allows developers to enable or disable logging dynamically using environment variables, making it easier to troubleshoot issues without modifying code.

Debug logging is disabled by default, keeping terminal noise to a minimum during normal development. Once you need insights into what the Content SDK is doing—just turn it on!

Enabling Debug Logs

  1. Use the DEBUG environment variable to control what logs are shown.
  2. When logging is enabled and you run your application, all triggered instances of debug() relevant to the namespaces you choose to include will be displayed in your console.
  3. To enable all logs, set the DEBUG environment variable to content-sdk:* DEBUG=content-sdk:*
  4. To enable specific namespaces only, set the DEBUG environment variable like DEBUG=content-sdk:layout

to only log layout-service-related debug messages.

  1. To enable multiple namespaces, You can also combine multiple namespaces like DEBUG=content-sdk:layout,content-sdk:dictionary
  2. Or exclude some namespaces like DEBUG=content-sdk:*,-content-sdk:layout This shows all except layout logs.

Namespaces / Categories

The Content SDK defines a set of namespaces for debug logs. Some of the key ones:

Namespace

What It Logs

content-sdk:http

HTTP request/response logging (for the fetch / request wrappers)

content-sdk:dictionary

Logs from the dictionary (localization) service

content-sdk:layout

Layout service logs (how pages/components are fetched)

content-sdk:editing

Logs related to the SitecoreAI editor (preview / editing integration)

content-sdk:sitemap

Logs from the sitemap service

content-sdk:robots

For robots.txt service

content-sdk:redirects

For redirect service / middleware in Next.js

content-sdk:errorpages

Error-page service logs

content-sdk:multisite

Multi-site service & Next.js multisite middleware

content-sdk:personalize

Personalization / A-B test service logs

content-sdk:common

Common messages across SDK

Advanced / Optional Debug Config

There are additional environment variables to tweak how debug output looks

Env Variable

Description

DEBUG_HIDE_DATE

Whether to hide timestamps in debug logs (especially for non-TTY). Default: false

DEBUG_COLORS

Enable/disable colored output. Default: true

DEBUG_DEPTH

Controls how deeply nested objects are inspected when logging. Default: 2

DEBUG_MULTILINE

Whether to pretty-print objects on multiple lines. Default: false

DEBUG_SHOW_HIDDEN

Whether to show hidden properties in objects. Default: false

Scope / When It Works

    • Debug logs are only for server-side code. According to the official docs, they don’t apply in the browser / client-side.
    • When you run production builds (or use npm run start:production), you can still enable debug logging by setting the DEBUG env var accordingly.

 Why Use Debug Logging in Content SDK

  • Troubleshooting: Useful to trace what GraphQL / layout queries are being made, and how the client is handling them.
  • Performance Analysis: Check HTTP request / response payloads and timings.
  • Debugging Middleware: If you have custom middleware (editing host, personalization, sitemap) — logs help you understand internal behavior.
  • Selective Logging: You don’t need to flood your logs. You can target specific areas (layout, dictionary, HTTP) using namespaces.

 Example Usage - To debug layout + dictionary:

  • Add to .env.local: DEBUG=content-sdk:layout,content-sdk:dictionary
  • Run the app: npm run dev
  • For production: DEBUG=content-sdk:layout,content-sdk:dictionary npm run start:production

Happy Coding!

 


Comments

Popular posts from this blog

Migrating from Sitecore JSS SDK to Content SDK – Best Practices and Pitfalls

Beginner’s Guide: Step-by-Step Local Installation for Sitecore XM Cloud

Setting Up Sitecore Headless with Next.js in Disconnected Mode