Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete re-implementation #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

node_modules
npm-debug.log
.nyc_output
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node-graylog (C) 2011 Egor Egorov <[email protected]>
Copyright (c) 2017 Wizcorp
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed this, since there's literally nothing left of the original implementation.


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ install:
test:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> make test
make: `test' is up to date.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happens because you have a test folder

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah lol, nice catch. I always just run npm test, so didn't run into that. Will fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the Makefile tbh.

npm test

bench:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest to allow the destination server as an argument (so to allow testing against an arbitrary Graylog/Logstash server).

node ./bench.js

# target: lint, Lints every JavaScript file in the project that are staged to be comitted.
lint:
./scripts/lint-staged.sh
Expand Down
129 changes: 67 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,105 @@
# node-graylog2
[![NPM version](http://img.shields.io/npm/v/graylog2.svg?style=flat-square)](https://www.npmjs.org/package/graylog2) [![NPM license](http://img.shields.io/npm/l/graylog2.svg?style=flat-square)](https://www.npmjs.org/package/graylog2)
# graylog2

Graylog2 client library for Node.js, based on node-graylog. This
has been heavily modified to the point where there is not much left
of the original; however, this library should still be compatible
with the old one, except for configuration and the GLOBAL function setup
(some optional arguments in logging calls are not supported; they will be
logged as additional data).
[![NPM version](http://img.shields.io/npm/v/graylog2.svg?style=flat-square)](https://www.npmjs.org/package/graylog2)
[![NPM license](http://img.shields.io/npm/l/graylog2.svg?style=flat-square)](https://www.npmjs.org/package/graylog2)

** New: ** Chunked [GELF](https://github.com/Graylog2/graylog2-docs/wiki/GELF)
is now supported.
Graylog2 client library for Node.js

## Synopsis
## Installation

```sh
npm install graylog2 --save
```

## What is Graylog?

Graylog is popular logging software. You can get it at http://www.graylog2.org. Incidentally, since this package
uses Graylog's [GELF](http://docs.graylog.org/en/latest/pages/gelf.html) protocol, you can also use this with other
logging software that has GELF support.

## Usage

### Available functions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Available events should also be documented.


* graylog.emergency
* graylog.alert
* graylog.critical
* graylog.error
* graylog.warning
* graylog.notice
* graylog.info
* graylog.debug
* graylog.emergency(short, full, fields, timestamp)
* graylog.alert(short, full, fields, timestamp)
* graylog.critical(short, full, fields, timestamp)
* graylog.error(short, full, fields, timestamp)
* graylog.warning(short, full, fields, timestamp)
* graylog.notice(short, full, fields, timestamp)
* graylog.info(short, full, fields, timestamp)
* graylog.debug(short, full, fields, timestamp)

Arguments:

- short (string): A short message to log.
- full (string, optional): Additional details.
- fields (object, optional): An object of key/value pairs to help with filtering.
- timestamp (integer, optional): A custom timestamp (milliseconds).

### Code snippets

```javascript
var graylog2 = require("graylog2");
var logger = new graylog2.graylog({
```js
var Graylog = require('graylog2');
var logger = new Graylog({
servers: [
{ 'host': 127.0.0.1, port: 12201 },
{ 'host': 127.0.0.2, port: 12201 }
{ host: '127.0.0.1', port: 12201 },
{ host: '127.0.0.2', port: 12201 }
],
hostname: 'server.name', // the name of this host
// (optional, default: os.hostname())
facility: 'Node.js', // the facility for these log messages
// (optional, default: "Node.js")
bufferSize: 1350 // max UDP packet size, should never exceed the
// MTU of your system (optional, default: 1400)
hostname: 'server.name', // the name of this host (optional, default: os.hostname())
facility: 'Node.js', // the facility for these log messages (optional, default: "Node.js")
bufferSize: 1350 // max UDP packet size, should not exceed the MTU of your network (optional, default: 1400)
});

logger.on('error', function (error) {
console.error('Error while trying to write to graylog2:', error);
console.error('Error while trying to write to Graylog2:', error);
});

logger.on('warning', function (error) {
console.error('Non-fatal error while trying to write to Graylog2:', error);
});
```

Short message:

```javascript
logger.log("What we've got here is...failure to communicate");
```js
logger.debug("What we've got here is...failure to communicate");
```

Long message:

```javascript
logger.log("What we've got here is...failure to communicate", "Some men you just
can't reach. So you get what we had here last week, which is the way he wants
it... well, he gets it. I don't like it any more than you men.");
```js
var short = "What we've got here is...failure to communicate";
var long = "Some men you just can't reach. So you get what we had here last week, " +
"which is the way he wants it... well, he gets it. I don't like it any more than you men.";

logger.debug(short, long);
```

Short with additional data:

```javascript
logger.log("What we've got here is...failure to communicate", { cool: 'beans' });
```js
logger.debug("What we've got here is...failure to communicate", { cool: 'beans' });
```

Long with additional data:

```javascript
logger.log("What we've got here is...failure to communicate", "Some men you just
can't reach. So you get what we had here last week, which is the way he wants
it... well, he gets it. I don't like it any more than you men.",
{
cool: "beans"
}
);
```
```js
var short = "What we've got here is...failure to communicate";
var long = "Some men you just can't reach. So you get what we had here last week, " +
"which is the way he wants it... well, he gets it. I don't like it any more than you men.";

Flush all log messages and close down:
```javascript
logger.close(function(){
console.log('All done - cookie now?');
process.exit();
});
logger.debug(short, long, { cool: 'beans' });
```

## Example

See `test.js`.
Send all pending log messages and close the socket:

## What is graylog2 after all?

It's a miracle. Get it at http://www.graylog2.org/
```js
logger.close(function () {
console.log('All done!');
});
```

## Installation
### More examples

npm install graylog2
See the files in the `test` folder.
75 changes: 75 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
var Graylog = require('.').graylog;
var fs = require('fs');
var client;
var servers = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest to make this accept CLI arguments, so that you can test against an existing Graylog setup

{ host: '127.0.0.1', port: 12201 }
];

function createClient() {
client = new Graylog({
servers: servers,
facility: 'node-graylog benchmark'
});

client.on('error', function (error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is required - wouldn't on('error') trigger a throw anyhow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

throw error;
});
}


var i = 0;
var count = 20000;
var small = 'h'.repeat(1000);
var big = 'h'.repeat(25000);
var bigRandom = require('crypto').randomBytes(20000).toString('base64');

console.log('');

function log(str, label, i, n, cb) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are str, i and n? Variables could use a bit of renaming here.

if (i === 0) {
createClient();
console.time(label + ' x' + n);

client.on('drain', function () {
console.timeEnd(label + ' x' + n);

console.log('Sent:', client.sent, '- Compressed:', client.compressed);
console.log('');

if (client.sent !== n) {
throw new Error('Should have sent: ' + n);
}

cb();
});
}

if (i < n) {
client.log('test', str);
process.nextTick(log, str, label, i + 1, n, cb);
}
}

function testSmall(cb) {
log(small, 'small', 0, 10000, cb);
}

function testBig(cb) {
log(big, 'big', 0, 5000, cb);
}

function testBigAndRandom(cb) {
log(bigRandom, 'bigAndRandom', 0, 2000, cb);
}

function end() {
console.log('Complete.');
console.log('Please check your logging service and verify that insertion was successful.');
console.log('');
}

testSmall(function () {
testBig(function () {
testBigAndRandom(end);
});
});
Loading