@dxos/log
Advanced tools
Comparing version 0.1.13 to 0.1.14
{ | ||
"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 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136332
9
1778
60
8
1
+ Added@dxos/node-std@0.1.14
+ Added@dxos/node-std@0.1.14(transitive)
+ Addedassert@2.1.0(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedis-nan@1.3.2(transitive)
+ Addedobject-is@1.1.6(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedpath-browserify@1.0.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedutil@^0.12.4