Giter Site home page Giter Site logo

winston-graylog2's Introduction

Build Status

winston-graylog2

A graylog2 transport for winston based on the node-graylog2 Library

Installation

Recently rewritten to use ES6 for better compatibility with Winston@3, so please use only with NodeJS >=6.4.0.

If you need to support older versions of Node, please use a version compatible with ^[email protected].

$ npm install winston
$ npm install winston-graylog2

Usage

var winston = require('winston');
var WinstonGraylog2 = require('winston-graylog2');

var options = { ...<your config options here>... };
winston.add(new WinstonGraylog2(options));

or

var winston
var WinstonGraylog2 = require('winston-graylog2');

var options = { ...<your config options here>... };
var logger = winston.createLogger({
  exitOnError: false,
  transports: [
    new WinstonGraylog2(options),
  ],
});

Options

  • name: Transport name
  • level: Level of messages this transport should log. (default: info)
  • silent: Boolean flag indicating whether to suppress output. (default: false)
  • handleExceptions: Boolean flag, whenever to handle uncaught exceptions. (default: false)
  • exceptionsLevel: Level of exceptions logs when handleExceptions is true. (default: error)
  • graylog:
    • servers; list of graylog2 servers
      • host: your server address (default: localhost)
      • port: your server port (default: 12201)
    • hostname: the name of this host (default: os.hostname())
    • facility: the facility for these log messages (default: "Node.js")
    • bufferSize: max UDP packet size, should never exceed the MTU of your system (default: 1400)
  • staticMeta: meta data to be always used by each logging message, for instance environment (development, staging, live)

Older versions of winston-graylog2 allowed the prelog and processMeta options for pre-processing logs and metadata attached to messages (such as stack traces). Winston 3 has implemented independent, custom formatters for handling these things, and they are no longer supported at the transport level. See the Winston docs for details on formatters.

example:

{
  name: 'Graylog',
  level: 'debug',
  silent: false,
  handleExceptions: false,
  graylog: {
    servers: [{host: 'localhost', port: 12201}, {host: 'remote.host', port: 12201}],
    hostname: 'myServer',
    facility: 'myAwesomeApp',
    bufferSize: 1400
  },
  staticMeta: {env: 'staging'}
}

Upgrading from earlier versions of winston-graylog2

Since [email protected] relies heavily on a very powerful set of formatters there are a few formatting actions that winston-graylog2 no longer needs to do. The two primary cases are the inclusion of a meta object, and converting errors so that the stack trace is included in the log message rather than just the name of the error.

[email protected] includes an excellent formatter for dealing with meta, conveniently named metadata. To use it, you can either grab it from the winston.format object, or use the one on logform.format. See the metadata formatter docs for more details.

For formatting Errors, [email protected] includes the errors formatter. To use it, you can either grab it from the winston.format object, or use the one on logform.format. If you want to include the stack trace in the log message, be sure to pass this formatter the { stack: true } option. See the errrors formatter docs for more details.

In order to get functionality identical to earlier versions of winston-graylog2, use both of these formatters together.

var winston = require('winston');
var { format } = winston;
var WinstonGraylog2 = require('winston-graylog2');

var options = { ...<your config options here>... };
var logger = winston.createLogger({
  exitOnError: false,
  format: format.combine(
    format.errors({ stack: true }),
    format.metadata(),
  ),
  transports: [
    new WinstonGraylog2(options)  ],
});

logger.info({ message: 'this is an info message', answer: 42 });
// or equivalently
logger.info('this is an info message', { answer: 42 });

logger.error({ message: new Error('FakeError'), somenumber: 96 });
// or equivalently
logger.error(new Error('FakeError'), { somenumber: 96 });

Log Levels

Supported log levels, as from node-graylog2, are the following

Winston Level Graylog2 level
emerg emergency
alert alert
crit critical
error error
warning warning
warn warning
notice notice
info info
debug debug

All other possible winston levels, or custom levels, will default to info

winston-graylog2's People

Contributors

abdul-hannan avatar bestmike007 avatar bharley avatar christianhaller3000 avatar cr1stal avatar dependabot[bot] avatar jeremy-j-ackso avatar krivchun avatar lautis avatar lijsselstein avatar myrlund avatar notslang avatar odino avatar oroce avatar philk avatar pierreinglebert avatar potatopankakes avatar qamil avatar sashman avatar schfkt avatar shidhincr avatar shintaronippon avatar smithclay avatar trufi avatar unlucio avatar urbanslug avatar victorb avatar wavded avatar yoniprv avatar yujinlim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

winston-graylog2's Issues

uncaughtExceptions

These errors aren't getting captured, not sure of the root cause but figured I'd report it.

{ date: 'Mon Jun 25 2012 16:51:22 GMT-0500 (CDT)',
  process: 
   { pid: 4125,
     uid: 0,
     gid: 0,
     cwd: '/',
     execPath: '/usr/local/bin/node',
     version: 'v0.6.19',
     argv: [ '/usr/local/bin/node', '/var/www/extra_dm/app.js' ],
     memoryUsage: { rss: 152215552, heapTotal: 89730432, heapUsed: 50744776 } },
  os: 
   { loadavg: [ 1.38134765625, 1.33349609375, 1.33349609375 ],
     uptime: 669829.87667782 },
  trace: 
   [ { column: 11,
       file: 'dgram.js',
       function: 'errnoException',
       line: 352,
       method: null,
       native: false },
     { column: 15,
       file: 'dgram.js',
       function: null,
       line: 123,
       method: null,
       native: false },
     { column: 12,
       file: 'dgram.js',
       function: 'lookup',
       line: 49,
       method: null,
       native: false },
     { column: 10,
       file: '[as lookup] (dgram.js',
       function: 'UDP.lookup4',
       line: 59,
       method: 'lookup4',
       native: false },
     { column: 16,
       file: 'dgram.js',
       function: 'Socket.bind',
       line: 120,
       method: 'bind',
       native: false },
     { column: 10,
       file: 'dgram.js',
       function: 'Socket._startReceiving',
       line: 308,
       method: '_startReceiving',
       native: false },
     { column: 8,
       file: 'dgram.js',
       function: 'Socket.send',
       line: 171,
       method: 'send',
       native: false },
     { column: 19,
       file: 'Object].log (/var/www/extra_dm/node_modules/winston-graylog2/lib/winston-graylog2.js',
       function: '[object',
       line: 72,
       method: null,
       native: false },
     { column: 17,
       file: '/var/www/extra_dm/node_modules/winston/lib/winston/logger.js',
       function: 'emit',
       line: 186,
       method: null,
       native: false },
     { column: 13,
       file: '/var/www/extra_dm/node_modules/winston/node_modules/async/lib/async.js',
       function: null,
       line: 86,
       method: null,
       native: false } ],
  stack: 
   [ 'Error: bind EMFILE',
     '    at errnoException (dgram.js:352:11)',
     '    at dgram.js:123:15',
     '    at lookup (dgram.js:49:12)',
     '    at UDP.lookup4 [as lookup] (dgram.js:59:10)',
     '    at Socket.bind (dgram.js:120:16)',
     '    at Socket._startReceiving (dgram.js:308:10)',
     '    at Socket.send (dgram.js:171:8)',
     '    at [object Object].log (/var/www/extra_dm/node_modules/winston-graylog2/lib/winston-graylog2.js:72:19)',
     '    at emit (/var/www/extra_dm/node_modules/winston/lib/winston/logger.js:186:17)',
     '    at /var/www/extra_dm/node_modules/winston/node_modules/async/lib/async.js:86:13' ] }

Upgrade to es6

It would be nice and forward looking if we took advantage of some of the es6 features and wrote this code in a more functional way such as using arrow functions, const etc as well as fixing the current jsdoc annotations?

Some of that has already been done by using const for imports in PR #54.

I'd like to become a maintainer

Hello guys. I noticed you're looking for a maintainer for this project. Me and my team -- we use it heavily in our projects. So it'd be great to maintain the lib, add more features, etc.

winston should be in peerDependencies

Like this:

package.json

  "dependencies": {
    "graylog2": "^0.1.3",
    "lodash": "^3.1.0",
  },
  "peerDependencies": {
    "winston": "^1.0.0"
 }

Otherwise the graylog2 might drag in and use an older version of the winston library.

Process hangs after logging (issue #41 has come back)

Hi,

  • winston 3.3.3
  • winston-graylog 2.1.2

I'm having the same issue as #41 where after writing to Graylog the process doesn't exit.

I can see the fix applied on PR #24 (or a modified version of it) is still there in the code.

Is there something else I need to do to make it call close() after writing?

Adjust to winston 3.x transport API

Hi,

since the release of Winston 3.0 there were some major changes of their API. Currently I`m getting this message with the use of graylog2 transport:

Kibana is a legacy winston transport. Consider upgrading:
    - Upgrade docs: https://github.com/winstonjs/winston/blob/master/UPGRADE-3.0.md

If I got this right the adjustment has to be done within winston-graylog2.

Thank you and best regards
Philipp

Fails Silently

When setting to a remote host, the module fails silently and exits the node process when trying to send a log message.

It should be clear in the Readme what protocols it supports

Currently it's not very clear what is supported and what is not. From the provided exemple, I can infer that the lib sends data on UDP, but is there a way to support TCP, TCP over TLS, HTTP?

Thank you for making that clear. And thank you for maintaining this project!

prevents program from exiting

Since this never calls close on the graylog2 UDP stream, this transport prevents the program from exiting when it's finished.

Here's some code I'm using to test:

winston = require('winston')
Graylog = require('winston-graylog2')

log = new(winston.Logger)(
  transports: [
    new (winston.transports.Console)(
      handleExceptions: true
    ),
    new(Graylog)(
      silent: false,
      handleExceptions: true,
      graylog:
        servers: [{host: '104.236.18.71', port: 12201}],
        bufferSize: 1400
    )
  ]
)

log.info('test')

Take over of library

Hi, here at namshi we are actively using graylog and winston. Since you're not maintaining this lib anymore me and @odino would like to take it over if for you is ok.

cheers
lucio

Consider using gelf-node

Instead of implementing the GELF protocol yourself, you might consider using gelf-node โ€“ย I've found this to be a well tested and good implementation of GELF ๐Ÿ‘

Deprecation message: graylog2 is a legacy winston transport

Description

The following message is being generated:

console.error node_modules/winston-transport/legacy.js:99
graylog2 is a legacy winston transport. Consider upgrading:
- Upgrade docs: https://github.com/winstonjs/winston/blob/master/UPGRADE-3.0.md

Looking at that code above: https://github.com/winstonjs/winston-transport/blob/master/legacy.js#L97-L103

Possible Solution

Upgrade the winston dependency.
Once the winston dependency is upgraded it looks like the deprecation is possibly related to the transport: https://github.com/winstonjs/winston/blob/master/UPGRADE-3.0.md#transports

https://github.com/namshi/winston-graylog2/blob/master/lib/winston-graylog2.js#L63
https://github.com/namshi/winston-graylog2/blob/master/lib/winston-graylog2.js#L64
https://github.com/namshi/winston-graylog2/blob/master/lib/winston-graylog2.js#L97

Environment Details

This is being seen when using:
node v10.8.0
nom 6.2.0
[email protected]
[email protected]
โ””โ”€โ”€ [email protected]

Standardize code by using a linter and prettifier

Use a tool such as:

  • eslint to have more standard code
  • prettier to have code that is more legible

This would make it easier for people who want to contribute and just easier to understand.

I suggest Google's eslint config because I feel it is more thorough thus leaving less room for bikeshedding.

Process hangs after logging

Hello,

I am testing your transport for Winston and for some reason it seems to be logging, but also causes my script to hang. My complete script is as follows:

var mysql   = require('mysql');
var winston = require('winston');

winston.add(require('winston-graylog2'),
    {
        name: "Graylog",
        level: "info",
        silent: false,
        handleExceptions: false,
        graylog: {
            servers: [
                {
                    host: "localhost",
                    port: 12201
                }
            ],
            hostname: "test server",
            bufferSize: 1400
        },
        staticMeta: {
            application: "Test Job",
            job: "test log job"
        }
    }
);

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'test'
});

connection.connect(function (err) {

    if (err) {
        winston.log('info', 'error connecting: ' + err.stack);
    }

    winston.log('info', 'connected as id ' + connection.threadId);
});

connection.query("SELECT COUNT(*) AS count FROM consumers", function (error, results, fields) {
    winston.log('info', 'Querying clients table');

    if (!error) {
        winston.log('info', 'Count: ' + results[0].count)
    }
    if (error) {
        winston.log('info', 'Query error ' + error);
    }
});

connection.end(function (error) {
    if (!error) {
        winston.log('info', 'Closing ' + connection.threadId);
    }
    if (error) {
        winston.log('info', 'Error closing connection: ' + error);
    }
});

If I omit the winston-graylog2 require, it works fine, and completes immediately.

Any ideas? Thanks in advance for any help that you can provide.

Logging staticMeta not working

I am trying to log to Graylog with some staticMeta. However, it only works when I add a single field in staticMeta. If I add more, nothing is logged to Graylog.

The below code works fine:

const myFormat = printf(({ level, message, label, timestamp }) => {
  if(!label){
    label = `MAIN PROCESS`;
  }
  return `[${dateFormat(timestamp, LOG_DATE_FORMAT)}] [${label}] ${level}: ${message}`;
});

const logger = createLogger({
  format: combine(
    timestamp(),
    errors({ stack: true }),
    myFormat
  ),
  transports: [
    new transports.Console(),
    new WinstonGraylog2({
      graylog: {
        servers: [
          {host: 'graylog.coast.ebuero.de', port: 12201}
        ],
        facility: 'GELF',
        bufferSize: 1400
			},
      staticMeta: {
        project: `${app.getName()}`
      }
    }),
    new winston.transports.File({ filename: path.join(os.homedir(),'cai_output.log') })
  ]
});

I have project in my gray log entries:
image

However,, if I try:

      staticMeta: {
        project: `${app.getName()}`,
        platform: `${process.platform}`
      }

or

      staticMeta: {
        project: `${app.getName()}`,
        platform: 'test'
      }

Nothing is logged, nothing at all.

TypeError when converting circular structure to JSON.

There should be a strategy for dealing with circular JSON structures so that we can get some kind of logging on them.

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'TLSSocket'
    |     property 'parser' -> object with constructor 'HTTPParser'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at graylog.log [as _log] (/app/node_modules/graylog2/graylog.js:170:31)
    at graylog.info (/app/node_modules/graylog2/graylog.js:106:17)
    at Immediate.<anonymous> (/app/node_modules/winston-graylog2/lib/winston-graylog2.js:79:50)
    at processImmediate (internal/timers.js:456:21)

graylog2 web interface issue under "hosts"

I need your help. I installed logstash agent on 4 servers and all are posting their logs onto graylog2 server where graylog2 web interface is also installed. But on the web interface under "hosts" I can see all the 4 servers name,But when I click on any particular host, it is not showing messages only for that server. it gives results randomly. Can U please tell how to configure this one to correctly display messages per host?Kindly reply me soon ..

Customize Message Length in winston graylog?

I'm using this package recently and I found a problem that only the first 100 characters of a log message are log to graylog because of this line:

this.graylog2[getMessageLevel(level)](msg.substring(0, 100), msg, meta);

and I have to drill down multiple list to see the entire message. I made a change to get rid of the substring function and then it works for me.

For design perspective, this is a dirty change that I shouldn't made for my project, and I didn't see that there is a way that we can through this in configuration level currently, so I'm asking if someone can wrap it up and give us the option so that we can pass in the message length as a parameter.

additional Fields

According to the GELF Payload Specification every field you send and prefix with an underscore () will be treated as an additional field._

Is there any chance to use this feature from within winston-graylog2 ?

log levels not working

According to your docs I should have access to the following log levels
emergency
alert
critical
error
warning
notice
info
debug

however it seems like only a couple of them are working.

if I try winston.warning('this is a test'); I get a fatal error. If I try winston.log('warning', 'this is a test'); it does not error, but it also does not log.

but if I do winston.debug('this is a test'); and winston.log('debug', 'this is a test'); it works fine.

What am I doing wrong?

I setup winston like this

var config = {
    name: 'Graylog',
    level: 'debug',
    silent: false,
    handleExceptions: true,
    prelog: function (msg) {
        return msg.trim();
    },
    graylog: {
        servers: [{host: '192.168.0.1', port: 12201}],
        hostname: 'server.lan',
        facility: 'Dialer List Definitions',
        bufferSize: 1400
    }
};

var winston = require('winston');
winston.add(require('winston-graylog2'), config);

log level not making it through

If you have colorize turned on for the winston logger you will not get log level set correctly

The reason is that the first character is an escape character to make error display in red.
https://github.com/namshi/winston-graylog2/blob/master/lib/winston-graylog2.js#L25

Not sure why you write that formatters can't be set on transport level see below how I did it and now it works!

Here is a sample what will work.

"use strict";

const os = require("os");
const winston = require("winston");
const { format } = winston;
const WinstonGraylog2 = require("winston-graylog2");
const {
  NODE_ENV,
  GRAYLOG_URL,
  GRAYLOG_PORT,
  GRAYLOG_FACILITY_NAME,
  LOG_LEVEL,
} = require("../config/env");

var grayLogOptions = {
  name: "Graylog",
  silent: false,
  level: LOG_LEVEL,
  handleExceptions: false,
  //showLevel: false,
  graylog: {
    servers: [
      {
        host: GRAYLOG_URL,
        port: GRAYLOG_PORT,
      },
    ],
    hostname: os.hostname(),
    facility: GRAYLOG_FACILITY_NAME,
    bufferSize: 1400,
  },
};

const winstonTransports = [];

if (NODE_ENV === "production") {
  winstonTransports.push(new WinstonGraylog2(grayLogOptions));
}

const logger = new winston.createLogger({
  transports: winstonTransports,
  format: format.combine(
    format.errors({ stack: true }), // Handle errors (was automagic in winston@2)
    // new
    format.metadata(),
  ),
});
logger.add(
  new winston.transports.Console({
    format: winston.format.combine(
      format.splat(), // Handle splat (was automagic in winston@2)
      format.timestamp(), // { timestamp: true }
      format.colorize(), // { colorize: true })
      format.simple(), // Default serialization in winston@2
    ),
  }),
);

module.exports = { logger };

Multiple levels going to single transport

Is there an option how to configure multiple levels to a single transport or the only way is to configure multiple transports with different levels?
I solved this by putting level: 'debug' in the configuration

Logging extra metadata after log

Hi there, I wanted to know if its possible to log extra metadata after log , something similar to winston rewriters but avoiding to show it on the console message

The reason I'm asking is because the SerilogGraylog library logs s stringLevel property basically doing toString to the level, and I wanted to make it compatible with this one

thanks!

Logging Objects or Errors is not working

'use strict';

const winston = require('winston');
const Graylog = require('winston-graylog2');

const logger = new winston.Logger({
	level: 'debug',
	transports: [
		new Graylog({
			level: 'debug',
			handleExceptions: true,
			graylog: {
				servers: [{ host: '10.42.221.139', port: 12201 }],
				facility: 'debug-test'
			}
		})
	]
});


logger.info('Text Test');
logger.debug({foo: 'bar'});
logger.error(new Error());
logger.error(new Error('Test Error'));

setTimeout(() => {process.exit(0)}, 1000);

When running the following script, the first test is the only one inserted in Graylog.

Graylog

Support for custom mapping of winston levels with graylog2 levels

Currently the level mapping is hardcoded. This causes level warn to be ignored. Also as winston supports custom levels its necessary to support custom level mapping in this transport (Correct me here if I am wrong). I guess we can take a mapping for the levels in the options object.
I will submit a PR as I have made the necessary edits. Once the PR is accepted I can add it to the doc.

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.