🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@arcjet/logger

Package Overview
Dependencies
Maintainers
2
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arcjet/logger - npm Package Compare versions

Comparing version
1.7.0
to
1.8.0-rc.0
+139
dist/index.d.ts
//#region src/index.d.ts
/**
* Supported log levels.
*
* The order from most verbose to least verbose is
* `debug`, `info`, `warn`, to `error`.
* When using for example `info`, then `warn` and `error` messages
* are also logged.
*/
type LogLevel = "debug" | "info" | "warn" | "error";
/**
* Configuration.
*/
interface Options {
/**
* Log level.
*/
level: LogLevel;
}
/**
* Configuration.
*
* @deprecated
* Use `Options` instead.
*/
type LoggerOptions = Options;
/**
* Logger.
*/
declare class Logger {
#private;
/**
* Create a new logger.
*
* @param options
* Configuration.
* @returns
* Logger.
*/
constructor(options: Options);
/**
* Debug.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
debug(message: string, ...interpolationValues: unknown[]): void;
/**
* Debug.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
debug(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
/**
* Info.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
info(message: string, ...interpolationValues: unknown[]): void;
/**
* Info.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
info(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
/**
* Warn.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
warn(message: string, ...interpolationValues: unknown[]): void;
/**
* Warn.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
warn(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
/**
* Error.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
error(message: string, ...interpolationValues: unknown[]): void;
/**
* Error.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
error(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
}
//#endregion
export { LogLevel, Logger, LoggerOptions, Options };
import { sprintf } from "@arcjet/sprintf";
//#region src/index.ts
function bigintReplacer(key, value) {
if (typeof value === "bigint") return "[BigInt]";
return value;
}
function tryStringify(value) {
try {
return JSON.stringify(value, bigintReplacer);
} catch {
return "[Circular]";
}
}
const PREFIX = "✦Aj";
function getMessage(mergingObject, message, interpolationValues) {
if (typeof mergingObject === "string") {
interpolationValues = [message, ...interpolationValues];
message = mergingObject;
}
if (typeof message === "string") return sprintf(message, ...interpolationValues);
if (typeof mergingObject === "object" && mergingObject !== null && "msg" in mergingObject && typeof mergingObject.msg === "string") return sprintf(mergingObject.msg, [message, ...interpolationValues]);
}
function getOutput(messageOrObject, message, interpolationValues) {
let output = getMessage(messageOrObject, message, interpolationValues);
if (typeof output !== "string") return;
if (typeof messageOrObject === "object" && messageOrObject !== null) for (const [key, value] of Object.entries(messageOrObject)) output += `\n ${key}: ${tryStringify(value)}`;
return output;
}
/**
* Logger.
*/
var Logger = class {
#logLevel;
/**
* Create a new logger.
*
* @param options
* Configuration.
* @returns
* Logger.
*/
constructor(options) {
if (typeof options.level !== "string") throw new Error(`Invalid log level`);
switch (options.level) {
case "debug":
this.#logLevel = 0;
break;
case "info":
this.#logLevel = 1;
break;
case "warn":
this.#logLevel = 2;
break;
case "error":
this.#logLevel = 3;
break;
default: throw new Error(`Unknown log level: ${options.level}`);
}
}
debug(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 0) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") console.debug(`${PREFIX} DEBUG ${output}`);
}
}
info(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 1) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") console.info(`${PREFIX} INFO ${output}`);
}
}
warn(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 2) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") console.warn(`${PREFIX} WARN ${output}`);
}
}
error(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 3) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") console.error(`${PREFIX} ERROR ${output}`);
}
}
};
//#endregion
export { Logger };
+33
-30
{
"name": "@arcjet/logger",
"version": "1.7.0",
"version": "1.8.0-rc.0",
"description": "Arcjet lightweight logger which mirrors the Pino structured logger interface",

@@ -9,12 +9,6 @@ "keywords": [

"log",
"utility",
"util"
"util",
"utility"
],
"license": "Apache-2.0",
"homepage": "https://arcjet.com",
"repository": {
"type": "git",
"url": "git+https://github.com/arcjet/arcjet-js.git",
"directory": "logger"
},
"bugs": {

@@ -24,2 +18,3 @@ "url": "https://github.com/arcjet/arcjet-js/issues",

},
"license": "Apache-2.0",
"author": {

@@ -30,34 +25,42 @@ "name": "Arcjet",

},
"engines": {
"node": ">=22.21.0 <23 || >=24.5.0"
"repository": {
"type": "git",
"url": "git+https://github.com/arcjet/arcjet-js.git",
"directory": "logger"
},
"type": "module",
"main": "./index.js",
"types": "./index.d.ts",
"files": [
"index.d.ts",
"index.js"
"dist"
],
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public",
"tag": "latest"
},
"scripts": {
"build": "rollup --config rollup.config.js",
"lint": "eslint .",
"test-api": "node --test -- test/*.test.js",
"test-coverage": "node --experimental-test-coverage --test -- test/*.test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"build": "tsdown",
"typecheck": "tsgo --noEmit",
"test-api": "node --test -- test/*.test.ts",
"test-coverage": "node --experimental-test-coverage --test -- test/*.test.ts",
"test": "npm run build && npm run test-coverage"
},
"dependencies": {
"@arcjet/sprintf": "1.7.0"
"@arcjet/sprintf": "1.8.0-rc.0"
},
"devDependencies": {
"@arcjet/eslint-config": "1.7.0",
"@arcjet/rollup-config": "1.7.0",
"@rollup/wasm-node": "4.62.2",
"@types/node": "22.19.21",
"eslint": "9.39.4",
"typescript": "5.9.3"
"tsdown": "0.22.3",
"typescript": "6.0.3"
},
"publishConfig": {
"access": "public",
"tag": "latest"
"engines": {
"node": ">=22.21.0 <23 || >=24.5.0"
}
}
/**
* Supported log levels.
*
* The order from most verbose to least verbose is
* `debug`, `info`, `warn`, to `error`.
* When using for example `info`, then `warn` and `error` messages
* are also logged.
*/
export type LogLevel = "debug" | "info" | "warn" | "error";
/**
* Configuration.
*/
export interface Options {
/**
* Log level.
*/
level: LogLevel;
}
/**
* Configuration.
*
* @deprecated
* Use `Options` instead.
*/
export type LoggerOptions = Options;
/**
* Logger.
*/
export declare class Logger {
#private;
/**
* Create a new logger.
*
* @param options
* Configuration.
* @returns
* Logger.
*/
constructor(options: Options);
/**
* Debug.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
debug(message: string, ...interpolationValues: unknown[]): void;
/**
* Debug.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
debug(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
/**
* Info.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
info(message: string, ...interpolationValues: unknown[]): void;
/**
* Info.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
info(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
/**
* Warn.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
warn(message: string, ...interpolationValues: unknown[]): void;
/**
* Warn.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
warn(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
/**
* Error.
*
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
error(message: string, ...interpolationValues: unknown[]): void;
/**
* Error.
*
* @param mergingObject
* Merging object copied into the JSON log line.
* @param message
* Template.
* @param interpolationValues
* Parameters to interpolate.
* @returns
* Nothing.
*/
error(mergingObject: Record<string, unknown>, message?: string, ...interpolationValues: unknown[]): void;
}
import { sprintf } from '@arcjet/sprintf';
function bigintReplacer(key, value) {
if (typeof value === "bigint") {
return "[BigInt]";
}
return value;
}
// TODO: Deduplicate this and sprintf implementation
function tryStringify(value) {
try {
return JSON.stringify(value, bigintReplacer);
}
catch {
return "[Circular]";
}
}
const PREFIX = "✦Aj";
function getMessage(mergingObject, message, interpolationValues) {
// The first argument was the message so juggle the arguments
if (typeof mergingObject === "string") {
interpolationValues = [message, ...interpolationValues];
message = mergingObject;
}
// Prefer a string message over `mergingObject.msg`, as per Pino:
// https://github.com/pinojs/pino/blob/8db130eba0439e61c802448d31eb1998cebfbc98/docs/api.md#message-string
if (typeof message === "string") {
return sprintf(message, ...interpolationValues);
}
if (typeof mergingObject === "object" &&
mergingObject !== null &&
"msg" in mergingObject &&
typeof mergingObject.msg === "string") {
return sprintf(mergingObject.msg, [message, ...interpolationValues]);
}
}
function getOutput(messageOrObject, message, interpolationValues) {
let output = getMessage(messageOrObject, message, interpolationValues);
if (typeof output !== "string") {
return;
}
if (typeof messageOrObject === "object" && messageOrObject !== null) {
for (const [key, value] of Object.entries(messageOrObject)) {
output += `\n ${key}: ${tryStringify(value)}`;
}
}
return output;
}
/**
* Logger.
*/
class Logger {
#logLevel;
/**
* Create a new logger.
*
* @param options
* Configuration.
* @returns
* Logger.
*/
constructor(options) {
if (typeof options.level !== "string") {
throw new Error(`Invalid log level`);
}
switch (options.level) {
case "debug":
this.#logLevel = 0;
break;
case "info":
this.#logLevel = 1;
break;
case "warn":
this.#logLevel = 2;
break;
case "error":
this.#logLevel = 3;
break;
default: {
throw new Error(`Unknown log level: ${options.level}`);
}
}
}
debug(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 0) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") {
console.debug(`${PREFIX} DEBUG ${output}`);
}
}
}
info(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 1) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") {
console.info(`${PREFIX} INFO ${output}`);
}
}
}
warn(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 2) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") {
console.warn(`${PREFIX} WARN ${output}`);
}
}
}
error(messageOrObject, message, ...interpolationValues) {
if (this.#logLevel <= 3) {
const output = getOutput(messageOrObject, message, interpolationValues);
if (typeof output !== "undefined") {
console.error(`${PREFIX} ERROR ${output}`);
}
}
}
}
export { Logger };