@rarible/logger
Advanced tools
Comparing version 0.10.0-alpha.10 to 0.10.0-alpha.13
@@ -20,2 +20,8 @@ import { LogLevel } from "./domain"; | ||
maxByteSize?: number; | ||
/** | ||
* Put custom indent for JSON stringify function | ||
* If you don't want to see structured and pretty values then use 0 | ||
* @default 2 | ||
*/ | ||
indent?: number; | ||
}; | ||
@@ -27,2 +33,3 @@ export declare class Logger<C extends LoggerContextData = {}> implements AbstractLogger<C> { | ||
private readonly middlewares; | ||
private readonly indent; | ||
private readonly reducedMiddleware; | ||
@@ -29,0 +36,0 @@ constructor(config: LoggerConfig<C>); |
@@ -13,6 +13,8 @@ "use strict"; | ||
constructor(config) { | ||
var _a, _b, _c; | ||
this.config = config; | ||
this.contextOverrides = {}; | ||
this.maxByteSize = this.config.maxByteSize || 1024 * 10; | ||
this.middlewares = this.config.middlewares || []; | ||
this.maxByteSize = (_a = this.config.maxByteSize) !== null && _a !== void 0 ? _a : 1024 * 10; | ||
this.middlewares = (_b = this.config.middlewares) !== null && _b !== void 0 ? _b : []; | ||
this.indent = (_c = this.config.indent) !== null && _c !== void 0 ? _c : 2; | ||
this.reducedMiddleware = (0, combine_1.combineMiddlewares)(...this.middlewares); | ||
@@ -24,4 +26,6 @@ this.getContext = () => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const finalData = yield this.reducedMiddleware((0, stringify_1.stringifyObject)(this.maxByteSize, Object.assign(Object.assign({}, data), (yield this.getContext())))); | ||
yield Promise.all(this.config.transports.map(x => x.handle(finalData))); | ||
const context = yield this.getContext(); | ||
const string = (0, stringify_1.stringifyObject)(this.maxByteSize, Object.assign(Object.assign({}, data), context), this.indent); | ||
const result = yield this.reducedMiddleware(string); | ||
yield Promise.all(this.config.transports.map(x => x.handle(result))); | ||
} | ||
@@ -28,0 +32,0 @@ catch (error) { |
import type { LoggerValue } from "../../domain"; | ||
export declare function stringify(maxByteSize: number, x: unknown): LoggerValue; | ||
export declare function stringifyObject<T extends Record<string, unknown>>(maxByteSize: number, x: T): Record<keyof T, LoggerValue>; | ||
export declare function stringifyObject<T extends Record<string, unknown>>(maxByteSize: number, object: T, indent?: number): Record<keyof T, LoggerValue>; | ||
export declare function stringify(value: unknown, indent: number): LoggerValue; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stringifyObject = exports.stringify = void 0; | ||
exports.stringify = exports.stringifyObject = void 0; | ||
const tslib_1 = require("tslib"); | ||
const json_stringify_safe_1 = tslib_1.__importDefault(require("json-stringify-safe")); | ||
const object_sizeof_1 = tslib_1.__importDefault(require("object-sizeof")); | ||
function stringify(maxByteSize, x) { | ||
if ((0, object_sizeof_1.default)(x) > maxByteSize) | ||
return `[too-big-object]`; | ||
if (typeof x === "number") | ||
return x; | ||
if (typeof x === "string") | ||
return x; | ||
if (typeof x === "undefined") | ||
return `[null]`; | ||
if (typeof x === "function") | ||
return `[function: ${x.name || "unnamed"}]`; | ||
if (typeof x === "symbol") | ||
return `[symbol: ${x.toString() || "unnamed"}]`; | ||
if (typeof x === "object" && x === null) | ||
return `[null]`; | ||
if (typeof x === "object" && "stack" in x && "name" in x && "message" in x) | ||
return `[${x.name}: ${x.message}]`; | ||
if (Array.isArray(x)) { | ||
if (x.length > 0) | ||
return x.map(x => stringify(maxByteSize, x)).join(", "); | ||
return `[empty]`; | ||
} | ||
return (0, json_stringify_safe_1.default)(x); | ||
} | ||
exports.stringify = stringify; | ||
function stringifyObject(maxByteSize, x) { | ||
return Object.keys(x).reduce((prev, curr) => { | ||
const is_circular_1 = require("@rarible/utils/build/common/is-circular"); | ||
const utils_1 = require("@rarible/utils/build/error/utils"); | ||
const UNKNOWN = `[unknown]`; | ||
function stringifyObject(maxByteSize, object, indent = 2) { | ||
return Object.keys(object).reduce((prev, curr) => { | ||
const key = curr; | ||
prev[key] = stringify(maxByteSize, x[key]); | ||
const value = object[key]; | ||
const result = stringify(value, indent); | ||
if ((0, object_sizeof_1.default)(result) > maxByteSize) { | ||
prev[key] = `[too-big-object]`; | ||
} | ||
else { | ||
prev[key] = result; | ||
} | ||
return prev; | ||
@@ -40,1 +24,42 @@ }, {}); | ||
exports.stringifyObject = stringifyObject; | ||
function stringify(value, indent) { | ||
const loggableValue = toLoggerValue(value); | ||
if (loggableValue !== UNKNOWN) | ||
return loggableValue; | ||
if (typeof value === "object") { | ||
// Check for circular structures on this stage because all below might | ||
// be a subject of circular structures | ||
if ((0, is_circular_1.isCircular)(value)) | ||
return `[circular-object]`; | ||
return JSON.stringify(value, serializer, indent); | ||
} | ||
return UNKNOWN; | ||
} | ||
exports.stringify = stringify; | ||
function toLoggerValue(value) { | ||
if (typeof value === "number") | ||
return value; | ||
if (typeof value === "boolean") | ||
return `${value}`; | ||
if (typeof value === "string") | ||
return value; | ||
if (typeof value === "undefined") | ||
return `[undefined]`; | ||
if (typeof value === "bigint") | ||
return value.toString(); | ||
if (typeof value === "object" && value === null) | ||
return `[null]`; | ||
if (typeof value === "symbol") | ||
return `[symbol: ${value.toString() || "unnamed"}]`; | ||
if (typeof value === "function") | ||
return `[function: ${value.name || "unnamed"}]`; | ||
if ((0, utils_1.isErrorLike)(value)) | ||
return `[${value.name}: ${value.message}]`; | ||
return UNKNOWN; | ||
} | ||
function serializer(_, value) { | ||
const loggableValue = toLoggerValue(value); | ||
if (loggableValue === UNKNOWN) | ||
return value; | ||
return loggableValue; | ||
} |
{ | ||
"name": "@rarible/logger", | ||
"version": "0.10.0-alpha.10", | ||
"version": "0.10.0-alpha.13", | ||
"private": false, | ||
@@ -24,3 +24,2 @@ "description": "Logging utility for ts projects", | ||
"generic-type-guard": "^3.7.2", | ||
"json-stringify-safe": "^5.0.1", | ||
"object-sizeof": "^2.6.3" | ||
@@ -45,3 +44,3 @@ }, | ||
}, | ||
"gitHead": "db86bc82bfc491a74c3717c5c7f1e996306206ef" | ||
"gitHead": "248bce48687c674c82baeaf28130f91975b7a4dc" | ||
} |
Sorry, the diff of this file is not supported yet
55338
4
582
- Removedjson-stringify-safe@^5.0.1
- Removedjson-stringify-safe@5.0.1(transitive)