This logger is a simple wrap around winston. There are standard winston transports plus daily rotate file and Kafka transports. The Kafka transport is built around kafkajs.
NOTE: Transports are called Sinks in this module.
npm install winston-logger-kafka
First, you must supply a basic properties for the logger:
- module: A name of your service.
- component: A name of your service component.
- level: A log level.
The quickest way to create logger is to use getDefaultLogger method that returns a winston logger with console transport:
import * as Logger from "winston-logger-kafka";
const config: Logger.LoggerConfig = {
level: Logger.Levels.INFO
};
const logger = Logger.getDefaultLogger(config);
logger.info('Hello!')
Use this method to create logger with custom transports:
import * as Logger from "winston-logger-kafka";
const config: Logger.LoggerConfig = {
level: Logger.Levels.INFO
};
const sinks = [
new Logger.ConsoleSink,
new Logger.FileSink({
filename: './logs/%DATE%_log_file.log',
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: false,
maxSize: '20m',
frequency: '14d',
}),
new Logger.KafkaSink({
clientConfig: {brockers: ['localhost:9092']},
producerConfig: { allowAutoTopicCreation: false },
sinkTopic: 'test_topic',
})
]
const logger = Logger.getLogger(config, sinks);
logger.info('Hello!')
NOTE: Standard sink options are the same as options of corresponding winston transport.
You can also use kafka transport with pure winston:
import {KafkaTransport, KafkaTransportConfig} from "winston-logger-kafka";
const kafka_transport_conf = {
clientConfig: {brockers: ['localhost:9092']},
producerConfig: { allowAutoTopicCreation: false },
sinkTopic: 'test_topic',
}
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new KafkaTransport(kafka_transport_conf)
],
});
Get logger for a child class:
const childLoggerConf = {
level: Logger.Levels.INFO
};
const childLogger = Logger.getChildLogger(parentlogger, childLoggerConf);
MIT