0.1.2 • Published 1 year ago

winston-logger-kafka v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Simple winston-based logger with Kafka transport

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.

Installation

npm install winston-logger-kafka

Usage

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.

getDefaultLogger() method

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!')

getLogger() method

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.

Use standalone kafka 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)
    ],
});

getChildLogger method

Get logger for a child class:

const childLoggerConf = {
    level: Logger.Levels.INFO
};

const childLogger = Logger.getChildLogger(parentlogger, childLoggerConf);

LICENSE

MIT

AUTHOR: Dmitry Amanov
0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.0.13

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago