Customizing Log Formatting
Learn how to customize the formatting of your log messages with DoubleTie Logger.
How to Customize Log Formatting
This guide shows you how to customize the formatting of your log messages with DoubleTie Logger.
Prerequisites
- A basic logger setup from the Getting Started section
Steps
1. Register a custom formatter
DoubleTie Logger allows you to create custom formatters that control how your log messages appear. Let's create a JSON formatter for machine-readable logs:
When this code runs, your logs will be output as JSON strings.
2. Apply formatters to specific logs
You can also manually format individual log messages using a specific formatter:
3. Creating a minimal formatter
Here's an example of a minimal formatter that removes colors and timestamps:
4. Creating a custom colorful formatter
You can also create formatters with custom colors:
Formatter API
A formatter is a function with the following signature:
Parameter | Description |
---|---|
level | The log level ('error', 'warn', 'info', 'success', 'debug') |
message | The message string being logged |
args | Array of additional arguments passed to the log method |
appName | Optional application name for namespace separation |
Default Formatter
DoubleTie Logger comes with a default formatter that:
- Shows timestamps
- Color-codes log levels
- Includes the app name when provided
- Pretty-formats any objects passed as arguments
You can always revert to the default formatter if needed: