@yigityalim/logger
v0.5.0
Published
Lightweight, flexible logging library with multiple transports and formatters for Node.js and Next.js
Maintainers
Readme
@yigityalim/logger
Structured logging package.
Features
- ✅ Log Levels: debug, info, warn, error
- ✅ Multiple Formatters: JSON, Pretty, Structured
- ✅ Context Propagation: Child loggers with inherited context
- ✅ Performance Monitoring: Track operation duration
- ✅ Log Aggregation: Collect and analyze logs
- ✅ Log Retention: Automatic cleanup with policies
- ✅ File Transport: Write logs to files with rotation
- ✅ Sentry Integration: Error tracking for Next.js and Expo
- ✅ Environment-Aware: Auto-configures based on NODE_ENV
- ✅ Type-Safe: Full TypeScript support
- ✅ Zero Config: Works out of the box
Installation
# Already available in monorepo
import { createLogger } from '@yigityalim/logger';Usage
Basic Usage
import { createLogger } from '@yigityalim/logger';
const logger = createLogger({
name: 'api',
});
logger.info('Server started', { port: 3000 });
logger.error('Database error', new Error('Connection failed'));With Context (Child Loggers)
const logger = createLogger({ name: 'api' });
// Create child logger with context
const userLogger = logger.child({ userId: 'user-123' });
userLogger.info('User logged in');
// Output: [timestamp] [INFO] User logged in context={"userId":"user-123"}
userLogger.info('Profile updated', { field: 'email' });
// Output: [timestamp] [INFO] Profile updated context={"userId":"user-123"} metadata={"field":"email"}Log Levels
logger.debug('Debug message'); // Only in development
logger.info('Info message'); // General information
logger.warn('Warning message'); // Warning
logger.error('Error message', new Error('Something went wrong'));Custom Configuration
const logger = createLogger({
name: 'api',
level: 'info', // Minimum log level
format: 'json', // json | pretty | structured
enabled: true, // Enable/disable logging
context: { // Default context
service: 'api',
version: '1.0.0',
},
});Formatters
Pretty (Development)
17:32:45.123 INFO Server started [{"port":3000}]JSON (Production)
{"timestamp":"2025-01-30T17:32:45.123Z","level":"info","message":"Server started","metadata":{"port":3000}}Structured
[2025-01-30T17:32:45.123Z] [INFO] Server started metadata={"port":3000}Environment Variables
NODE_ENV=production # Auto-selects JSON format and 'info' level
NODE_ENV=development # Auto-selects Pretty format and 'debug' levelExamples
API Server
import { createLogger } from '@yigityalim/logger';
const logger = createLogger({ name: 'api' });
app.listen(3000, () => {
logger.info('Server started', { port: 3000 });
});
app.use((err, req, res, next) => {
logger.error('Request failed', err);
res.status(500).json({ error: 'Internal server error' });
});Job Processing
const logger = createLogger({ name: 'jobs' });
async function processJob(jobId: string) {
const jobLogger = logger.child({ jobId });
jobLogger.info('Job started');
try {
// Process job
jobLogger.info('Job completed');
} catch (error) {
jobLogger.error('Job failed', error);
}
}User Actions
const logger = createLogger({ name: 'app' });
function handleUserAction(userId: string, action: string) {
const userLogger = logger.child({ userId });
userLogger.info('User action', { action });
}Best Practices
- Use Child Loggers: Create child loggers with context for better traceability
- Include Metadata: Add relevant metadata to log entries
- Use Appropriate Levels:
debug: Detailed debugging informationinfo: General informational messageswarn: Warning messageserror: Error messages with stack traces
- Sanitize Sensitive Data: Never log passwords, tokens, or PII
License
MIT
