0.4.42 • Published 10 months ago

@mcf/logger v0.4.42

Weekly downloads
247
License
MIT
Repository
github
Last release
10 months ago

npm version

@mcf/logger

Exposes a logger based on Winston. It includes formats and plugins for integration with the Morgan request logging library, and Zipkin request distributed tracing library.

Scope

  • Basic logging
  • Allows for formatter extensions
  • Allows for transport extensions
  • Console transport creator
  • File transport creator
  • FluentD transport creator
  • Fluent transport security configuration
  • Fluent transport ID tagger

Installation

Install it via npm or yarn:

npm i @mcf/logger
# or
yarn add @mcf/logger

Usage

Requiring

import {createConsoleTransport, createFluentTransport, createLogger} from '@mcf/logger';
// or
const {createConsoleTransport, createFluentTransport, createLogger} = require('@mcf/logger');

Basic

By default, the logger already logs to the console and no configuration is needed. This should suffice for basic apps.

const basicLogger = createLogger();
basicLogger.silly('a silly hi');
basicLogger.debug('a debug hi');
basicLogger.http('a http hi');
basicLogger.info('a info hi');
basicLogger.warn('a warn hi');
basicLogger.error('a error hi');

FluentD

We use fluent for our centralised logs collector. To initialise the transport, create the logger as such:

const fluentLogger = createLogger({
  additionalTransports: [
    createFluentTransport({
      host: 'localhost',
      port: 44224,
      timeout: 2.0,
      requireAckResponse: false,
    }),
  ],
  // optional message transformation, depends on your elasticsearch setup
  formatters: [
    (info) => {
      const messageIsObject = typeof info.message === 'object';
      return {
        ...info,
        message: messageIsObject ? 'meta' : info.message,
        meta: messageIsObject ? info.message : undefined,
      };
    },
  ],
});

fluentLogger.info('hello world!');

The following fluent.conf should get you up and running:

<source>
  @type forward
  bind 0.0.0.0
  port 24224
</source>

<match **.*>
  @type stdout
</match>

See the usage example for more examples of creating plaintext/encrypted loggers.

Documentation

The library exposes the following methods:

MethodDescription
.createLoggerCreates the logger object which can be used
.createFluentTransportCreates a FluentD compatible transport
.createConsoleTransportCreates a Console transport
.createFileTransportCreates a File transport
.createMorganStreamCreates an object consumable by Morgan

.createLogger(:options)

This function accepts a configuration object as the parameter where the keys are documented as follows:

KeyDescriptionDefault
additionalTransportsWinston transports to be added on (no overriding of the default Console transport)[]
formattersWinston formatters created via winston.format(...)()[]
levelDefault level (ENUM { "error", "warn", "info", "http", "debug", "silly" })"silly"
namespaceGive a name to your logger)give-me-a-name
silentSet to true to turn off loggingfalse
transportsWinston transports[winston.transports.Console()]

.createFluentTransport(:options)

This function accepts a configuration object as the parameter where the keys are documented as follows:

KeyDescriptionDefault
hostFluentD service hostname"localhost"
portFluentD service port24224
requireAckResponseSpecifies whether we should connect via TCP (true) or UDP (false)false
securityA security object with .clientHostname : string and .sharedKey : string propertiesundefined
tagTag for the logsprocess.env.HOSTNAME || os.hostname() || 'unknown'
tlsSpecifies if we should use TLStrue
tlsOptionsSpecifies an options object for the TLS connection which has a .ca propertyundefined
timeoutTimeout for a push3.0

This is referenced from the fluent-logger library.

.createConsoleTransport(:options)

This function accepts a configuration object as the parameter where the keys are documented as follows:

KeyDescriptionDefault
formatWinston formatters created via winston.format(...)()Combination of colorize and custom printf displaying namespace, timestamp, level and message

.createFileTransport(:options)

This function accepts a configuration object as the parameter where the keys are documented as follows:

KeyDescriptionDefault
formatWinston formatters created via winston.format(...)()Custom printf displaying namespace, timestamp, level and message
filenameFilename to outputundefined

.createMorganStream(:options)

This function returns an object that can be used by Morgan to specify a write stream.

The properties for the :options object are:

KeyDescriptionDefault
logLevelThe desire level of logs to use for request logs by Morgan"silly"
loggerThe logger instance to be usedundefined

Examples

Confirm all dependencies have been installed:

yarn;

To run the usage example, set up FluentD first:

yarn svc:fluent;

In another terminal, run the usage application which will pipe to the FluentD instance:

yarn demo-usage;

To stop FluentD, run:

yarn svc:fluent:stop;

Changelog

0.2.x

0.2.1

Added in stream creator for Morgan (see .createMorganStream)

0.2.0

Added more:

  • Fluent transport security configuration (.security)
  • Fluent transport ID tagger (.tag)
  • Fluent transport TLS support (.tls and .tlsOptions)

0.1.x

0.1.0

Initial release with:

  • Basic logging
  • Allows for formatter extensions
  • Allows for transport extensions
  • FluentD transport creator
  • Console transport creator
0.4.42

10 months ago

0.4.40

11 months ago

0.4.39

1 year ago

0.4.38

1 year ago

0.4.37

1 year ago

0.4.36

1 year ago

0.4.35

2 years ago

0.4.33

2 years ago

0.4.34

2 years ago

0.4.32

2 years ago

0.4.31

3 years ago

0.4.30

3 years ago

0.4.29

3 years ago

0.4.28

3 years ago

0.4.25

3 years ago

0.4.24

3 years ago

0.4.22

4 years ago

0.4.21

4 years ago

0.4.20

4 years ago

0.4.19

4 years ago

0.4.18

4 years ago

0.4.17

4 years ago

0.4.16

4 years ago

0.4.15

5 years ago

0.4.14

5 years ago

0.4.13

5 years ago

0.4.12

5 years ago

0.4.11

5 years ago

0.4.10

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago