Socket
Socket
Sign inDemoInstall

@dxos/log

Package Overview
Dependencies
Maintainers
13
Versions
2240
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dxos/log - npm Package Compare versions

Comparing version 0.1.13 to 0.1.14

dist/lib/browser/index.mjs

16

package.json
{
"name": "@dxos/log",
"version": "0.1.13",
"version": "0.1.14",
"description": "Logger",

@@ -9,9 +9,8 @@ "homepage": "https://dxos.org",

"author": "DXOS.org",
"main": "dist/src/index.js",
"main": "dist/lib/node/index.cjs",
"browser": {
"node:util": "util",
"./dist/src/platform/node/index.js": "./dist/src/platform/browser/index.js",
"./src/platform/node/index.ts": "./src/platform/browser/index.ts"
"./src/platform/node/index.ts": "./src/platform/browser/index.ts",
"./dist/lib/node/index.cjs": "./dist/lib/browser/index.mjs"
},
"types": "dist/src/index.d.ts",
"types": "dist/types/src/index.d.ts",
"files": [

@@ -26,11 +25,10 @@ "dist",

"lodash.pickby": "^4.6.0",
"util": "^0.12.4"
"@dxos/node-std": "0.1.14"
},
"devDependencies": {
"@swc-node/sourcemap-support": "^0.2.0",
"@swc/core": "^1.3.9",
"@swc/core": "^1.3.21",
"@types/js-yaml": "^4.0.5",
"@types/lodash.defaultsdeep": "^4.6.6",
"@types/lodash.pickby": "^4.6.7",
"@types/mocha": "^8.2.2",
"@types/node": "^18.11.9",

@@ -37,0 +35,0 @@ "pirates": "~4.0.5",

@@ -5,2 +5,4 @@ //

import { describe, test } from '@dxos/test';
import { log } from '../log';

@@ -10,3 +12,3 @@ import { debugInfo, ownershipClass } from './ownership';

describe('classes', function () {
it('field instance', function () {
test('field instance', function () {
@ownershipClass

@@ -42,3 +44,3 @@ class Inner {

it('return values', async function () {
test('return values', async function () {
@ownershipClass

@@ -45,0 +47,0 @@ class Instance {

@@ -7,2 +7,4 @@ //

import { describe, test } from '@dxos/test';
import { LogLevel } from './config';

@@ -28,4 +30,6 @@ import { log } from './log';

/* eslint-disable @stayradiated/prefer-arrow-functions/prefer-arrow-functions */
describe('log', function () {
it('throws an error', function () {
test('throws an error', function () {
try {

@@ -38,3 +42,3 @@ throw new LogError('Test failed', { value: 1 });

it('throws an error showing stacktrace', function () {
test('throws an error showing stacktrace', function () {
try {

@@ -47,3 +51,3 @@ throw new LogError('Test failed', { value: 2 });

it('catches an error', function () {
test('catches an error', function () {
try {

@@ -56,3 +60,3 @@ throw new LogError('ERROR ON LINE 21', { value: 3 });

it('config', function () {
test('config', function () {
log.config({

@@ -67,3 +71,3 @@ filter: LogLevel.INFO

it('config file', function () {
test('config file', function () {
log.config({

@@ -78,3 +82,3 @@ file: path.join('packages/common/log/test-config.yml')

it('levels', function () {
test('levels', function () {
log('Default level log message');

@@ -87,3 +91,3 @@ log.debug('Debug level log message');

it('context', function () {
test('context', function () {
log.info('Message with context', {

@@ -90,0 +94,0 @@ title: 'test',

@@ -56,3 +56,3 @@ //

// Catch only shows error message, not stacktrace.
log.catch = (error: Error | any, context, meta) => processLog(LogLevel.ERROR, error.message, context, meta);
log.catch = (error: Error | any, context, meta) => processLog(LogLevel.ERROR, error.stack, context, meta);

@@ -59,0 +59,0 @@ // Show break.

@@ -20,3 +20,22 @@ //

export const BROWSER_PROCESSOR: LogProcessor = (config, entry) => {
type Config = {
useTestProcessor: boolean;
printFileLinks: boolean;
};
const CONFIG: Config =
typeof mochaExecutor !== 'undefined'
? {
useTestProcessor: true,
printFileLinks: true
}
: {
useTestProcessor: false,
printFileLinks: false
};
/**
* For running apps in the browser normally.
*/
const APP_BROWSER_PROCESSOR: LogProcessor = (config, entry) => {
if (!shouldLog(config, entry.level, entry.meta?.file ?? '')) {

@@ -57,8 +76,47 @@ return;

if (LOG_BROWSER_CSS?.length) {
args = [`%c${link}\n%c${args.join(' ')}`, ...LOG_BROWSER_CSS];
if (CONFIG.printFileLinks) {
if (LOG_BROWSER_CSS?.length) {
args = [`%c${link}\n%c${args.join(' ')}`, ...LOG_BROWSER_CSS];
} else {
args = [link + '\n', ...args];
}
}
const level = levels[entry.level] ?? console.log;
if (typeof entry.meta?.callSite === 'function') {
entry.meta.callSite(level, args);
} else {
args = [link + '\n', ...args];
level(...args);
}
};
/**
* For running unit tests in the headless browser.
*/
const TEST_BROWSER_PROCESSOR: LogProcessor = (config, entry) => {
if (!shouldLog(config, entry.level, entry.meta?.file ?? '')) {
return;
}
let path = '';
if (entry.meta) {
path = `${getRelativeFilename(entry.meta.file)}:${entry.meta.line}`;
}
let args = [];
args.push(entry.message);
if (entry.context && Object.keys(entry.context).length > 0) {
args.push(entry.context);
}
const levels: any = {
[LogLevel.ERROR]: console.error,
[LogLevel.WARN]: console.warn,
[LogLevel.DEBUG]: console.log
};
if (CONFIG.printFileLinks) {
args = [path, ...args];
}
const level = levels[entry.level] ?? console.log;

@@ -71,1 +129,3 @@ if (typeof entry.meta?.callSite === 'function') {

};
export const BROWSER_PROCESSOR: LogProcessor = CONFIG.useTestProcessor ? TEST_BROWSER_PROCESSOR : APP_BROWSER_PROCESSOR;

@@ -100,3 +100,5 @@ //

// If ERROR then show stacktrace.
parts.context = inspect(level === LogLevel.ERROR ? context : { error: String(context), ...c }, { colors: true });
parts.context = inspect(level === LogLevel.ERROR ? context : { error: context?.stack ?? String(context), ...c }, {
colors: true
});
} else if (context && Object.keys(context).length > 0) {

@@ -103,0 +105,0 @@ // Remove undefined fields.

@@ -5,2 +5,4 @@ //

import { describe, test } from '@dxos/test';
import { log } from './log';

@@ -10,3 +12,3 @@ import { logInfo } from './scope';

describe('Scope capture', function () {
it('field instance', function () {
test('field instance', function () {
class Container {

@@ -13,0 +15,0 @@ constructor(private readonly _id: string) {}

@@ -7,3 +7,15 @@ //

export const logInfo = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
/**
* Decorate fields, properties, or methods to automatically include their values in log messages.
*
* Example:
*
* ```typescript
* class Example {
* @logInfo
* peerId: PublicKey;
* }
* ```
*/
export const logInfo = (target: any, propertyKey: string, descriptor?: PropertyDescriptor) => {
// console.log(target, propertyKey, descriptor);

@@ -13,3 +25,11 @@ (target[logInfoProperties] ??= []).push(propertyKey);

/**
* Introspects class instance to find decorated metadata.
* @param scope Class instance.
*/
export const gatherLogInfoFromScope = (scope: any): Record<string, any> => {
if (!scope) {
return {};
}
const res: Record<string, any> = {};

@@ -20,10 +40,6 @@

for (const prop of infoProps) {
if (typeof scope[prop] === 'function') {
try {
res[prop] = scope[prop]();
} catch (err: any) {
res[prop] = err.message;
}
} else {
res[prop] = scope[prop];
try {
res[prop] = typeof scope[prop] === 'function' ? scope[prop]() : scope[prop];
} catch (err: any) {
res[prop] = err.message;
}

@@ -30,0 +46,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc