Skip to content

Commit

Permalink
🤘
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Feb 21, 2018
0 parents commit be0ae46
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

# [*.md]
# trim_trailing_whitespace = false
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Lock files
shrinkwrap.yaml
package-lock.json
yarn.lock

# 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

# 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 directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# 0x
.__browserify_string_empty.js
profile-*
*.flamegraph

# tap --cov
.nyc_output/

# JetBrains IntelliJ IDEA
.idea/
*.iml

# VS Code
.vscode/
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js

node_js:
- "9"
- "8"
- "6"

# before_install:
# - curl -L https://unpkg.com/@pnpm/self-installer | node
# install:
# - pnpm install

script:
- npm run lint-ci
- npm run test-ci

notifications:
email:
on_success: never
on_failure: always
92 changes: 92 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# pino-std-serializers

This module provides a set of standard object serializers for the
[Pino](https://getpino.io) logger.

## Serializers

### `exports.err(error)`
Serializes an `Error` like object. Returns an object:

```js
{
type: 'string', // The name of the object's constructor.
message: 'string', // The supplied error message.
stack: 'string' // The stack when the error was generated.
}
```

Any other extra properties, e.g. `statusCode`, that have been attached to the
object will also be present on the serialized object.

### `exports.mapHttpResponse(response)`
Used internally by Pino for general response logging. Returns an object:

```js
{
res: {}
}
```

Where `res` is the `response` as serialized by the standard response serializer.

### `exports.mapHttpRequest(request)`
Used internall by Pino for general request logging. Returns an object:

```js
{
req: {}
}
```

Where `req` is the `request` as serialized by the standard request serializer.

### `exports.req(request)`
The default `request` serializer. Returns and object:

```js
{
id: 'string', // Default is an empty string. Attach a synchronous function
// to the input `request` that returns an identifier to have
// the value filled.
method: 'string',
url: 'string',
headers: Object,
remoteAddress: 'string',
remotePort: Number,
raw: Object // Non-enumerable, i.e. will not be in the output, original
// request object. This is available for subsequent serializers
// to use.
}
```

### `exports.res(response)`
The default `response` serializer. Returns an object:

```js
{
statusCode: Number,
header: Array, // The list of headers to be sent in the response.
raw: Object // Non-enumerable, i.e. will not be in the output, original
// response object. This is available for subsequent serializers
// to use.
}
```

### `exports.wrapRequestSerializer(customSerializer)`
A utility method for wrapping the default request serializer. This allows
custom serializers to work with the already serialized object.

The `customSerializer` accepts one parameter: the newly serialized request
object.

### `exports.wrapResponseSerializer(customSerializer)`
A utility method for wrapping the default response serializer. This allows
custom serializers to work with the already serialized object.

The `customSerializer` accepts one parameter: the newly serialized response
object.

## License

MIT License
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use srict'

var reqSerializers = require('./lib/req')
var resSerializers = require('./lib/res')

module.exports = {
err: require('./lib/err'),
mapHttpRequest: reqSerializers.mapHttpRequest,
mapHttpResponse: resSerializers.mapHttpResponse,
req: reqSerializers.reqSerializer,
res: resSerializers.resSerializer,

wrapRequestSerializer: function wrapRequestSerializer (customSerializer) {
if (customSerializer === reqSerializers.reqSerializer) return customSerializer
return function wrappedReqSerializer (req) {
return customSerializer(reqSerializers.reqSerializer(req))
}
},

wrapResponseSerializer: function wrapResponseSerializer (customSerializer) {
if (customSerializer === resSerializers.resSerializer) return customSerializer
return function wrappedResSerializer (res) {
return customSerializer(resSerializers.resSerializer(res))
}
}
}
17 changes: 17 additions & 0 deletions lib/err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

module.exports = errSerializer

function errSerializer (err) {
var obj = {
type: err.constructor.name,
message: err.message,
stack: err.stack
}
for (var key in err) {
if (obj[key] === undefined) {
obj[key] = err[key]
}
}
return obj
}
72 changes: 72 additions & 0 deletions lib/req.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'

module.exports = {
mapHttpRequest,
reqSerializer
}

var rawSymbol = Symbol('pino-raw-req-ref')
var pinoReqProto = Object.create({}, {
id: {
enumerable: true,
writable: true,
value: ''
},
method: {
enumerable: true,
writable: true,
value: ''
},
url: {
enumerable: true,
writable: true,
value: ''
},
headers: {
enumerable: true,
writable: true,
value: {}
},
remoteAddress: {
enumerable: true,
writable: true,
value: ''
},
remotePort: {
enumerable: true,
writable: true,
value: ''
},
raw: {
enumerable: false,
get: function () {
return this[rawSymbol]
},
set: function (val) {
this[rawSymbol] = val
}
}
})
Object.defineProperty(pinoReqProto, rawSymbol, {
writable: true,
value: {}
})

function reqSerializer (req) {
var connection = req.connection
const _req = Object.create(pinoReqProto)
_req.id = typeof req.id === 'function' ? req.id() : req.id
_req.method = req.method
_req.url = req.url
_req.headers = req.headers
_req.remoteAddress = connection && connection.remoteAddress
_req.remotePort = connection && connection.remotePort
_req.raw = req
return _req
}

function mapHttpRequest (req) {
return {
req: reqSerializer(req)
}
}
47 changes: 47 additions & 0 deletions lib/res.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

module.exports = {
mapHttpResponse,
resSerializer
}

var rawSymbol = Symbol('pino-raw-res-ref')
var pinoResProto = Object.create({}, {
statusCode: {
enumerable: true,
writable: true,
value: 0
},
header: {
enumerable: true,
writable: true,
value: ''
},
raw: {
enumerable: false,
get: function () {
return this[rawSymbol]
},
set: function (val) {
this[rawSymbol] = val
}
}
})
Object.defineProperty(pinoResProto, rawSymbol, {
writable: true,
value: {}
})

function resSerializer (res) {
const _res = Object.create(pinoResProto)
_res.statusCode = res.statusCode
_res.header = res._header
_res.raw = res
return _res
}

function mapHttpResponse (res) {
return {
res: resSerializer(res)
}
}
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "pino-std-serializers",
"version": "1.0.0",
"description": "A collection of standard object serializers for Pino",
"main": "index.js",
"scripts": {
"lint": "standard | snazzy",
"lint-ci": "standard",
"test": "tap --no-cov 'test/**/*.test.js'",
"test-ci": "tap --cov --coverage-report=text 'test/**/*.test.js'"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/pinojs/pino-std-serializers.git"
},
"keywords": [
"pino",
"logging"
],
"author": "James Sumners <james.sumners@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/pinojs/pino-std-serializers/issues"
},
"homepage": "https://github.com/pinojs/pino-std-serializers#readme",
"precommit": [
"lint",
"test"
],
"devDependencies": {
"pre-commit": "^1.2.2",
"snazzy": "^7.0.0",
"standard": "^11.0.0",
"tap": "^11.1.1"
}
}
Loading

0 comments on commit be0ae46

Please sign in to comment.