ColdFusion Logging and Observability

Because "it works on my machine" doesn't scale

Implement structured logging with JSON format and distributed tracing for comprehensive observability in ColdFusion 2025 applications.

Structured Logging Principles

You should move beyond plain text logs to structured, machine-parseable formats that make analysis and debugging easier:

  • JSON Format: Easy to parse and query
  • Consistent Fields: timestamp, level, message, context
  • Correlation IDs: Track requests across services
  • Contextual Data: User ID, session ID, request ID

Container Logging Strategy

For containerized applications, you should follow the twelve-factor app methodology for logging:

  • Write logs to stdout/stderr rather than to files on disk
  • Use JSON layout for structured output that's easy to parse
  • Let your container orchestration platform handle log collection and forwarding
  • Centralize all logs in an aggregation system like Elasticsearch, CloudWatch, or Splunk
Log4j JSON Configuration
<!-- Configure Log4j for JSON output to stdout -->
<!-- Generic Log4j 2 example. Check Adobe's documentation for your release before -->
<!-- changing ColdFusion's own logging, test in staging and back up files first. -->
<!-- CF 2025 ships cfusion/lib/log4j2.properties (properties syntax, no log4j2.xml); -->
<!-- it configures library logs such as axis2, hibernate and esclient. -->
<!-- Log4j documents JsonLayout (which uses Jackson) as deprecated; prefer JsonTemplateLayout. -->
<Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <JsonLayout compact="true" eventEol="true" />
    </Console>
</Appenders>

Log Levels and When to Use Them

  • FATAL: Application cannot continue
  • ERROR: Operation failed, needs attention
  • WARN: Potentially harmful situation
  • INFO: Important business events
  • DEBUG: Detailed diagnostic info (dev/staging only)
  • TRACE: Very detailed debugging (dev only)
Production: Set log level to INFO. DEBUG and TRACE create excessive logs and impact performance.

ColdFusion Logging Best Practices

Logging Examples
<!-- Basic logging -->
<cflog
    file="application"
    type="information"
    text="User logged in: userID=#userID#">

<!-- Include correlation ID for request tracking -->
<cflog
    file="application"
    type="error"
    text="Payment failed: orderID=#orderID#, correlationID=#request.correlationID#">

<!-- Log structured data as JSON -->
<cflog
    file="application"
    type="information"
    text="#serializeJSON({
        'event': 'orderCompleted',
        'orderID': orderID,
        'amount': total,
        'userID': userID,
        'correlationID': request.correlationID
    })#">
// Basic logging
writeLog(
    file = "application",
    type = "information",
    text = "User logged in: userID=#userID#"
);

// Include correlation ID for request tracking
writeLog(
    file = "application",
    type = "error",
    text = "Payment failed: orderID=#orderID#, correlationID=#request.correlationID#"
);

// Log structured data as JSON
writeLog(
    file = "application",
    type = "information",
    text = serializeJSON({
        "event": "orderCompleted",
        "orderID": orderID,
        "amount": total,
        "userID": userID,
        "correlationID": request.correlationID
    })
);

Log Rotation and Retention

You should configure log rotation to prevent disk space exhaustion on your servers:

  • Traditional Servers: Use Log4j RollingFileAppender
  • Containers: Let platform handle rotation (Kubernetes, Docker)
  • Retention: 30-90 days depending on compliance requirements
  • Archival: Move old logs to object storage (S3, Azure Blob)

Metrics and Monitoring

You should expose key metrics for monitoring systems to track application health and performance:

  • JMX Metrics: Heap usage, GC stats, thread pools (monitor with JVM tuning)
  • PMT Integration: Built-in performance metrics
  • Custom Metrics: Business KPIs, error rates
  • Health Endpoints: /healthcheck.cfm for load balancers
Health Check Endpoint
<!--- Simple health check endpoint --->
<cfset response = {
    "status": "healthy",
    "timestamp": now(),
    "version": "2025.0.1",
    "checks": {
        "database": checkDatabase(),
        "cache": checkCache()
    }
}>
<cfcontent type="application/json">
<cfoutput>#serializeJSON(response)#</cfoutput>
// Simple health check endpoint
response = {
    "status": "healthy",
    "timestamp": now(),
    "version": "2025.0.1",
    "checks": {
        "database": checkDatabase(),
        "cache": checkCache()
    }
};

cfcontent(type="application/json");
writeOutput(serializeJSON(response));

Distributed Tracing

You should implement distributed tracing for microservices architectures to track requests across multiple services:

  • Generate a unique trace ID for each incoming request
  • Propagate the trace ID to all downstream service calls
  • Use W3C Trace Context standard headers for interoperability
  • Include the trace ID in all log messages to correlate events
  • An OpenTelemetry Java agent or APM tool can propagate trace context for you
Distributed Tracing
<!--- W3C traceparent: 00-<32 hex trace-id>-<16 hex span-id>-<flags> --->
<cfset headers = getHTTPRequestData().headers>
<cfif structKeyExists(headers, "traceparent") AND listLen(headers.traceparent, "-") EQ 4>
    <!--- Keep the incoming trace ID --->
    <cfset request.traceID = listGetAt(headers.traceparent, 2, "-")>
<cfelse>
    <!--- createUUID() has 32 hex digits once the dashes are removed --->
    <cfset request.traceID = lCase(replace(createUUID(), "-", "", "all"))>
</cfif>
<!--- New span ID for this hop --->
<cfset spanID = left(lCase(replace(createUUID(), "-", "", "all")), 16)>
<cfset request.traceparent = "00-#request.traceID#-#spanID#-01">

<!--- Propagate to downstream services --->
<cfhttp url="#serviceURL#" method="GET">
    <cfhttpparam type="header" name="traceparent" value="#request.traceparent#">
</cfhttp>

<!--- Include in all logs --->
<cflog file="application" type="information"
    text="Processing request: traceID=#request.traceID#">
// W3C traceparent: 00-<32 hex trace-id>-<16 hex span-id>-<flags>
headers = getHTTPRequestData().headers;
if (structKeyExists(headers, "traceparent") && listLen(headers.traceparent, "-") == 4) {
    // Keep the incoming trace ID
    request.traceID = listGetAt(headers.traceparent, 2, "-");
} else {
    // createUUID() has 32 hex digits once the dashes are removed
    request.traceID = lCase(replace(createUUID(), "-", "", "all"));
}
// New span ID for this hop
spanID = left(lCase(replace(createUUID(), "-", "", "all")), 16);
request.traceparent = "00-#request.traceID#-#spanID#-01";

// Propagate to downstream services
cfhttp(url=serviceURL, method="GET") {
    cfhttpparam(type="header", name="traceparent", value=request.traceparent);
}

// Include in all logs
writeLog(
    file="application",
    type="information",
    text="Processing request: traceID=#request.traceID#"
);

Security and Compliance

  • Never log passwords, credit card numbers, or personally identifiable information (PII)
  • Implement masking or redaction for sensitive data that must be logged
  • Encrypt logs both in transit and at rest to protect sensitive information
  • Implement appropriate log retention policies to comply with regulations like GDPR and HIPAA
  • Control access to log files and logging systems using proper authentication and authorization

Logging Checklist

  • JSON structured logging configured
  • Log level set to INFO in production
  • Logs sent to centralized aggregation
  • Correlation IDs implemented
  • Log rotation configured
  • Sensitive data excluded from logs
  • Health check endpoint implemented
  • Metrics exposed via JMX or PMT

Gotchas

  • DEBUG logging in production causes performance degradation and disk fills
  • Logging inside tight loops creates massive log volume
  • Synchronous logging can block request threads - use async appenders
  • Log aggregation systems have ingestion limits - monitor quota
  • PII in logs creates compliance violations - scrub sensitive data
  • Missing log rotation fills disk and crashes server

Need Help?

Convective can help implement observability best practices for ColdFusion applications.

Find out more
ColdFusion Logging and Observability - Structured Logging and Tracing Guide | CFGuide