@evdy-consumer/dailyom-suite-logging
v0.0.7
Published
This library was generated with Nx.
Readme
DailyOM Logging Library
This library was generated with Nx.
The logging library provides a unified logging solution for Next.js and backend systems in the DailyOM Suite monorepo. It implements distributed tracing through trace and span IDs, allowing for correlation of logs across service boundaries
Features
- Trace Context Management: Automatic generation and propagation of trace and span IDs
- Hierarchical Spans: Create child spans for tracking operations within a request
- Configurable Output: Log to console, files, or both
- Daily Log Rotation: Automated log file rotation with size thresholds
- Context Preservation: Maintains trace context across asynchronous operations using AsyncLocalStorage
Usage
Basic Logging
import { TracedLogger } from '@dailyom-suite/logging';
// Create a logger instance
const logger = new TracedLogger({
name: 'my-service',
level: 'info', // Optional: default is 'info'
logToConsole: true, // Optional: default is true
logToFile: true, // Optional: default is true
fileName: 'app-name.log' // Optional: default uses name + timestamp
});
// Log messages with context
logger.info({ userId: '123' }, 'User logged in');
logger.error({ orderId: '456' }, 'Failed to process order');Trace Context and Spans
// Create a traced operation
logger.withSpan('database-query', () => {
// All logs in this function will share the same trace context
logger.debug({ query: 'SELECT * FROM users' }, 'Executing query');
// ...
return result;
});
// For async operations
await logger.withSpanAsync('api-request', async () => {
logger.info({ url: '/api/data' }, 'Making API request');
const response = await fetch('/api/data');
logger.info({ status: response.status }, 'Received API response');
return response.json();
});Manual Trace Context Manipulation
import {
createTraceContext,
createChildSpan,
traceContextStorage
} from '@dailyom-suite/logging';
// Create a new trace context
const context = createTraceContext();
// Create a child span from parent context
const childContext = createChildSpan(context);
// Run code with a specific trace context
traceContextStorage.run(context, () => {
// All logs in this scope will use this context
});Configuration
The library looks for the following environment variables:
- LOG_DIR: Directory where log files will be stored (defaults to ./logs)
Log File Management
Log files follow this naming pattern:
{app-name}.{YYYY-MM-DD}.{HH-MM-SS}.logFiles rotate:
- Every day (24 hours)
- When they exceed 10MB in size
Building
Run nx build logging to build the library.
Running unit tests
Run nx test logging to execute the unit tests via Jest.
CI
PRs that contain changes to logging will run tests and builds to ensure project structure is adhered to
