Installation
Basic Usage
Critical Logging Pattern
The first argument must always be a static string. All variables should be passed in the object as the second argument.- Log aggregation tools can group logs by the static message
- Searching logs by message becomes predictable
- Variable data is properly indexed and queryable
Error Logging Pattern
When logging errors, the error object must be in a parameter namederror in the second argument object:
- Stack traces are properly captured
- Error serialization works correctly
- Log analysis tools can extract error details
Features
Log Levels
The logger supports the standard log levels:error: For error conditionswarn: For warning conditionsinfo: For informational messagesdebug: For debugging information
Automatic Contextual Information
Each log message automatically includes:- Timestamp
- Log level
- File name where the log was triggered (automatically detected)
- OpenTelemetry trace and span IDs (when available)
Formatting
The logger supports two main output formats:-
Text Format (for development):
- Colorized output
- Human-readable formatting
- Activated when
ORION_DEV=1environment variable is set
-
JSON Format (for production):
- Structured JSON logs
- Ideal for log aggregation and analysis
- Default in production environments
OpenTelemetry Integration
The logger automatically detects and includes OpenTelemetry trace and span IDs when available, enabling correlation between logs and traces.Advanced Configuration
Setting Log Level
Adding Custom Transports
Complete Logger Configuration
Best Practices
-
Use Static Message Strings: The first argument must be a static string for proper log aggregation:
-
Pass Error Objects Correctly: Use the
errorproperty name: -
Add Context: Use
logger.addContext(module)at the top of your files to automatically include file information. -
Use Appropriate Levels:
debug: Detailed information useful during developmentinfo: Normal application behavior, milestoneswarn: Unexpected but handled issueserror: Errors that prevent proper operation
-
Include Relevant Data: Pass structured objects with relevant context: