Skip to content

Commit

Permalink
Unit tests and virtually full coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Korving committed Jan 13, 2017
1 parent d01904f commit 7656ec9
Show file tree
Hide file tree
Showing 15 changed files with 680 additions and 280 deletions.
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]>
MIT License

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
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

* 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.
33 changes: 16 additions & 17 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Graylog = require('./graylog').graylog;
var Graylog = require('.').graylog;
var fs = require('fs');
var client;
var servers = [
{ 'host': '127.0.0.1', 'port': 12201 }
{ host: '127.0.0.1', port: 12201 }
];

function createClient() {
Expand All @@ -27,25 +27,24 @@ console.log('');

function log(str, label, i, n, cb) {
if (i === 0) {
createClient();
console.time(label + ' x' + n);
createClient();
}

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

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

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

cb();
});
cb();
});
}

} else {
if (i < n) {
client.log('test', str);
process.nextTick(log, str, label, i + 1, n, cb);
}
Expand All @@ -64,9 +63,9 @@ function testBigAndRandom(cb) {
}

function end() {
console.log('Complete.');
console.log('Please check your logging service and verify that insertion was successful.');
console.log('');
console.log('Insertion complete. Please check', 'http://' + servers[0].host + ':3000', 'and verify that insertion was successfull');
console.log('');
}

testSmall(function () {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/Graylog');
Loading

0 comments on commit 7656ec9

Please sign in to comment.