GithubHelp home page GithubHelp logo

hapi-madero's Introduction

hapi-madero

Build Status Dependencies Status

NPM

A HapiJS plugin for writing logs to files

HapiJS plugin for writing logs to files. It creates 5 different files, depending on the log type ('info', 'error', 'warning', 'request', 'plugin'). I find it useful for services like splunk which can read logs from the server directly.

Installing

npm install hapi-madero

Usage

Here is a snippet of a basic setup:

// Deps =========================================
const Hapi = require('hapi');
const Madero = require('hapi-madero');

// Server =======================================
const server = new Hapi.Server({ debug: false });

// Server Connection ============================
server.connection({ port: 3000, host: 'localhost' });

// Madero Options ===============================
const maderoOptions = { path: './logs' };

// Adding Madero Plugin =========================
server.register({ register: Madero, options: maderoOptions }, err => {

	if (err) {
        console.log('[ERROR]:', 'Failed loading plugin: hapi-madero,', err);
    }

    server.route({
        method: 'GET',
        path: '/',
        handler: (request, reply) => {

            request.log(['test'], { message: 'test', foo: 'bar' });
            return reply('ok')
        }
    });

    // Logging all responses
    server.on('response', (request) => {

        const response = request.response;
        let statusCode = response.statusCode;
        let message = 'Auto request log';
        let entry = { statusCode, message };
        let error;

        if (response.isBoom) {
            statusCode = response.output.payload.statusCode;
            message = response.message;
            error = response.output.payload;
            entry = { statusCode, message, error };

            // Logs error
            return request.log(['error', 'boom'], entry);
        }

        // Log every request
        return request.log(['info'], entry);
    });

    server.start(err => {

        if (err) {
            server.plugins['hapi-madero'].console(err, 'error');
        }
        server.log(['info', 'app', 'start'], {
            message: `Hapi-Madero server started`,
            port: server.info.port,
            protocol: server.info.protocol,
            uri: server.info.uri,
            address: server.info.address
        });
    });
});

Plugin Options

path - String

Madero needs to know where to save the files, this will tell madero where the log files will be saved. I.E.: ./logs will create a directory in the project root called 'logs'. Defaults to path.resolve(path.dirname(require.main.filename), './logs') (which will create the directory where the main file is run)

stopTimeoutMsec - Number

Overrides the timeout in millisecond before forcefully terminating a connection. Defaults to 15000 (15 seconds)

silent - Boolean

If you do not want to see every log in the console. Defaults to false

signals - Boolean

Whether you want madero to handle SIGTERM or SIGINT. Defaults to true

exceptions - Boolean

Whether you want madero to handle uncaughtException or unhandledRejection. Defaults to true

timestampKey - String

In case you need to specify the timestamp key for the events, you can change it here. Defaults to @timestamp

unixStamp - Boolean

By default, each event timestamp is set to a ISO string: YYYY-MM-DDTHH:mm:ss.sssZ, changing this to true will change to a unix stamp of 13 numbers.

Plugin Methods

write (options, [callback])

options - required - Object

Recieves a the following:

  • async - Boolean - Wether to write to file async or not
  • request - Object - The request object
  • entry - Object - The entry that will be written to file. This expects the following:
    • message - required - String - Entry message
    • tags - required - Array - Array of strings used to identify the event. Tags are used instead of log levels and provide a much more expressive mechanism for describing and filtering events.
    • error - Object - An error object
    • data - Object - Any additional data to be saved with the entry
callback - function

Called once it has finshed writing to file

console (data, [type, [callback]])

data - Object

This will be the object that you want to log to console

type - String

Can be one of: error, info, warn. Defaults to info

callback - function

Called once it has finshed logging to console

License

This project is licensed under the MIT License - see the LICENSE.md file for details

hapi-madero's People

Contributors

pipa avatar

Watchers

 avatar  avatar

hapi-madero's Issues

Boom doesn't add error details

Boom doesn't add error details in the request log.

E.G.: boom.badGateway(err.message, err) will result in a request log with error: {} in its entry

it should create a request log but should have the error field with data

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.