@hyperflake/logger
v1.1.9
Published
A simple Node.js logger module for logging messages with flexible & customizable transports.
Downloads
297
Readme
@hyperflake/logger
A simple Node.js logger module for logging messages with flexible & customizable transports.
Installation
npm install @hyperflake/logger
Usage
import { Logger, transports } from '@hyperflake/logger';
// Create a logger instance with a transport configuration
const logger = new Logger({
transports: [
new transports.Console(),
new transports.MongoDB({
uri: 'mongodb://localhost/logging',
collectionName: 'app-logs',
}),
],
});
// Log an info message
logger.log({
message: "This is an info message",
});
// Log an error message
logger.error({
message: "This is an error message",
});
// Log a warning message
logger.warn({
message: "This is a warning message",
});
API
Logger
Constructor
new Logger<TLoggerData>(options: LoggerOptions);
Creates a new logger instance with the specified transport configuration.
- options: Logger options.
- transports: Array of transports to be used for logging.
Methods
- log(entry: LogEntry): void: Logs an info message.
- error(entry: LogEntry): void: Logs an error message.
- warn(entry: LogEntry): void: Logs a warning message.
Transports
Console Transport
A transport that logs messages to the console.
new transports.Console(options?: ConsoleTransportOptions);
- options: Options for the console transport.
- formatterFn:
(data: LogEntry<TLoggerData>) => string
Function that formats log data into a string before outputting it to the console. (Optional).
- formatterFn:
MongoDB Transport
A transport that logs messages to MongoDB.
new transports.MongoDB(options: MongoDBTransportOptions);
- options: Options for the MongoDB transport.
- uri: MongoDB connection URI.
- dbName: Name of the MongoDB database. Defaults to DB name based on connection URI if not provided. (Optional)
- collectionName: Name of the collection to store logs.
- capped: Whether to create the log collection as capped or not. Defaults to true. (Optional)
- cappedSize: Size of logs capped collection in bytes. Defaults to 10,000,000. (Optional)
- cappedMax: Size of logs capped collection in number of documents. (Optional)
Example
import { Logger, transports } from '@hyperflake/logger'
// Create a logger instance with console transport & additional data type
const consoleLogger = new Logger<{ userId: number }>({
transports: [
new transports.Console(),
],
});
// Log a message with additional data
consoleLogger.log({
message: "This is a log message",
userId: 100001,
});
// Create a logger instance with custom formatterFn
const customizedConsoleLogger = new transports.Console({
formatterFn: (data) => {
return `[${data.timestamp.toString()}] ${data.level} - ${data.message}`;;
}
});
// This is the default formatterFn in case no formatterFn is passed
const defaultFormatterFn = (data: LogData<TLoggerData>) => {
return JSON.stringify(data);
}
// Create a logger instance with MongoDB transport
const mongoLogger = new Logger<{ token: string }>({
transports: [
new transports.MongoDB({
uri: 'mongodb://localhost/logging',
collectionName: 'app-logs',
}),
],
});
// Log a message with additional data
mongoLogger.log({
message: "This is a log message",
token: '0wq4kWuo63DJaCmLC',
});
// Create a mongodb logger instance with custom options
const customizedMongoLogger = new Logger<{ token: string }>({
transports: [
new transports.MongoDB({
uri: 'mongodb://localhost',
dbName: 'logging',
collectionName: 'app-logs',
capped: true, // Defaults to true,
cappedSize: 1024 * 1024,
cappedMax: 100000
}),
],
});