@tsmw-utils/logging
v1.1.11
Published
A TypeScript logging utility package supporting correlation, multiple loggers, and Elasticsearch integration.
Readme
@tsmw-utils/logging
A TypeScript logging utility package supporting correlation, multiple loggers, and Elasticsearch integration.
Source code: https://github.com/CompilerOfDust/tsmw-utils-logging
Installation
bun install @tsmw-utils/logging
# or
npm install @tsmw-utils/loggingUsage
1. Import the Loggers and Correlation Factory
import { ElasticLogger, HttpConnection, CorrelationFactory } from '@tsmw-utils/logging';
import { LogProider } from '@tsmw-utils/logging/src/logging/log-provider';
import { ConsoleLogger } from '@tsmw-utils/logging/src/logging/loggers/console-logger';2. Create a Correlation
const correlationFactory = new CorrelationFactory('MyService');
const correlation = correlationFactory.createCorrelation({
context: 'UserSignup',
id: 'abc123'
});3. Set Up Loggers
Console Logger
const consoleLogger = new ConsoleLogger();Elasticsearch Logger
const elasticLogger = new ElasticLogger({
node: {
url: new URL('http://localhost:9200'),
ssl: { rejectUnauthorized: false }
},
Connection: HttpConnection
});4. Combine Loggers (Optional)
const logger = new LogProider()
.withLogger(consoleLogger)
.withLogger(elasticLogger)
.build();5. Log Messages
await logger.info(correlation, 'User signed up successfully');
await logger.error(correlation, 'Failed to sign up user', new Error('Signup error'));API
CorrelationFactory: Creates correlation objects for log context.ILogger: Logger interface.ConsoleLogger: Logs to the console.ElasticLogger: Logs to Elasticsearch.LogProider: Aggregates multiple loggers.
Example
import { ElasticLogger, HttpConnection, CorrelationFactory } from '@tsmw-utils/logging';
import { LogProider } from '@tsmw-utils/logging/src/logging/log-provider';
import { ConsoleLogger } from '@tsmw-utils/logging/src/logging/loggers/console-logger';
const correlationFactory = new CorrelationFactory('ExampleService');
const correlation = correlationFactory.createCorrelation({ context: 'Example', id: '1' });
const logger = new LogProider()
.withLogger(new ConsoleLogger())
.withLogger(new ElasticLogger({
node: { url: new URL('http://localhost:9200'), ssl: { rejectUnauthorized: false } },
Connection: HttpConnection
}))
.build();
await logger.info(correlation, 'This is an info log');
await logger.error(correlation, 'This is an error log', new Error('Example error'));For more details, see the source files:
