Traceflow
Traceflow is a unified logging system that provides contextual logging across workflows, activities, and console commands. It automatically routes messages to the appropriate target based on the execution context.Overview
Traceflow allows you to log messages using a single API (traceflow()) that automatically adapts to different execution contexts:
- Workflows: Messages are stored in the database as
traceflow_logs - Activities: Messages are stored in the database and linked to the parent workflow
- Console Commands: Messages are displayed in the terminal with progress bar integration
- Default: Messages are written to Laravel’s log files
Basic Usage
Use thetraceflow() helper function to log messages:
Available Methods
info(string $message, ?array $context = null)
Log an informational message:
success(string $message, ?array $context = null)
Log a success message:
warn(string $message, ?array $context = null)
Log a warning message:
error(string $message, ?Throwable $exception = null)
Log an error message. Optionally include an exception:
throw(string $message, Throwable $exception)
Log an error and re-throw the exception:
raw(mixed $message)
Log raw data without formatting:
incrementProcessedCount(int $amount = 1)
Increment the processed count for progress tracking:
incrementFailureCount()
Increment the failure count for tracking:
Targets
Traceflow automatically routes messages to different targets based on the execution context:WorkflowTarget
When used in workflows, messages are stored in thetraceflow_logs table:
ActivityTarget
When used in activities, messages are stored in the database and linked to the parent workflow:CommandTarget
When used in console commands, messages are displayed in the terminal:info() messages are displayed as hints below the progress bar to avoid creating new lines.
LogTarget
When no specific target is set, messages are written to Laravel’s log files:Usage Examples
In Workflows
In Activities
In Console Commands
Context Data
All methods accept optional context data that is stored with the log entry:traceflow_logs table and can be viewed in the workflow detail page.
Progress Tracking
Traceflow integrates with progress tracking in workflows and commands:- Workflows:
incrementProcessedCount()updates the workflow’sprocessed_count - Activities:
incrementProcessedCount()updates the parent workflow’sprocessed_count - Commands:
incrementProcessedCount()advances the progress bar (if active)
Best Practices
- Use descriptive messages: Include relevant information in your log messages
- Include context: Pass context data for better debugging
- Track progress: Use
incrementProcessedCount()for long-running operations - Handle errors: Always log errors with exceptions for full stack traces
- Use appropriate levels: Use
info()for general messages,warn()for warnings,error()for errors
Configuration
The default target can be configured inconfig/core.php: