@compas/stdlib
Advanced tools
Comparing version 0.0.182 to 0.0.183
export { uuid } from "./src/datatypes.js"; | ||
export { AppError } from "./src/error.js"; | ||
export { newLogger } from "./src/logger/logger.js"; | ||
export type Either<T, E> = import("./types/advanced-types").Either<T, E>; | ||
@@ -50,2 +49,7 @@ export type EitherN<T, E> = import("./types/advanced-types").EitherN<T, E>; | ||
} from "./src/utils.js"; | ||
export { | ||
newLogger, | ||
extendGlobalLoggerContext, | ||
setGlobalLoggerOptions, | ||
} from "./src/logger/logger.js"; | ||
export { bytesToHumanReadable, printProcessMemoryUsage } from "./src/memory.js"; | ||
@@ -52,0 +56,0 @@ export { |
@@ -78,3 +78,7 @@ /// <reference path="./types/advanced-types.d.ts"> | ||
export { newLogger } from "./src/logger/logger.js"; | ||
export { | ||
newLogger, | ||
extendGlobalLoggerContext, | ||
setGlobalLoggerOptions, | ||
} from "./src/logger/logger.js"; | ||
@@ -81,0 +85,0 @@ export { bytesToHumanReadable, printProcessMemoryUsage } from "./src/memory.js"; |
{ | ||
"name": "@compas/stdlib", | ||
"version": "0.0.182", | ||
"version": "0.0.183", | ||
"description": "All kinds of utility functions", | ||
@@ -17,6 +17,6 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@types/node": "17.0.9", | ||
"@types/node": "17.0.10", | ||
"dotenv": "14.2.0", | ||
"lodash.merge": "4.6.2", | ||
"pino": "7.6.3" | ||
"pino": "7.6.4" | ||
}, | ||
@@ -23,0 +23,0 @@ "maintainers": [ |
/** | ||
* @typedef {import("../../types/advanced-types").LoggerOptions} LoggerOptions | ||
* Shallow assigns properties of the provided context to the global context. | ||
* These properties can still be overwritten by providing the 'ctx' property when | ||
* creating a new Logger. | ||
* | ||
* @param {Record<string, any>} context | ||
*/ | ||
export function extendGlobalLoggerContext(context: Record<string, any>): void; | ||
/** | ||
* Set various logger options, affecting loggers created after calling this function. | ||
* | ||
* @param {GlobalLoggerOptions} options | ||
*/ | ||
export function setGlobalLoggerOptions({ | ||
pinoTransport, | ||
pinoDestination, | ||
}: GlobalLoggerOptions): void; | ||
/** | ||
* Create a new logger instance | ||
@@ -17,2 +31,20 @@ * | ||
import("../../types/advanced-types").LoggerOptions<any>; | ||
export type GlobalLoggerOptions = { | ||
/** | ||
* Set pino | ||
* transport, only used if the printer is 'ndjson'. | ||
*/ | ||
pinoTransport?: | ||
| pino.TransportSingleOptions<unknown> | ||
| pino.TransportMultiOptions<unknown> | ||
| pino.TransportPipelineOptions<unknown> | ||
| undefined; | ||
/** | ||
* Set Pino | ||
* destination, only used if the printer is 'ndjson' and no 'pinoTransport' is | ||
* provided. Use `pino.destination()` create the destination or provide a stream. | ||
*/ | ||
pinoDestination?: import("pino").DestinationStream | undefined; | ||
}; | ||
import { pino } from "pino"; | ||
//# sourceMappingURL=logger.d.ts.map |
@@ -1,7 +0,26 @@ | ||
import pino from "pino"; | ||
import { pino } from "pino"; | ||
import { environment, isProduction } from "../env.js"; | ||
import { isNil } from "../lodash.js"; | ||
import { merge } from "../lodash.js"; | ||
import { noop } from "../utils.js"; | ||
import { writeGithubActions, writePretty } from "./writer.js"; | ||
/** | ||
* @typedef {import("../../types/advanced-types").LoggerOptions} LoggerOptions | ||
*/ | ||
/** | ||
* @typedef {object} GlobalLoggerOptions | ||
* @property {Parameters<import("pino").transport<any>>[0]} [pinoTransport] Set pino | ||
* transport, only used if the printer is 'ndjson'. | ||
* @property {import("pino").DestinationStream} [pinoDestination] Set Pino | ||
* destination, only used if the printer is 'ndjson' and no 'pinoTransport' is | ||
* provided. Use `pino.destination()` create the destination or provide a stream. | ||
*/ | ||
/** | ||
* @type {{pretty: (writePretty|((stream: NodeJS.WritableStream, level: string, | ||
* timestamp: Date, context: string, message: any) => void)|*), "github-actions": | ||
* (writeGithubActions|((stream: NodeJS.WritableStream, level: string, timestamp: Date, | ||
* context: string, message: any) => void)|*)}} | ||
*/ | ||
const writersLookup = { | ||
@@ -12,7 +31,62 @@ pretty: writePretty, | ||
let globalPino = pino( | ||
{ | ||
formatters: { | ||
level: (label) => ({ level: label }), | ||
bindings: () => ({}), | ||
}, | ||
serializers: {}, | ||
base: {}, | ||
}, | ||
pino.destination(1), | ||
); | ||
/** @type {object} */ | ||
const globalContext = {}; | ||
/** | ||
* @typedef {import("../../types/advanced-types").LoggerOptions} LoggerOptions | ||
* Shallow assigns properties of the provided context to the global context. | ||
* These properties can still be overwritten by providing the 'ctx' property when | ||
* creating a new Logger. | ||
* | ||
* @param {Record<string, any>} context | ||
*/ | ||
export function extendGlobalLoggerContext(context) { | ||
Object.assign(globalContext, context); | ||
} | ||
/** | ||
* Set various logger options, affecting loggers created after calling this function. | ||
* | ||
* @param {GlobalLoggerOptions} options | ||
*/ | ||
export function setGlobalLoggerOptions({ pinoTransport, pinoDestination }) { | ||
if (pinoTransport) { | ||
globalPino = pino({ | ||
formatters: { | ||
level: (label) => ({ level: label }), | ||
bindings: () => ({}), | ||
}, | ||
serializers: {}, | ||
base: {}, | ||
// @ts-ignore | ||
transport: pinoTransport, | ||
}); | ||
} else if (pinoDestination) { | ||
globalPino = pino( | ||
{ | ||
formatters: { | ||
level: (label) => ({ level: label }), | ||
bindings: () => ({}), | ||
}, | ||
serializers: {}, | ||
base: {}, | ||
}, | ||
pinoDestination, | ||
); | ||
} | ||
} | ||
/** | ||
* Create a new logger instance | ||
@@ -26,3 +100,2 @@ * | ||
export function newLogger(options) { | ||
const app = environment.APP_NAME; | ||
const stream = options?.stream ?? process.stdout; | ||
@@ -38,24 +111,6 @@ | ||
const context = options?.ctx ?? {}; | ||
if (isProduction() && app) { | ||
context.application = app; | ||
} | ||
const context = merge({}, globalContext, options?.ctx ?? {}); | ||
if (printer === "ndjson") { | ||
const pinoLogger = pino( | ||
{ | ||
formatters: { | ||
level: (label) => ({ level: label }), | ||
bindings: () => ({}), | ||
}, | ||
serializers: {}, | ||
base: {}, | ||
transport: options?.pinoOptions?.transport, | ||
}, | ||
// @ts-ignore | ||
options?.pinoOptions?.destination ?? | ||
(isNil(options?.pinoOptions?.transport) | ||
? pino.destination(1) | ||
: undefined), | ||
).child({ context }); | ||
const pinoLogger = globalPino.child({ context }); | ||
@@ -62,0 +117,0 @@ return { |
@@ -8,5 +8,6 @@ import { lstatSync, realpathSync } from "fs"; | ||
import dotenv from "dotenv"; | ||
import { refreshEnvironmentCache } from "./env.js"; | ||
import { environment, isProduction, refreshEnvironmentCache } from "./env.js"; | ||
import { AppError } from "./error.js"; | ||
import { isNil } from "./lodash.js"; | ||
import { extendGlobalLoggerContext } from "./logger/logger.js"; | ||
@@ -84,2 +85,8 @@ /** | ||
if (isProduction() && environment.APP_NAME) { | ||
extendGlobalLoggerContext({ | ||
application: environment.APP_NAME, | ||
}); | ||
} | ||
const logger = newLogger({ | ||
@@ -86,0 +93,0 @@ ctx: { type: name }, |
import { RandomUUIDOptions } from "crypto"; | ||
import Pino from "pino"; | ||
import { SonicBoom } from "sonic-boom"; | ||
@@ -130,21 +128,2 @@ import { AppError } from "../src/error.js"; | ||
/** | ||
* Supported Pino options if the 'ndjson' logger is used | ||
*/ | ||
pinoOptions?: | ||
| { | ||
transport?: | ||
| Pino.TransportSingleOptions | ||
| Pino.TransportMultiOptions | ||
| Pino.TransportPipelineOptions; | ||
destination?: | ||
| string | ||
| number | ||
| Pino.DestinationObjectOptions | ||
| Pino.DestinationStream | ||
| NodeJS.WritableStream | ||
| SonicBoom; | ||
} | ||
| undefined; | ||
/** | ||
* Context that should be logged in all log lines. e.g | ||
@@ -151,0 +130,0 @@ * a common request id. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78508
2634
+ Added@types/node@17.0.10(transitive)
+ Addedpino@7.6.4(transitive)
- Removed@types/node@17.0.9(transitive)
- Removedpino@7.6.3(transitive)
Updated@types/node@17.0.10
Updatedpino@7.6.4