Packages@accelint/logger
Overview
Structured logger built on LogLayer with callsite tracking, environment detection, and flexible transports
Usage
import { getLogger } from '@accelint/logger';
// Create a singleton logger instance
const logger = getLogger({
enabled: true,
env: process.env.NODE_ENV as 'production' | 'development',
level: 'info',
prefix: '[MyApp]',
});
logger.info('Application started');
logger.withMetadata({ userId: 123 }).warn('Token expiring soon');Exports
Functions
- getLogger - Returns a singleton LogLayer logger instance
- bootstrap - Initializes a new LogLayer instance with default plugins
Constants
- DEBUG - Log level constant for debug messages
- ERROR - Log level constant for error messages
- FATAL - Log level constant for fatal error messages
- INFO - Log level constant for informational messages
- TRACE - Log level constant for trace messages
- WARN - Log level constant for warning messages
Plugins
- callsitePlugin - Tracks and injects source code location into log data
- environmentPlugin - Injects environment context (server/browser) into log data
Transports
- prettyTransport - Pretty-printed console transport
- structuredTransport - Structured JSON log transport
Types
LogLevel- The log level type:'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'LoggerOptions- Configuration options for the loggerCallsitePluginOptions- Options for callsite tracking pluginEnvironmentPluginOptions- Options for environment detection plugin
Examples
Example: Basic logger setup
import { getLogger, INFO } from '@accelint/logger';
const logger = getLogger({
enabled: true,
level: INFO,
env: 'production',
});
logger.info('Server started', { port: 3000 });Example: Development logger with pretty output
import { getLogger, DEBUG } from '@accelint/logger';
const logger = getLogger({
enabled: true,
level: DEBUG,
env: 'development',
pretty: true,
prefix: '[Dev]',
});
logger.debug('Cache miss', { key: 'user:123' });Example: Production logger with structured JSON
import { getLogger, WARN } from '@accelint/logger';
const logger = getLogger({
enabled: true,
level: WARN,
env: 'production',
pretty: false, // Outputs JSON
});
logger.error('Payment failed', { orderId: 'ord_123', reason: 'insufficient_funds' });Example: Custom plugins
import { getLogger } from '@accelint/logger';
import type { LogLayerPlugin } from '@loglayer/shared';
const timingPlugin: LogLayerPlugin = {
id: 'timing',
onBeforeDataOut({ data }) {
return {
...data,
timestamp: Date.now(),
};
},
};
const logger = getLogger({
enabled: true,
plugins: [timingPlugin],
});Example: Custom transports
import { getLogger, prettyTransport, structuredTransport } from '@accelint/logger';
// Multiple transports: pretty console + structured JSON
const logger = getLogger({
enabled: true,
transports: [
prettyTransport({ level: 'debug' }),
structuredTransport({ level: 'info' }),
],
});
logger.info('Logged to both transports');Example: Log groups
import { getLogger } from '@accelint/logger';
const logger = getLogger({
enabled: true,
groups: {
database: { enabled: true },
network: { enabled: false }, // Network logs disabled
},
});
logger.withGroup('database').info('Query executed');
logger.withGroup('network').info('This will not log');Good to know:
getLoggerreturns a singleton. The first call initializes the logger with the provided options; subsequent calls return the same instance and ignore new options. Usebootstrapdirectly if you need multiple independent logger instances.
Related
- LogLayer Documentation - Upstream library documentation
- getLogger - Singleton logger factory
- bootstrap - Direct logger initialization