@bernierllc/logger

中文文档
2.0.0发布于 2026/11/05MITTypeScriptCommonJS近 30 天下载 3745

Structured logging with multiple transports, log levels, and correlation tracking for BernierLLC ecosystem

原始使用文档
文档目录36 节
  1. Features
  2. Installation
  3. Quick Start
  4. Usage Examples
  5. Basic Logging
  6. Performance Tracking
  7. Correlation Tracking
  8. Child Loggers
  9. Data Sanitization
  10. Custom Transports
  11. Multiple Transports
  12. API Reference
  13. Logger
  14. Log Levels
  15. Transports
  16. Formatters
  17. Runtime Configuration
  18. Quick Setup
  19. Configuration Discovery
  20. CLI Commands
  21. Environment Variables
  22. Complete Documentation
  23. Configuration
  24. LoggerOptions
  25. LogContext
  26. Error Handling
  27. Performance
  28. Best Practices
  29. Integration Status
  30. Logger Integration
  31. Docs-Suite Integration
  32. NeverHub Integration
  33. Development
  34. Migration: zod 4 (breaking)
  35. License
  36. Related Packages

@bernierllc/logger

Production-ready structured logging with multiple transports, log levels, correlation tracking, and performance monitoring for the BernierLLC ecosystem.

Features

  • Structured Logging: JSON-based structured log output with metadata
  • Multiple Transports: Console, file, HTTP, database, and custom transports
  • Log Levels: Configurable log levels with filtering (ERROR, WARN, INFO, HTTP, VERBOSE, DEBUG, SILLY)
  • Correlation Tracking: Request/trace ID correlation across async operations
  • Performance Tracking: Duration tracking, timers, and profiling
  • Data Sanitization: Automatic PII protection and sensitive data masking
  • Context Management: Rich context and metadata support
  • Child Loggers: Contextual logger inheritance
  • Graceful Error Handling: Transport failure recovery and error callbacks

Installation

npm install @bernierllc/logger

Quick Start

import { Logger, LogLevel, ConsoleTransport, FileTransport } from '@bernierllc/logger';

const logger = new Logger({
  level: LogLevel.INFO,
  context: {
    service: 'user-service',
    version: '1.0.0',
    environment: process.env.NODE_ENV || 'development'
  },
  transports: [
    new ConsoleTransport({ level: LogLevel.DEBUG }),
    new FileTransport({ 
      filename: 'app.log',
      level: LogLevel.INFO 
    })
  ],
  sanitize: true,
  enableCorrelation: true
});

logger.info('Application started');
logger.error('Database connection failed', new Error('Connection timeout'));

Usage Examples

Basic Logging

import { createLogger } from '@bernierllc/logger';

const logger = createLogger({
  context: {
    service: 'my-service',
    version: '1.0.0',
    environment: 'production'
  }
});

logger.info('User logged in', { userId: '123', email: '[email protected]' });
logger.warn('High memory usage', { memoryUsage: '85%' });
logger.error('Payment failed', new Error('Invalid card'));

Performance Tracking

// Using timers
const timer = logger.startTimer('database-query');
await performDatabaseQuery();
timer.end('Query completed successfully');

// Using time wrapper
const result = await logger.time('cache-operation', async () => {
  return await cacheData(key, value);
});

// Using profiling
logger.profile('data-processing');
await processLargeDataset();
logger.profile('data-processing'); // Logs duration

Correlation Tracking

import { Correlation, CorrelationManager } from '@bernierllc/logger';

// Express middleware
app.use((req, res, next) => {
  const manager = CorrelationManager.getInstance();
  const context = manager.fromHeaders(req.headers);
  
  Correlation.run(context, () => {
    req.logger = logger.child({
      requestId: context.correlationId,
      method: req.method,
      url: req.url
    });
    next();
  });
});

// In route handlers
app.get('/users/:id', async (req, res) => {
  req.logger.info('Fetching user', { userId: req.params.id });
  // All logs will include correlation ID automatically
});

Child Loggers

const parentLogger = logger.child({
  component: 'auth-service'
});

const childLogger = parentLogger.child({
  subcomponent: 'token-validator'
});

childLogger.info('Token validated'); 
// Includes both component and subcomponent in context

Data Sanitization

const logger = new Logger({
  sanitize: true,
  sanitizeFields: ['password', 'token', 'creditCard', 'ssn'],
  transports: [new ConsoleTransport()]
});

logger.info('User data', {
  username: '[email protected]',
  password: 'secret123',    // Will be [REDACTED]
  age: 30                   // Will be preserved
});

Custom Transports

class SlackTransport implements LogTransport {
  name = 'slack';
  level = LogLevel.ERROR;

  async write(entry: LogEntry): Promise<void> {
    if (entry.level <= LogLevel.ERROR) {
      await this.sendToSlack(entry.message, entry.metadata);
    }
  }

  private async sendToSlack(message: string, metadata: any) {
    // Send to Slack webhook
  }
}

const logger = new Logger({
  transports: [
    new ConsoleTransport(),
    new SlackTransport()
  ]
});

Multiple Transports

const logger = new Logger({
  level: LogLevel.INFO,
  transports: [
    new ConsoleTransport({
      level: LogLevel.DEBUG,
      formatter: new TextFormatter()
    }),
    new FileTransport({
      filename: 'app.log',
      level: LogLevel.INFO,
      maxSize: 10 * 1024 * 1024, // 10MB
      maxFiles: 5
    }),
    new HTTPTransport({
      url: 'https://logs.example.com/api',
      level: LogLevel.WARN,
      headers: { 'Authorization': 'Bearer token' },
      batchSize: 100
    })
  ]
});

API Reference

Logger

Constructor

new Logger(options: LoggerOptions)

Methods

  • info(message: string, metadata?: object) - Log info message
  • warn(message: string, metadata?: object) - Log warning message
  • error(message: string, error?: Error, metadata?: object) - Log error message
  • debug(message: string, metadata?: object) - Log debug message
  • verbose(message: string, metadata?: object) - Log verbose message
  • silly(message: string, metadata?: object) - Log silly message
  • startTimer(label: string): LogTimer - Start performance timer
  • time(label: string, fn: () => Promise<T>): Promise<T> - Time async operation
  • profile(label: string) - Profile code section
  • child(context: Partial<LogContext>): Logger - Create child logger
  • setCorrelationId(id: string) - Set correlation ID
  • setUserId(id: string) - Set user ID
  • flush(): Promise<void> - Flush all transports
  • close(): Promise<void> - Close logger and transports

Log Levels

enum LogLevel {
  ERROR = 0,   // Highest priority
  WARN = 1,
  INFO = 2,
  HTTP = 3,
  VERBOSE = 4,
  DEBUG = 5,
  SILLY = 6    // Lowest priority
}

Transports

ConsoleTransport

Logs to console with appropriate console methods (error, warn, log, debug).

FileTransport

Logs to files with rotation support.

HTTPTransport

Sends logs to HTTP endpoints with batching and retry logic.

DatabaseTransport

Stores logs in databases with connection pooling.

Formatters

JSONFormatter

Outputs structured JSON logs.

TextFormatter

Outputs human-readable text logs.

StructuredFormatter

Outputs flattened structured logs with prefixed fields.

Runtime Configuration

The logger supports runtime configuration through config files, allowing you to customize logging behavior without modifying code.

Quick Setup

  1. Initialize configuration:

    npm run config:init
    
  2. Edit the generated logger.config.js:

    module.exports = {
      level: 'info',
      context: {
        service: 'my-service',
        version: '1.0.0'
      },
      console: {
        enabled: true,
        level: 'debug'
      }
    };
    
  3. Use in your code:

    import { createLoggerFromConfig } from '@bernierllc/logger';
    
    const logger = await createLoggerFromConfig();
    logger.info('Application started');
    

Configuration Discovery

The logger automatically discovers configuration files in this order:

  1. logger.config.js (JavaScript - recommended)
  2. logger.config.cjs (CommonJS)
  3. logger.config.mjs (ES modules)
  4. logger.config.json (JSON)
  5. package.json → "logger" key

CLI Commands

# Initialize configuration
npm run config:init

# Validate configuration
npm run config:validate

# Print resolved configuration
npm run config:print

Environment Variables

All configuration options can be overridden with LOGGER_* environment variables:

LOGGER_LEVEL=debug
LOGGER_SERVICE=my-service
LOGGER_CONSOLE_LEVEL=info
LOGGER_FILE_ENABLED=true
LOGGER_FILE_FILENAME=logs/app.log

Complete Documentation

For detailed configuration options, examples, and migration guide, see: Logger Runtime Configuration Documentation

Configuration

LoggerOptions

interface LoggerOptions {
  level: LogLevel;                    // Minimum log level
  transports: LogTransport[];         // Array of transports
  context?: Partial<LogContext>;      // Default context
  enableCorrelation?: boolean;        // Enable correlation tracking
  sanitize?: boolean;                 // Enable data sanitization
  sanitizeFields?: string[];          // Fields to sanitize
  formatter?: LogFormatter;           // Default formatter
  onError?: (error: Error, transport: string) => void; // Error handler
}

LogContext

interface LogContext {
  service: string;        // Service name
  version: string;        // Service version
  environment: string;    // Environment (dev, staging, prod)
  hostname?: string;      // Server hostname
  pid?: number;           // Process ID
  thread?: string;        // Thread identifier
  [key: string]: any;     // Additional context fields
}

Error Handling

The logger includes comprehensive error handling:

const logger = new Logger({
  transports: [transport1, transport2],
  onError: (error, transportName) => {
    console.error(`Transport ${transportName} failed:`, error);
    // Handle transport failures (e.g., fallback logging)
  }
});

Transport failures are isolated and don't affect other transports or application execution.

Performance

The logger is optimized for production use:

  • Minimal logging overhead (<1ms per log entry)
  • Asynchronous transport writing
  • Efficient message and metadata size limiting
  • Smart batching for network transports
  • Memory-conscious correlation tracking

Best Practices

  1. Use appropriate log levels - Reserve ERROR for actual errors
  2. Include context - Add relevant metadata to log entries
  3. Use correlation IDs - Track requests across services
  4. Sanitize sensitive data - Prevent PII leakage
  5. Configure transports appropriately - Different levels for different outputs
  6. Use child loggers - Create context-specific loggers
  7. Handle transport failures - Implement error callbacks
  8. Monitor performance - Use built-in timing functions

Integration Status

Logger Integration

  • Status: N/A - This is the core logging package
  • Usage: Other BernierLLC packages integrate with this logger for consistent logging across the ecosystem

Docs-Suite Integration

  • Status: Ready - Full TypeDoc documentation export support
  • Format: TypeDoc with comprehensive API documentation
  • Features: Automatic documentation generation, code examples, and type definitions

NeverHub Integration

  • Status: Integrated - Full service discovery and event bus support
  • Implementation:
    • Auto-detects NeverHub availability at runtime
    • Graceful degradation when NeverHub is not available
    • Enhanced transport capabilities when NeverHub is present
    • Service registration with logging capabilities
    • Event-driven log aggregation and forwarding
  • Capabilities:
    • Dynamic configuration from NeverHub config service
    • Enhanced observability with distributed tracing
    • Automatic service health reporting via logs
    • Cross-service log correlation

Development

# Install dependencies
npm install

# Build the package
npm run build

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Lint code
npm run lint

Migration: zod 4 (breaking)

This release moves the package to zod 4 (zod@^4.6.7) to fix the zod 3.x advisory. Exported config schemas (loggerConfigSchema, resolvedLoggerConfigSchema) are now zod 4 types. Consumers still on zod 3 must upgrade to zod 4 to compose these exports.

License

Copyright (c) 2025 Bernier LLC. All rights reserved.

版本与发布记录

@bernierllc/logger 共发布 14 个版本,首次发布于 2025/08/29;过去 12 个月发布了 13 个版本。

版本发布日期距今
2.0.0latest2026/11/053 天前
1.7.12026/07/143 个月前
1.7.02026/05/205 个月前
1.6.02026/05/205 个月前
1.4.32026/05/205 个月前
1.4.22026/05/195 个月前

关于 @bernierllc/logger 的常见问题

@bernierllc/logger 是什么?

Structured logging with multiple transports, log levels, and correlation tracking for BernierLLC ecosystem

如何安装 @bernierllc/logger?

在项目目录运行 npm install @bernierllc/logger,或使用 pnpm add / yarn add / bun add。

@bernierllc/logger 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bernierllc/logger 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bernierllc/logger 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bernierllc/logger 可以商用吗?

@bernierllc/logger 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bernierllc/logger 的下载量有多少?

根据 npm 官方下载统计,@bernierllc/logger 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,745 次,日均约 125 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bernierllc/logger 还在维护吗?

最新版本 2.0.0 发布于 2026/11/05(3 天前);至今共发布 14 个版本,其中过去 12 个月发布了 13 个;首次发布于 2025/08/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。