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.2 to 0.1.3

2

dist/src/context.d.ts

@@ -6,3 +6,3 @@ import { LogConfig, LogLevel } from './config';

*/
export declare type LogContext = Record<string, any>;
export declare type LogContext = Record<string, any> | Error | any;
/**

@@ -9,0 +9,0 @@ * Generated meta data from source map.

@@ -16,3 +16,3 @@ import { LogOptions } from './config';

error: Logger;
catch: (error: Error, context?: LogContext, meta?: LogMetadata) => void;
catch: (error: Error | any, context?: LogContext, meta?: LogMetadata) => void;
}

@@ -22,3 +22,3 @@ /**

*/
export declare const log: any;
export declare const log: Log;
declare global {

@@ -25,0 +25,0 @@ const dx_log: Log;

@@ -14,5 +14,8 @@ "use strict";

log._config = (0, options_1.getConfig)();
// Set config.
log.config = (options) => {
log._config = (0, options_1.getConfig)(options);
};
// TODO(burdon): API to set context and separate error object.
// E.g., log.warn('failed', { key: 123 }, err);
log.debug = (...params) => processLog(config_1.LogLevel.DEBUG, ...params);

@@ -22,4 +25,4 @@ log.info = (...params) => processLog(config_1.LogLevel.INFO, ...params);

log.error = (...params) => processLog(config_1.LogLevel.ERROR, ...params);
// TODO(burdon): Option to display/hide stack.
log.catch = (error, context, meta) => processLog(config_1.LogLevel.ERROR, String(error.stack), context, meta, error);
// TODO(burdon): Not required since can determine value.
log.catch = (error, context, meta) => processLog(config_1.LogLevel.ERROR, error.message, context, meta, error);
/**

@@ -26,0 +29,0 @@ * Process the current log call.

@@ -13,8 +13,15 @@ "use strict";

describe('log', function () {
it('line numbers', function () {
log_1.log.warn('LOG LINE 12'); // TODO(burdon): Test by configuring custom processor.
it('throws an error', function () {
try {
throw new Error('ERROR ON LINE 15');
throw new Error('Test failed');
}
catch (err) {
log_1.log.warn('failed', err);
}
});
it('catches an error', function () {
try {
throw new Error('ERROR ON LINE 21');
}
catch (err) {
log_1.log.catch(err);

@@ -21,0 +28,0 @@ }

@@ -34,2 +34,3 @@ "use strict";

exports.parseFilter = parseFilter;
const IS_BROWSER = typeof window !== 'undefined' || typeof navigator !== 'undefined';
const getConfig = (_options) => {

@@ -44,5 +45,6 @@ var _a, _b, _c, _d;

if (options.file) {
options = (0, lodash_defaultsdeep_1.default)(options, (0, platform_1.loadOptions)(options.file));
options = (0, lodash_defaultsdeep_1.default)({}, (0, platform_1.loadOptions)(options.file), options);
console.log(JSON.stringify(options, undefined, 2));
}
const defaultProcessor = typeof window !== 'undefined' && typeof window.document !== 'undefined' ? processors_1.BROWSER_PROCESSOR : processors_1.CONSOLE_PROCESSOR;
const defaultProcessor = IS_BROWSER ? processors_1.BROWSER_PROCESSOR : processors_1.CONSOLE_PROCESSOR;
return {

@@ -49,0 +51,0 @@ options,

import { LogOptions } from '../../config';
/**
* Node config loader.
*/
export declare const loadOptions: (filepath?: string) => LogOptions | undefined;
//# sourceMappingURL=index.d.ts.map

@@ -12,8 +12,19 @@ "use strict";

const js_yaml_1 = __importDefault(require("js-yaml"));
const path_1 = __importDefault(require("path"));
/**
* Node config loader.
*/
const loadOptions = (filepath) => {
if (filepath) {
const text = fs_1.default.readFileSync(filepath, 'utf-8');
if (text) {
return js_yaml_1.default.load(text);
const fullpath = path_1.default.join(process.cwd(), filepath);
console.log(`Log file: ${fullpath}`);
try {
const text = fs_1.default.readFileSync(fullpath, 'utf-8');
if (text) {
return js_yaml_1.default.load(text);
}
}
catch (err) {
console.warn(`Invalid log file: ${fullpath}`);
}
}

@@ -20,0 +31,0 @@ };

@@ -11,2 +11,3 @@ "use strict";

const chalk_1 = __importDefault(require("chalk"));
const lodash_pickby_1 = __importDefault(require("lodash.pickby"));
const node_util_1 = require("node:util");

@@ -71,5 +72,12 @@ const config_1 = require("../config");

}
if ((context && Object.keys(context).length > 0) || context instanceof Error) {
parts.context = (0, node_util_1.inspect)(context, false, undefined, true);
if (context instanceof Error) {
parts.context = (0, node_util_1.inspect)(level === config_1.LogLevel.ERROR ? context : String(context), { colors: true });
}
else if (context && Object.keys(context).length > 0) {
// Remove undefined fields.
// https://nodejs.org/api/util.html#utilinspectobject-options
// Remove undefined fields.
// https://nodejs.org/api/util.html#utilinspectobject-options
parts.context = (0, node_util_1.inspect)((0, lodash_pickby_1.default)(context, (value) => value !== undefined), { depth: undefined, colors: true, maxArrayLength: 8, sorted: true });
}
const line = formatter(config, parts).filter(Boolean).join(' ');

@@ -76,0 +84,0 @@ console.log(line);

{
"name": "@dxos/log",
"version": "0.1.2",
"version": "0.1.3",
"description": "Logger",

@@ -24,2 +24,3 @@ "homepage": "https://dxos.org",

"lodash.defaultsdeep": "^4.6.1",
"lodash.pickby": "^4.6.0",
"util": "^0.12.4"

@@ -32,4 +33,5 @@ },

"@types/lodash.defaultsdeep": "^4.6.6",
"@types/lodash.pickby": "^4.6.7",
"@types/mocha": "^8.2.2",
"@types/node": "^16.11.58",
"@types/node": "^18.11.9",
"pirates": "~4.0.5",

@@ -36,0 +38,0 @@ "sorcery": "^0.10.0",

@@ -11,3 +11,3 @@ //

*/
export type LogContext = Record<string, any>;
export type LogContext = Record<string, any> | Error | any;

@@ -14,0 +14,0 @@ /**

@@ -11,7 +11,13 @@ //

describe('log', function () {
it('line numbers', function () {
log.warn('LOG LINE 12'); // TODO(burdon): Test by configuring custom processor.
it('throws an error', function () {
try {
throw new Error('Test failed');
} catch (err: any) {
log.warn('failed', err);
}
});
it('catches an error', function () {
try {
throw new Error('ERROR ON LINE 15');
throw new Error('ERROR ON LINE 21');
} catch (err: any) {

@@ -18,0 +24,0 @@ log.catch(err);

@@ -25,3 +25,3 @@ //

catch: (error: Error, context?: LogContext, meta?: LogMetadata) => void;
catch: (error: Error | any, context?: LogContext, meta?: LogMetadata) => void;
}

@@ -38,2 +38,3 @@

// Set config.
log.config = (options: LogOptions) => {

@@ -43,2 +44,5 @@ log._config = getConfig(options);

// TODO(burdon): API to set context and separate error object.
// E.g., log.warn('failed', { key: 123 }, err);
log.debug = (...params) => processLog(LogLevel.DEBUG, ...params);

@@ -49,4 +53,4 @@ log.info = (...params) => processLog(LogLevel.INFO, ...params);

// TODO(burdon): Option to display/hide stack.
log.catch = (error: Error, context, meta) => processLog(LogLevel.ERROR, String(error.stack), context, meta, error);
// TODO(burdon): Not required since can determine value.
log.catch = (error: Error | any, context, meta) => processLog(LogLevel.ERROR, error.message, context, meta, error);

@@ -73,3 +77,3 @@ /**

// TODO(burdon): Instance loggers? (e.g., provide additional displayed logging context/filtering).
export const log = ((globalThis as any).dx_log ??= createLog());
export const log: Log = ((globalThis as any).dx_log ??= createLog());

@@ -76,0 +80,0 @@ declare global {

@@ -35,2 +35,4 @@ //

const IS_BROWSER = typeof window !== 'undefined' || typeof navigator !== 'undefined';
export const getConfig = (_options?: LogOptions): LogConfig => {

@@ -49,7 +51,7 @@ let options: LogOptions = defaultsDeep(

if (options.file) {
options = defaultsDeep(options, loadOptions(options.file));
options = defaultsDeep({}, loadOptions(options.file), options);
console.log(JSON.stringify(options, undefined, 2));
}
const defaultProcessor =
typeof window !== 'undefined' && typeof window.document !== 'undefined' ? BROWSER_PROCESSOR : CONSOLE_PROCESSOR;
const defaultProcessor = IS_BROWSER ? BROWSER_PROCESSOR : CONSOLE_PROCESSOR;

@@ -56,0 +58,0 @@ return {

@@ -7,12 +7,22 @@ //

import yaml from 'js-yaml';
import path from 'path';
import { LogOptions } from '../../config';
/**
* Node config loader.
*/
export const loadOptions = (filepath?: string): LogOptions | undefined => {
if (filepath) {
const text = fs.readFileSync(filepath, 'utf-8');
if (text) {
return yaml.load(text) as LogOptions;
const fullpath = path.join(process.cwd(), filepath);
console.log(`Log file: ${fullpath}`);
try {
const text = fs.readFileSync(fullpath, 'utf-8');
if (text) {
return yaml.load(text) as LogOptions;
}
} catch (err) {
console.warn(`Invalid log file: ${fullpath}`);
}
}
};

@@ -6,2 +6,3 @@ //

import chalk from 'chalk';
import pickBy from 'lodash.pickby';
import { inspect } from 'node:util';

@@ -89,4 +90,13 @@

if ((context && Object.keys(context).length > 0) || context instanceof Error) {
parts.context = inspect(context, false, undefined, true);
if (context instanceof Error) {
parts.context = inspect(level === LogLevel.ERROR ? context : String(context), { colors: true });
} else if (context && Object.keys(context).length > 0) {
// Remove undefined fields.
// https://nodejs.org/api/util.html#utilinspectobject-options
// Remove undefined fields.
// https://nodejs.org/api/util.html#utilinspectobject-options
parts.context = inspect(
pickBy(context, (value?: unknown) => value !== undefined),
{ depth: undefined, colors: true, maxArrayLength: 8, sorted: true }
);
}

@@ -93,0 +103,0 @@

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

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

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

Sorry, the diff of this file is not supported yet

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