getContext()
Get the current context within a function handler.Context
Current execution context
Example: Basic Usage
Logger
The Logger provides structured logging with automatic trace correlation.Methods
info()
Log an informational message.string
required
Log message
unknown
Optional structured data to include in the log
warn()
Log a warning message.string
required
Warning message
unknown
Optional structured data
error()
Log an error message.string
required
Error message
unknown
Optional error details or structured data
debug()
Log a debug message.string
required
Debug message
unknown
Optional debug data
Example: Structured Logging
Automatic Trace Correlation
Logs are automatically correlated with traces:trace_id, making it easy to trace requests across services.
Trace Span
The trace span allows adding custom attributes, events, and status to the current trace.setAttribute()
Add a custom attribute to the span.string
required
Attribute key
string | number | boolean
required
Attribute value
addEvent()
Add a timestamped event to the span.string
required
Event name
Record<string, any>
Optional event attributes
setStatus()
Set the span status.recordException()
Record an exception in the span.Example: Rich Tracing
withContext()
Manually set context for async operations (advanced use case).(context: Context) => Promise<T>
required
Async function to execute with the context
Context
required
Context object to use
T
The function’s return value
Example: Custom Context
Logger Constructor
Create a standalone logger instance outside of function handlers.string
Optional trace ID for correlation
string
Optional service name
string
Optional span ID
Example: Standalone Logger
Best Practices
Always use structured logging
Always use structured logging
Add meaningful trace attributes
Add meaningful trace attributes
Use appropriate log levels
Use appropriate log levels
Add events for important milestones
Add events for important milestones
Handle errors with trace context
Handle errors with trace context
Context Outside Handlers
If you callgetContext() outside a function handler, you get a default context with a basic logger: