README
:floppy_disk: bunyan-mssql-streamer
Exports logs from bunyan logger to MS SQL. See docs about bunyan streams for details.
This library wil be used in conjunction with restify's requestLogger and auditLogger to log Errors and Requests/Responses to MS SQL.
Install
> yarn add bunyan-mssql-streamer
Usage
There are 3 steps of setup
// 1. create bunyan logger as usual
const rootLogger = bunyan.createLogger({
name: "my-logger",
level: TRACE
});
// 2. create MS SQL Logger
const sqlLogger = new MsSqlLogger({
rootLogger: rootLogger,
mssql: {
server: "localhost",
database: "dbo",
user: "sa"
// etc [1]
}
});
// 3. add logger stream to root logger
rootLogger.addStream({
name: "mssql",
stream: sqlLogger.createMappingStream({
// ms sql table to store logs
table: "dbo.Errors",
// only records with this tag will be logged
tag: "error",
// tag will be seached in path "context.tag"
tagField: "$context.tag",
// map log records fields to sql table column
mapping: {
Exception: "$err.message",
StackTrace: "$err.stack",
Level: "ERROR",
MachineName: log => resolveHost(log.machineIp)
// etc [2]
}
}),
level: ERROR
});
You may add as many streams as you need. They will use single connection pool to DB.
[1]: see tediousjs/node-mssql for MS SQL configs
[2]: specify mapping as "table column name": "log record path spec"
.
If path spec begins with $
, it will be treated as JSON path.
You may also use a function as source field in mapping. That function will receive log record as first argument.
Dev workflows
Run tests
Fast tests
> yarn unittest
Slow tests
> yarn integtest
All tests
> yarn test
Release new version
> yarn release
This command will
- increment version in
package.json
- generate
CHANGELOG.md
- commit that chages with tag
See standard-version for details.
Publish to NPM
> npm publish