New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@plandek-utils/logging

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@plandek-utils/logging

TypeScript utils for Logging. Includes prettifying of JSON, logging utils, and colour utils.

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
2
Created
Source

@plandek-utils/logging

npm version Maintainability Test Coverage

TypeScript utils for Logging. Includes prettifying of JSON, logging utils, and colour utils.

Installation

npm install @plandek-utils/logging

Usage

Pretty JSON: prettyJSON and colorPrettyJSON or colourPrettyJSON

import { prettyJSON } from "@plandek-utils/logging";

const obj = { name: "John", age: 30 };
console.log(prettyJSON(obj)); // OUTPUT: {\n  "name": "John",\n  "age": 30\n}
import { colorPrettyJSON } from "@plandek-utils/logging";

const obj = { name: "John", age: 30 };
console.log(colorPrettyJSON(obj));

output:

"\x1b[90m{\x1b[39m\x1b[90m\n" +
  '  \x1b[39m\x1b[35m"name"\x1b[39m\x1b[90m:\x1b[39m\x1b[90m \x1b[39m\x1b[33m"John"\x1b[39m\x1b[90m,\x1b[39m\x1b[90m\n' +
  '  \x1b[39m\x1b[35m"age"\x1b[39m\x1b[90m:\x1b[39m\x1b[90m \x1b[39m\x1b[32m30\x1b[39m\x1b[90m\n' +
  "\x1b[39m\x1b[90m}\x1b[39m"

You can use colorPrettyJSON or colourPrettyJSON (alias).

makeColourUtils(mode: "with-colour" | "plain"): LogColourUtils

Creates a utility object for colouring log messages using CHALK.

import { makeColourUtils } from "@plandek-utils/logging";

const colorUtils = makeColourUtils("with-colour");
console.log(colorUtils.blue("Hello"));

const plainColorUtils = makeColourUtils("plain");
console.log(plainColorUtils.blue("Hello")); // Output: Hello as is.

Loggers

  • for real: buildPinoLogger(level: LevelWithSilent, redactPaths?: string[]): PreparedLogger
  • for test: buildSinkLogger(level: LevelWithSilent, bindings?: PlainObject): PreparedLogger
  • parse log level: parseLogLevelOrDefault(x: string): LogLevel

Creates a Pino logger with common configuration.

import { buildPinoLogger, buildSinkLogger, parseLogLevelOrDefault } from "@plandek-utils/logging";

const level = parseLogLevelOrDefault(process.env.LOG_LEVEL, "info"); // gets the log level if present, otherwise info. If the LOG_LEVEL is not a valid one it will throw.
const logger = buildPinoLogger(level, ["req.headers.authorization"]);

const testLogger = buildSinkLogger(level); // does not send any log -> useful for tests.

logger.info({ req: { headers: { authorization: "Bearer token" } } }, "User logged in");
// {"level":30,"time":"2024-10-28T11:10:58.250Z","pid":18166,"hostname":"044ce1509ebe","req":{"headers":{"authorization":"[REDACTED]"}},"msg":"User logged in"}

logger.info("Hi!");
// {"level":30,"time":"2024-10-28T11:12:48.732Z","pid":18166,"hostname":"044ce1509ebe","msg":"Hi!"}

makeLogging

import { makeLogging } from "@plandek-utils/logging";

const logger = buildPinoLogger("info");
const logging = makeLogging({ logger, section: "api" });

logging.info("User logged in", { userId: 123 });
// {"level":30,"time":"2024-10-28T11:14:13.519Z","pid":18166,"hostname":"044ce1509ebe","logSections":["api"],"userId":123,"msg":"User logged in"}

logging.withSection("database").debug("Query executed");
// no output since `debug` is not enabled in the logger

logging.withSection("database").error("Query failed");
// {"level":50,"time":"2024-10-28T11:14:59.509Z","pid":18166,"hostname":"044ce1509ebe","logSections":["api"],"logSections":["api","database"],"msg":"Query failed"}

makeLoggingWithRecord

Same as makeLogging, but keeping an in-memory record of all messages (and contexts) sent. This is meant for testing purposes.

import { makeLoggingWithRecord, type PreparedLogger } from "@plandek-utils/logging";

const logger: PreparedLogger = buildPinoLogger("info");
const loggingWithRecords = makeLoggingWithRecord({ logger, section: "api" });

loggingWithRecords.info("User logged in", { userId: 123 });
// {"level":30,"time":"2024-10-28T11:16:49.607Z","pid":18166,"hostname":"044ce1509ebe","logSections":["api"],"userId":123,"msg":"User logged in"}

console.log(loggingWithRecords.messages.info);
// [ [ "User logged in", { userId: 123 } ] ]

Development

This package is developed with TypeScript and uses Vitest for testing.

  • npm run build: build the package
  • npm test: run tests
  • npm run test:watch: run tests in watch mode
  • npm run test:coverage: run tests with coverage
  • npm run lint: lint files
  • npm run format: format files

License

MIT License - see LICENSE file

Keywords

logging

FAQs

Package last updated on 31 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts