Skip to content

Commit

Permalink
Merge pull request datalust#17 from stefan-jonasson/master
Browse files Browse the repository at this point in the history
Support for out-of-process logging
  • Loading branch information
larenelg authored May 27, 2020
2 parents c65b5b9 + c206080 commit bcd7fdc
Show file tree
Hide file tree
Showing 10 changed files with 1,425 additions and 147 deletions.
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 2,
"singleQuote": true,
"useTabs": false,
"trailingComma": "none",
"printWidth": 160
}
55 changes: 41 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# bunyan-seq [![Build status](https://ci.appveyor.com/api/projects/status/mrcbbrd33prih7bb?svg=true)](https://ci.appveyor.com/project/datalust/bunyan-seq) [![NPM](https://img.shields.io/npm/v/bunyan-seq.svg)](https://www.npmjs.com/package/bunyan-seq)


A Bunyan stream to send events to [Seq](https://getseq.net). Tested with Node.js versions 4.2.2 and up.

### Usage
Expand All @@ -12,28 +11,56 @@ let bunyan = require('bunyan');
let seq = require('bunyan-seq');

var log = bunyan.createLogger({
name: 'myapp',
streams: [
{
stream: process.stdout,
level: 'warn',
},
seq.createStream({
serverUrl: 'http://localhost:5341',
level: 'info'
})
]
name: 'myapp',
streams: [
{
stream: process.stdout,
level: 'warn'
},
seq.createStream({
serverUrl: 'http://localhost:5341',
level: 'info'
})
]
});

log.info('Hi!');
log.warn({lang: 'fr'}, 'Au revoir');
log.warn({ lang: 'fr' }, 'Au revoir');
```

You can specify property names as tokens in the log message to control how the event is rendered in Seq:

```js
// Seq will render this as 'Hi, Alice!'
log.info({user: 'Alice'}, 'Hi, {user}!');
log.info({ user: 'Alice' }, 'Hi, {user}!');
```

### Out-of-process (transport) usage

First, install `bunyan-seq` as a global tool:

```shell
npm install -g bunyan-seq
```

Then, pipe the output of your bunyan-enabled app to it:

```shell
node your-app.js | bunyan-seq --serverUrl http://localhost:5341 --apiKey 1234567890
```

`bunyan-seq` accepts the following parameters:

- `serverUrl` - this is the base URL of your Seq server; if omitted, the default value of `http://localhost:5341` will be used
- `apiKey` - your Seq API key, if one is required; the default does not send an API key
- `logOtherAs` - log other output (not formatted through bunyan) to seq at this loglevel. Useful to capture messages if the node process crashes or smilar.

#### Capturing other output

To enable capture of output not formatted through bunyan use the `logOtherAs` parameter. It's possible to use different settings for STDOUT/STDERR like this, when using bash:

```shell
node your-app.js 2> >(bunyan-seq --logOtherAs Error --serverUrl http://localhost:5341 --apiKey 1234567890) > >(bunyan-seq --logOtherAs Information --serverUrl http://localhost:5341 --apiKey 1234567890)
```

Read the [complete documentation](https://docs.getseq.net/docs/using-nodejs).
51 changes: 51 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node

const program = require('commander');
const split2 = require('split2');

const pkg = require('./package.json');
const SeqStream = require('./seq_stream');
const StringStream = require('./seq_string_transform');

function main() {
program
.version(pkg.version)
.option('-s, --serverUrl <serverUrl>', 'Seq server instance')
.option('-k, --apiKey <apiKey>', 'Seq API key')
.option(
'-o, --logOtherAs <Verbose|Debug|Information|Warning|Error|Fatal>',
'Log other output (not formatted through bunyan) to seq at this loglevel. Useful to capture messages if the node process crashes or smilar.',
(string) => {
if (['Verbose', 'Debug', 'Information', 'Warning', 'Error', 'Fatal'].includes(string)) {
return string;
}
console.error(`Warning, skipping option "logOtherAs": Invalid value supplied: "${string}"`);
return undefined;
}
)
.action(({ serverUrl, apiKey, logOtherAs }) => {
try {
const seqStream = new SeqStream({ serverUrl, apiKey });

process.stdin.pipe(split2()).pipe(new StringStream({ logOtherAs })).pipe(seqStream);

const handler = (err, name) => {
seqStream.end(() => {
process.exit(0);
});
};

process.on('SIGINT', () => handler(null, 'SIGINT'));
process.on('SIGQUIT', () => handler(null, 'SIGQUIT'));
process.on('SIGTERM', () => handler(null, 'SIGTERM'));
process.on('SIGLOST', () => handler(null, 'SIGLOST'));
process.on('SIGABRT', () => handler(null, 'SIGABRT'));
} catch (error) {
console.error(error);
}
});

program.parse(process.argv);
}

main();
27 changes: 27 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Writable } from 'stream';

declare namespace BunyanSeq {
interface SeqConfig {
serverUrl?: string;
apiKey?: string;
maxBatchingTime?: number;
eventSizeLimit?: number;
batchSizeLimit?: number;
name?: string;
level: config.level;
reemitErrorEvents?: boolean;
onError?: (e: Error) => void;
}

interface SeqStreamConfig {
name?: string;
level?: string;
type: 'raw';
stream: Writable;
reemitErrorEvents?: boolean;
}

function createStream(config: BunyanSeq.SeqConfig): SeqStreamConfig;
}

export = BunyanSeq;
Loading

0 comments on commit bcd7fdc

Please sign in to comment.