@lbu/insight
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -8,3 +8,5 @@ import { newLogger } from "./src/logger.js"; | ||
/** | ||
* Standard log instance | ||
* Standard log instance. | ||
* Comes with a depth of 4, prevents printing deeply nested objects | ||
* | ||
* @type {Logger} | ||
@@ -11,0 +13,0 @@ */ |
{ | ||
"name": "@lbu/insight", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"description": "Simple logger in NDJSON format", | ||
@@ -32,3 +32,3 @@ "main": "./index.js", | ||
}, | ||
"gitHead": "87bbba853bb050f7897c50ea291233297e2366e1" | ||
"gitHead": "b81b7a87aa31416c815ff2105990c7455de4846b" | ||
} |
@@ -9,15 +9,8 @@ # @lbu/insight | ||
## Why | ||
Here at [lightbase](https://lightbase.nl) we had a constantly growing | ||
boilerplate for new projects. To facilitate our needs more and to stop copying | ||
and pasting things around this project was born. This project is for now | ||
tailored at smaller & monolithic projects. | ||
## Features | ||
- Minimal API project boilerplate | ||
- Minimal project boilerplate | ||
- Script runner, can watch & reload almost anything (via nodemon) | ||
- Flexible code generators supporting routers, validators, api clients, mocks | ||
and more in the future. | ||
- Flexible code generators supporting routers, validators, api clients, mocks, | ||
CRUD queries and more in the future. | ||
- Opinionated structured logging | ||
@@ -28,5 +21,88 @@ - Common Koa middleware wrapped in a single function | ||
## Requirements | ||
- Node.js >= 14 | ||
- Yarn 1.x.x | ||
## Why | ||
Here at [lightbase](https://lightbase.nl) we had a constantly growing | ||
boilerplate for new projects. To facilitate our needs more and to stop copying | ||
and pasting things around this project was born. This project is for now | ||
tailored at monolithic projects. | ||
## Features breakdown | ||
**@lbu/cli**: | ||
- Run user scripts (in watch mode) | ||
- Run the linter or tests | ||
- A LBU based boilerplate | ||
**@lbu/lint-config**: | ||
- All necessary ESLint and Prettier dependencies | ||
- Default configuration for ESLint and Prettier | ||
**@lbu/insight**: | ||
- A structured logger | ||
- Writing newline delimited JSON in production | ||
- Pretty printing for development | ||
- Various utilities to get insight in the running process | ||
- Parser to process production logs in an external process | ||
**@lbu/stdlib**: | ||
- Various lodash inspired utilities (isNil, isPlainObject, ...) | ||
- Wrappers for child_process execution and spawning | ||
- Basic templating system | ||
- A `mainFn` wrapper that reads `.env` and calls the provided function if the | ||
file is the process entrypoint | ||
- Replacements for CommonJS `__dirname` and `__filename` | ||
**@lbu/server**: | ||
- Wrapper around Koa instance creation | ||
- 404 en error handling | ||
- Handle CORS | ||
- Send file helper | ||
- Re-exports koa-session and koa-compose | ||
**@lbu/store**: | ||
- Wrapper around the Minio S3 client | ||
- Wrapper around Postgres connection | ||
- Utilities for providing temporary databases in a test environment | ||
- Postgres migrations | ||
- Postgres and S3 combined for file storage | ||
- Caching files from S3 in memory or on local disk | ||
- Postgres powered JobQueue implementation | ||
- Supports priority, scheduling, multiple async workers | ||
- koa-session compatible SessionStore backed by Postgres | ||
**@lbu/code-gen**: | ||
- Code generators for the following: | ||
- router, with wildcard and path parameter support | ||
- validators, with pre- and postValidate hooks | ||
- queries, CRUD postgres queries | ||
- Axios based api client | ||
- Typescript or JSDoc types | ||
- Generated mocks | ||
- An extendable set of types: | ||
- boolean, number, string; | ||
- object, array, any; | ||
- date, uuid; | ||
- generic, anyOf, reference; | ||
- Remote structure loader | ||
- OpenAPI to LBU converter | ||
- Generate stubs (types and structure only) so packages can also use LBU | ||
- router structure | ||
- api client structure (Typescript supported) | ||
- and of course the normal types generator | ||
## Docs | ||
See [/docs](/docs/README.md) | ||
See [/docs](/docs/README.md) for all available APIs and various guides. | ||
@@ -33,0 +109,0 @@ ## Development |
import { writeNDJSON, writePretty } from "./writer.js"; | ||
/** | ||
* @typedef {object} LoggerOptions | ||
* @property {boolean} [pretty=false] | ||
* @property {number} [depth=3] | ||
* @property {WriteStream} [stream=process.stdout] | ||
* @property {*|{type: string}} [ctx] | ||
* @name LoggerOptions | ||
* | ||
* @typedef {object} | ||
* @property {boolean} [pretty=false] Use the pretty formatter instead of the NDJSON formatter | ||
* @property {number} [depth=3] Max-depth printed | ||
* @property {WriteStream} [stream=process.stdout] The stream to write the logs to | ||
* @property {*|{type: string}} [ctx] Context that should be logged in all log lines. e.g | ||
* a common request id. | ||
*/ | ||
/** | ||
* @typedef {function(arg: *): undefined} LogFn | ||
* @name LogFn | ||
* | ||
* Prints the provided argument | ||
* | ||
* @typedef {function(arg: *): undefined} | ||
*/ | ||
/** | ||
* @typedef {object} Logger | ||
* @property {function(): boolean} isProduction | ||
* @property {LogFn} info | ||
* @property {LogFn} error | ||
* @name Logger | ||
* | ||
* The logger only has two severities: | ||
* - info | ||
* - error | ||
* | ||
* Either a log line is innocent enough and only provides debug information if needed, or | ||
* someone should be paged because something goes wrong. For example handled 500 errors | ||
* don't need any ones attention, but unhandled 500 errors do. | ||
* @see {@lbu/server#logMiddleware} | ||
* | ||
* @typedef {object} | ||
* @property {function(): boolean} isProduction Check if this logger is using the pretty | ||
* printer or NDJSON printer | ||
* @property {LogFn} info Info log | ||
* @property {LogFn} error Error log | ||
*/ | ||
@@ -24,4 +43,5 @@ | ||
* Create a new logger | ||
* | ||
* @param {LoggerOptions} [options] | ||
* @return {Logger} | ||
* @returns {Logger} | ||
*/ | ||
@@ -55,5 +75,7 @@ export function newLogger(options) { | ||
* Bind a context object to the logger functions and returns a new Logger | ||
* The context is always printed | ||
* | ||
* @param {Logger} logger | ||
* @param {*} ctx | ||
* @return {Logger} | ||
* @param {object|{type: string}} ctx | ||
* @returns {Logger} | ||
*/ | ||
@@ -69,2 +91,8 @@ export function bindLoggerContext(logger, ctx) { | ||
/** | ||
* Wrap provided writer function to be used in the Logger | ||
* | ||
* @param fn | ||
* @returns {log} | ||
*/ | ||
function wrapWriter(fn) { | ||
@@ -71,0 +99,0 @@ return function log(stream, depth, level, context, message) { |
@@ -6,2 +6,3 @@ const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"]; | ||
* Support up to a pebibyte | ||
* | ||
* @param {number} [bytes] | ||
@@ -33,3 +34,4 @@ * @returns {string} | ||
* https://nodejs.org/dist/latest-v13.x/docs/api/process.html#process_process_memoryusage | ||
* @param logger | ||
* | ||
* @param {Logger} logger | ||
*/ | ||
@@ -36,0 +38,0 @@ export function printProcessMemoryUsage(logger) { |
@@ -6,3 +6,7 @@ import pump from "pump"; | ||
/** | ||
* @typedef {object} LogParserContext | ||
* @name LogParserContext | ||
* | ||
* The LogParserContext enables you too analyze logs produced by this Logger | ||
* | ||
* @typedef {object} | ||
* @property {(function(data: object): undefined)} [jsonProcessor] | ||
@@ -15,4 +19,5 @@ * @property {(function(data: string): undefined)} [textProcessor] | ||
* Create a new parser context | ||
* | ||
* @param {ReadStream} stream | ||
* @return {LogParserContext} | ||
* @returns {LogParserContext} | ||
*/ | ||
@@ -30,4 +35,5 @@ export function newLogParserContext(stream) { | ||
* textProcessor with the value. The original value is written to the returned stream | ||
* | ||
* @param {LogParserContext} lpc | ||
* @return {ReadStream} | ||
* @returns {ReadStream} | ||
*/ | ||
@@ -54,2 +60,3 @@ export function executeLogParser(lpc) { | ||
* Internal try to parse as json and execute jsonProcessor, else execute textProcessor | ||
* | ||
* @param {LogParserContext} lpc | ||
@@ -56,0 +63,0 @@ * @param {string} line |
import { inspect } from "util"; | ||
/** | ||
* @param stream | ||
* @param depth | ||
* @param level | ||
* @param timestamp | ||
* @param context | ||
* @param message | ||
*/ | ||
export function writeNDJSON(stream, depth, level, timestamp, context, message) { | ||
@@ -15,2 +23,10 @@ stream.write( | ||
/** | ||
* @param stream | ||
* @param depth | ||
* @param level | ||
* @param timestamp | ||
* @param context | ||
* @param message | ||
*/ | ||
export function writePretty(stream, depth, level, timestamp, context, message) { | ||
@@ -17,0 +33,0 @@ stream.write(formatDate(timestamp)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15166
389
110