Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ovhemert committed Jul 19, 2018
0 parents commit 0b2d97e
Show file tree
Hide file tree
Showing 13 changed files with 5,461 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
39 changes: 39 additions & 0 deletions .gitignore
@@ -0,0 +1,39 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

.vscode
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: node_js
sudo: false
node_js:
- '8'
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Osmond van Hemert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
@@ -0,0 +1,41 @@
# pino-papertrail

This module provides a "transport" for [pino][pino] that forwards
messages to the [papertrail][papertrail] log service through an UDPv4 socket. The module can echo the received logs or work silently.

You should install `pino-papertrail` globally for ease of use:

```bash
$ npm install --production -g pino-papertrail
```

## Usage

Given an application `foo` that logs via [pino][pino], and a system that
collects logs on port UDP `12345` on address `bar.papertrailapp.com`, you would use `pino-papertrail`
like so:

```bash
$ node foo | pino-papertrail --host bar.papertrailapp.com --port 12345 --appname foo
```

## Options

You can pass the following options via cli arguments:

| Description | Short command | Full command |
| ------------- | ------------- |-------------|
| Display help information | `-h` | `--help` |
| Display version | `-v` | `--version` |
| Application name (default: pino) | `-a` | `--appname` |
| Echo messages to the console (default: true) | `-e` | `--echo` |
| Papertrail destination address (default: localhost) | `-H` | `--host` |
| Papertrail destination port (default: 1234) | `-p` | `--port` |


## License

Licensed under [MIT](./LICENSE).

[pino]: https://www.npmjs.com/package/pino
[papertrail]: https://papertrailapp.com
56 changes: 56 additions & 0 deletions index.js
@@ -0,0 +1,56 @@
#! /usr/bin/env node
'use strict'

const fs = require('fs')
const path = require('path')

const minimist = require('minimist')
const pump = require('pump')

const pinoPapertrail = require('./lib/pino-papertrail')
const pkg = require('./package.json')

const options = {
alias: {
version: 'v',
help: 'h',
echo: 'e',
host: 'H',
port: 'p',
appname: 'a'
},
default: {
appname: 'pino',
echo: true,
host: 'localhost',
port: '1234'
}
}

const argv = minimist(process.argv.slice(2), options)

if (argv.help) {
console.log(fs.readFileSync(path.join(__dirname, './usage.txt'), 'utf8'))
process.exit(0)
}
if (argv.version) {
console.log(`${pkg.name} v${pkg.version}`)
process.exit(0)
}

const parseJson = pinoPapertrail.parseJson()
const toSyslog = pinoPapertrail.toSyslog(argv)
const papertrail = pinoPapertrail.toPapertrail(argv)

function shutdown () {
try {
papertrail.close()
} catch (e) {
process.exit()
}
}

process.on('SIGINT', function () { shutdown() })
process.on('SIGTERM', function () { shutdown() })

pump(process.stdin, parseJson, toSyslog, papertrail)
60 changes: 60 additions & 0 deletions lib/pino-papertrail.js
@@ -0,0 +1,60 @@
'use strict'

const dgram = require('dgram')
const stream = require('stream')

const fastJsonParse = require('fast-json-parse')
const split2 = require('split2')
const glossy = require('glossy')
const through2 = require('through2')

const PINO_LEVELS = { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60 }
const SYSLOG_SEVERITIES = { emergency: 0, alert: 1, critical: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 }

function _jsonParser (str) {
const result = fastJsonParse(str)
if (result.err) return
return result.value
}

function _levelToSeverity (level) {
if (level === PINO_LEVELS.trace || level === PINO_LEVELS.debug) { return SYSLOG_SEVERITIES.debug }
if (level === PINO_LEVELS.info) { return SYSLOG_SEVERITIES.info }
if (level === PINO_LEVELS.warn) { return SYSLOG_SEVERITIES.warning }
if (level === PINO_LEVELS.error) { return SYSLOG_SEVERITIES.error }
return SYSLOG_SEVERITIES.critical
}

module.exports.parseJson = function () {
return split2(_jsonParser)
}

module.exports.toPapertrail = function (options) {
const socket = dgram.createSocket('udp4')
const writableStream = new stream.Writable({
close () { socket.close() },
write (data, encoding, callback) {
socket.send(data, 0, data.length, options.port, options.host, function (err) {
if (options.echo === true) { console.log(data.toString()) }
callback(err)
})
}
})
return writableStream
}

module.exports.toSyslog = function (options) {
const syslogProducer = new glossy.Produce()
return through2.obj(function transport (data, enc, cb) {
const msg = syslogProducer.produce({
facility: options.facility || 'user',
severity: _levelToSeverity(data.level),
host: data.hostname,
appName: options.appname,
pid: data.pid,
date: data.time,
message: JSON.stringify(data)
})
cb(null, msg)
})
}

0 comments on commit 0b2d97e

Please sign in to comment.