0.1.1 • Published 6 years ago

@usvc/logger-request v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

@usvc/logger-request

Morgan based logger for usage in a microservices architecture.

The output logs will include the following properties:

KeyDescription
levelThis will always be "access"
methodThe method of the HTTP request
urlThe requested URL
statusThe status code of the request
contentLengthThe content length
responseTimeMsThe response time in milliseconds
httpVersionThe HTTP version
referrerThe referrer (if applicable)
remoteHostnameThe remote hostname (if found)
remoteAddressThe remote IP address
serverHostnameThe server hostname as specified in the createLogger method
timeISO timestamp of the request
userAgentUser agent of the request

Scope

  • Creates an Express compatible request logger middleware
  • Allow for token extension
  • Allow for specification of a custom logger and log level

Installation

npm i @usvc/logger-request;
# OR
yarn add @usvc/logger-request;

Usage

// es5:
const {createLogger} = require('@usvc/logger-request');
// es6:
import {createLogger} from '@usvc/logger-request';

Basic

const express = require('express');
// require as ^
const app = express();
app.use(createLogger());
app.listen(...);

Full Configuration

const express = require('express');
// require as ^
const app = express();
app.use(createLogger({

}));
app.listen(...);

API Documentaiton

createLogger(:options)

Creates the request logger middleware and returns an Express compatible middleware that you can app.use(...).

Parameters

KeyDefaults ToDescription
additionalTokens[]Additional tokenizers. Each item in the array should be of the form {id: 'some-property', fn: (req, res) => (/*your code here*/)}.
hostnameos.hostname() || process.env.HOSTNAME || 'unknown'The host name to use for the server in the logs
loggerconsoleThe logger to be used.
levelinfoThe level of the logger to be used.

getZipkinTokenizers()

This function returns an array of tokenizers that you can use in the additionalTokenizers option in createLogger().

These tokenizers depend on there being a .context property in the Request object passed by Express. This is automatically done if you are using the Zipkin tracer as defined in @usvc/tracer.

Using the returned tokenizers will result in the following additional keys in the log:

KeyDescription
traceIdThe trace ID
spanIdThe span ID
parentSpanIdThe parent span ID
sampledIndication whether the request was sampled or not

Examples

Usage with ES5

Goto: ./example/es5

Or run: npm run eg:es5

Usage with ES6

Goto: ./example/es6

Or run: npm run eg:es6

Usage with Winston

Goto: ./example/winston

Or run: npm run eg:winston

Development

See the development instructions on the main repository's README.

License

This package is licensed under the MIT license.

View the license at LICENSE.

Changelog

WIP

Contributors

NameEmailWebsiteAbout Me
Josephdev-at-joeir-dot-nethttps://github.com/zephinzerN/A

Cheers