|
| 1 | +# Advanced Configuration |
| 2 | + |
| 3 | +Clarity offers detailed configuration options to fine-tune logging behavior for various environments and use cases. This guide explains advanced configuration strategies and techniques. |
| 4 | + |
| 5 | +## Configuration Layers |
| 6 | + |
| 7 | +Clarity implements a layered configuration system: |
| 8 | + |
| 9 | +1. **Default configuration** - Sensible defaults that work out of the box |
| 10 | +2. **Environment variables** - Override defaults through environment settings |
| 11 | +3. **Configuration files** - Project-specific configuration files |
| 12 | +4. **Instance configuration** - Per-logger instance options |
| 13 | + |
| 14 | +This layered approach allows for global settings while enabling customization for specific components. |
| 15 | + |
| 16 | +## Using Configuration Files |
| 17 | + |
| 18 | +Clarity uses [bunfig](https://github.com/wobsoriano/bunfig) to support multiple configuration file formats: |
| 19 | + |
| 20 | +```ts |
| 21 | +// clarity.config.ts |
| 22 | +import type { ClarityConfig } from 'clarity' |
| 23 | + |
| 24 | +const config: Partial<ClarityConfig> = { |
| 25 | + level: 'debug', |
| 26 | + format: 'json', |
| 27 | + rotation: { |
| 28 | + frequency: 'daily', |
| 29 | + maxSize: 5 * 1024 * 1024, // 5MB |
| 30 | + compress: true, |
| 31 | + }, |
| 32 | +} |
| 33 | + |
| 34 | +export default config |
| 35 | +``` |
| 36 | + |
| 37 | +### Supported File Types |
| 38 | + |
| 39 | +- TypeScript: `clarity.config.ts` |
| 40 | +- JavaScript: `clarity.config.js` |
| 41 | +- JSON: `clarity.config.json` |
| 42 | +- Package.json: Include a `clarity` property |
| 43 | + |
| 44 | +### Configuration File Location |
| 45 | + |
| 46 | +Clarity automatically searches for configuration files in: |
| 47 | + |
| 48 | +1. Current working directory |
| 49 | +2. Project root directory |
| 50 | +3. User's home directory (for global settings) |
| 51 | + |
| 52 | +## Environment Variables |
| 53 | + |
| 54 | +Environment variables provide a way to configure Clarity without modifying code: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Set log level |
| 58 | +export CLARITY_LOG_LEVEL=debug |
| 59 | + |
| 60 | +# Set log directory |
| 61 | +export CLARITY_LOG_DIR=/var/log/myapp |
| 62 | + |
| 63 | +# Enable verbose mode |
| 64 | +export CLARITY_VERBOSE=true |
| 65 | + |
| 66 | +# Disable colored output |
| 67 | +export CLARITY_COLORS=false |
| 68 | + |
| 69 | +# Set output format |
| 70 | +export CLARITY_FORMAT=json |
| 71 | + |
| 72 | +# Disable timestamps |
| 73 | +export CLARITY_TIMESTAMP=false |
| 74 | +``` |
| 75 | + |
| 76 | +Environment variables take precedence over configuration files. |
| 77 | + |
| 78 | +## Dynamic Configuration |
| 79 | + |
| 80 | +For applications that need to change logging behavior at runtime: |
| 81 | + |
| 82 | +```ts |
| 83 | +import { config as globalConfig, Logger } from 'clarity' |
| 84 | + |
| 85 | +// Create logger with initial configuration |
| 86 | +const logger = new Logger('app', { |
| 87 | + level: 'info', |
| 88 | +}) |
| 89 | + |
| 90 | +// Update logging level dynamically |
| 91 | +function setDebugMode(enabled: boolean) { |
| 92 | + logger.setLevel(enabled ? 'debug' : 'info') |
| 93 | +} |
| 94 | + |
| 95 | +// Update global configuration |
| 96 | +function updateGlobalConfig(newConfig) { |
| 97 | + Object.assign(globalConfig, newConfig) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Environment-Specific Configuration |
| 102 | + |
| 103 | +Configure Clarity differently based on the runtime environment: |
| 104 | + |
| 105 | +```ts |
| 106 | +import { Logger } from 'clarity' |
| 107 | +import { isDevelopment, isProduction, isTest } from './env-helpers' |
| 108 | + |
| 109 | +// Base config |
| 110 | +const baseConfig = { |
| 111 | + defaultName: 'myapp', |
| 112 | + timestamp: true, |
| 113 | +} |
| 114 | + |
| 115 | +// Environment-specific settings |
| 116 | +const envConfig = isDevelopment() |
| 117 | + ? { |
| 118 | + level: 'debug', |
| 119 | + format: 'text', |
| 120 | + fancy: true, |
| 121 | + } |
| 122 | + : isTest() |
| 123 | + ? { |
| 124 | + level: 'error', // Only log errors in tests |
| 125 | + format: 'json', |
| 126 | + enabled: process.env.LOG_IN_TESTS === 'true', |
| 127 | + } |
| 128 | + : isProduction() |
| 129 | + ? { |
| 130 | + level: 'info', |
| 131 | + format: 'json', |
| 132 | + rotation: { |
| 133 | + frequency: 'daily', |
| 134 | + maxSize: 10 * 1024 * 1024, |
| 135 | + maxFiles: 30, |
| 136 | + compress: true, |
| 137 | + }, |
| 138 | + } |
| 139 | + : {} |
| 140 | + |
| 141 | +// Create logger with merged config |
| 142 | +const logger = new Logger('app', { |
| 143 | + ...baseConfig, |
| 144 | + ...envConfig, |
| 145 | +}) |
| 146 | +``` |
| 147 | + |
| 148 | +## Configuration Objects |
| 149 | + |
| 150 | +### Complete Configuration Example |
| 151 | + |
| 152 | +```ts |
| 153 | +import type { ClarityConfig } from 'clarity' |
| 154 | + |
| 155 | +const completeConfig: ClarityConfig = { |
| 156 | + // Log level determines which messages are recorded |
| 157 | + level: 'info', |
| 158 | + |
| 159 | + // Default name for loggers created without explicit names |
| 160 | + defaultName: 'clarity', |
| 161 | + |
| 162 | + // Whether to include timestamps in log output |
| 163 | + timestamp: true, |
| 164 | + |
| 165 | + // Whether to use colors in console output |
| 166 | + colors: true, |
| 167 | + |
| 168 | + // Default output format (text or JSON) |
| 169 | + format: 'text', |
| 170 | + |
| 171 | + // Maximum size of individual log files in bytes |
| 172 | + maxLogSize: 10 * 1024 * 1024, // 10MB |
| 173 | + |
| 174 | + // Date pattern for rotated log files |
| 175 | + logDatePattern: 'YYYY-MM-DD', |
| 176 | + |
| 177 | + // Directory to store log files |
| 178 | + logDirectory: './logs', |
| 179 | + |
| 180 | + // Log rotation configuration |
| 181 | + rotation: { |
| 182 | + // Time-based rotation |
| 183 | + frequency: 'daily', |
| 184 | + rotateHour: 0, |
| 185 | + rotateMinute: 0, |
| 186 | + rotateDayOfWeek: 0, // Sunday |
| 187 | + rotateDayOfMonth: 1, |
| 188 | + |
| 189 | + // Size-based rotation |
| 190 | + maxSize: 10 * 1024 * 1024, // 10MB |
| 191 | + |
| 192 | + // Retention policy |
| 193 | + maxFiles: 7, |
| 194 | + maxAge: 30, // days |
| 195 | + |
| 196 | + // Compression |
| 197 | + compress: true, |
| 198 | + |
| 199 | + // File pattern |
| 200 | + pattern: '%DATE%.log', |
| 201 | + |
| 202 | + // Encryption |
| 203 | + encrypt: { |
| 204 | + algorithm: 'aes-256-gcm', |
| 205 | + compress: true, |
| 206 | + keyRotation: { |
| 207 | + enabled: true, |
| 208 | + interval: 7, // days |
| 209 | + maxKeys: 5, |
| 210 | + }, |
| 211 | + }, |
| 212 | + }, |
| 213 | + |
| 214 | + // Whether to enable verbose output |
| 215 | + verbose: false, |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +### Rotation Configuration |
| 220 | + |
| 221 | +```ts |
| 222 | +import type { RotationConfig } from 'clarity' |
| 223 | + |
| 224 | +const rotationConfig: RotationConfig = { |
| 225 | + // Time-based rotation options |
| 226 | + frequency: 'daily', // 'hourly', 'daily', 'weekly', 'monthly', or 'none' |
| 227 | + rotateHour: 0, // Hour of day to rotate (0-23) |
| 228 | + rotateMinute: 0, // Minute of hour to rotate (0-59) |
| 229 | + rotateDayOfWeek: 0, // Day of week to rotate on (0=Sunday, 6=Saturday) |
| 230 | + rotateDayOfMonth: 1, // Day of month to rotate on (1-31) |
| 231 | + |
| 232 | + // Size-based rotation |
| 233 | + maxSize: 10 * 1024 * 1024, // Rotate when file exceeds this size (bytes) |
| 234 | + |
| 235 | + // Retention options |
| 236 | + maxFiles: 10, // Maximum number of rotated files to keep |
| 237 | + maxAge: 30, // Maximum age of rotated files in days |
| 238 | + |
| 239 | + // Compression |
| 240 | + compress: true, // Compress rotated files with gzip |
| 241 | + |
| 242 | + // Custom filename pattern |
| 243 | + pattern: '%DATE%.log', // Uses date pattern from config |
| 244 | + |
| 245 | + // Encryption options |
| 246 | + encrypt: true, // Enable encryption with default settings |
| 247 | + // OR |
| 248 | + encrypt: { |
| 249 | + algorithm: 'aes-256-gcm', // Encryption algorithm |
| 250 | + compress: true, // Compress before encrypting |
| 251 | + keyRotation: { |
| 252 | + enabled: true, // Enable key rotation |
| 253 | + interval: 30, // Rotate keys every 30 days |
| 254 | + maxKeys: 3, // Keep up to 3 previous keys |
| 255 | + }, |
| 256 | + }, |
| 257 | +} |
| 258 | +``` |
| 259 | + |
| 260 | +### Extended Logger Options |
| 261 | + |
| 262 | +```ts |
| 263 | +import type { ExtendedLoggerOptions } from 'clarity' |
| 264 | + |
| 265 | +const loggerOptions: ExtendedLoggerOptions = { |
| 266 | + // Log level |
| 267 | + level: 'debug', |
| 268 | + |
| 269 | + // Output format |
| 270 | + format: 'json', |
| 271 | + |
| 272 | + // Log directory |
| 273 | + logDirectory: './logs', |
| 274 | + |
| 275 | + // Rotation config |
| 276 | + rotation: { /* see rotation config example */ }, |
| 277 | + |
| 278 | + // Custom formatter |
| 279 | + formatter: customFormatter, |
| 280 | + |
| 281 | + // Terminal output options |
| 282 | + fancy: true, // Enable fancy terminal output |
| 283 | + showTags: true, // Show namespace tags in output |
| 284 | + tagFormat: { prefix: '[', suffix: ']' }, // Format for namespace tags |
| 285 | + timestampPosition: 'right', // Position timestamp on right side |
| 286 | + |
| 287 | + // Fingers-crossed mode (buffer logs until error occurs) |
| 288 | + fingersCrossedEnabled: true, |
| 289 | + fingersCrossed: { |
| 290 | + activationLevel: 'error', // Level that triggers log flushing |
| 291 | + bufferSize: 100, // Maximum number of entries to buffer |
| 292 | + flushOnDeactivation: true, // Flush buffer when deactivated |
| 293 | + stopBuffering: false, // Stop buffering after activation |
| 294 | + }, |
| 295 | + |
| 296 | + // Enable/disable logging |
| 297 | + enabled: true, |
| 298 | +} |
| 299 | +``` |
| 300 | + |
| 301 | +## Configuration Best Practices |
| 302 | + |
| 303 | +1. **Use environment-specific configurations** |
| 304 | + - Debug level in development |
| 305 | + - Info or higher in production |
| 306 | + - Minimal logging in tests |
| 307 | + |
| 308 | +2. **Set appropriate log rotation** |
| 309 | + - Daily rotation for most applications |
| 310 | + - Size-based rotation for high-volume logs |
| 311 | + - Keep enough history without wasting storage |
| 312 | + |
| 313 | +3. **Use JSON format in production** |
| 314 | + - Machine-parseable for log analysis tools |
| 315 | + - Contains structured data for filtering |
| 316 | + |
| 317 | +4. **Enable encryption for sensitive logs** |
| 318 | + - Use for logs containing personal data |
| 319 | + - Configure key rotation for enhanced security |
| 320 | + |
| 321 | +5. **Create namespaced loggers** |
| 322 | + - Use logger.extend() for component-specific logging |
| 323 | + - Helps filter logs by component |
| 324 | + |
| 325 | +6. **Leverage fingers-crossed mode** |
| 326 | + - Reduces disk I/O for normal operation |
| 327 | + - Ensures complete context when errors occur |
| 328 | + |
| 329 | +7. **Configure log directory appropriately** |
| 330 | + - Use node-writeable directories in production |
| 331 | + - Consider log management and rotation policies |
| 332 | + |
| 333 | +By mastering these advanced configuration techniques, you can tailor Clarity's behavior to suit your application's specific logging requirements. |
0 commit comments