@dxos/log
Advanced tools
Comparing version 0.1.10 to 0.1.11
@@ -18,2 +18,7 @@ import { LogConfig, LogLevel } from './config'; | ||
bugcheck?: string; | ||
/** | ||
* A callback that will invoke the provided function with provided arguments. | ||
* Useful in the browser to force a `console.log` call to have a certain stack-trace. | ||
*/ | ||
callSite?: (fn: Function, args: any[]) => void; | ||
} | ||
@@ -20,0 +25,0 @@ /** |
@@ -6,13 +6,19 @@ import { LogOptions } from './config'; | ||
*/ | ||
type Logger = (message: string, context?: LogContext, meta?: LogMetadata) => void; | ||
type LogFunction = (message: string, context?: LogContext, meta?: LogMetadata) => void; | ||
/** | ||
* Logging methods. | ||
*/ | ||
interface LogMethods { | ||
debug: LogFunction; | ||
info: LogFunction; | ||
warn: LogFunction; | ||
error: LogFunction; | ||
catch: (error: Error | any, context?: LogContext, meta?: LogMetadata) => void; | ||
break: () => void; | ||
} | ||
/** | ||
* Properties accessible on the logging function. | ||
*/ | ||
interface Log extends Logger { | ||
interface Log extends LogMethods, LogFunction { | ||
config: (options: LogOptions) => void; | ||
debug: Logger; | ||
info: Logger; | ||
warn: Logger; | ||
error: Logger; | ||
catch: (error: Error | any, context?: LogContext, meta?: LogMetadata) => void; | ||
} | ||
@@ -23,2 +29,5 @@ /** | ||
export declare const log: Log; | ||
/** | ||
* Accessible from browser console. | ||
*/ | ||
declare global { | ||
@@ -25,0 +34,0 @@ const dx_log: Log; |
@@ -24,4 +24,6 @@ "use strict"; | ||
log.error = (...params) => processLog(config_1.LogLevel.ERROR, ...params); | ||
// TODO(burdon): Not required since can determine value. | ||
log.catch = (error, context, meta) => processLog(config_1.LogLevel.ERROR, error.message, context, meta, error); | ||
// Catch only shows error message, not stacktrace. | ||
log.catch = (error, context, meta) => processLog(config_1.LogLevel.ERROR, error.message, context, meta); | ||
// Show break. | ||
log.break = () => log.info('——————————————————————————————————————————————————'); | ||
/** | ||
@@ -31,9 +33,3 @@ * Process the current log call. | ||
const processLog = (level, message, context, meta, error) => { | ||
log._config.processor(log._config, { | ||
level, | ||
message, | ||
context, | ||
meta, | ||
error | ||
}); | ||
log._config.processor(log._config, { level, message, context, meta, error }); | ||
}; | ||
@@ -45,4 +41,3 @@ return log; | ||
*/ | ||
// TODO(burdon): Instance loggers? (e.g., provide additional displayed logging context/filtering). | ||
exports.log = ((_a = (_b = globalThis).dx_log) !== null && _a !== void 0 ? _a : (_b.dx_log = createLog())); | ||
//# sourceMappingURL=log.js.map |
@@ -12,6 +12,21 @@ "use strict"; | ||
const log_1 = require("./log"); | ||
class LogError extends Error { | ||
constructor(message, context) { | ||
super(message); | ||
this.context = context; | ||
// Restore prototype chain. | ||
// https://stackoverflow.com/a/48342359 | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
toString() { | ||
return `LogError: ${this.message}`; | ||
} | ||
} | ||
log_1.log.config({ | ||
filter: config_1.LogLevel.DEBUG | ||
}); | ||
describe('log', function () { | ||
it('throws an error', function () { | ||
try { | ||
throw new Error('Test failed'); | ||
throw new LogError('Test failed', { value: 1 }); | ||
} | ||
@@ -22,5 +37,13 @@ catch (err) { | ||
}); | ||
it('throws an error showing stacktrace', function () { | ||
try { | ||
throw new LogError('Test failed', { value: 2 }); | ||
} | ||
catch (err) { | ||
log_1.log.error('failed', err); | ||
} | ||
}); | ||
it('catches an error', function () { | ||
try { | ||
throw new Error('ERROR ON LINE 21'); | ||
throw new LogError('ERROR ON LINE 21', { value: 3 }); | ||
} | ||
@@ -27,0 +50,0 @@ catch (err) { |
@@ -20,3 +20,3 @@ "use strict"; | ||
const BROWSER_PROCESSOR = (config, entry) => { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
if (!(0, context_1.shouldLog)(config, entry.level, (_b = (_a = entry.meta) === null || _a === void 0 ? void 0 : _a.file) !== null && _b !== void 0 ? _b : '')) { | ||
@@ -41,3 +41,3 @@ return; | ||
} | ||
const args = []; | ||
let args = []; | ||
args.push(entry.message); | ||
@@ -52,11 +52,17 @@ if (entry.context && Object.keys(entry.context).length > 0) { | ||
}; | ||
const level = (_d = levels[entry.level]) !== null && _d !== void 0 ? _d : console.log; | ||
if (LOG_BROWSER_CSS === null || LOG_BROWSER_CSS === void 0 ? void 0 : LOG_BROWSER_CSS.length) { | ||
level.call(level, `%c${link}\n%c${args.join(' ')}`, ...LOG_BROWSER_CSS); | ||
args = [`%c${link}\n%c${args.join(' ')}`, ...LOG_BROWSER_CSS]; | ||
} | ||
else { | ||
level.call(level, link + '\n', ...args); | ||
args = [link + '\n', ...args]; | ||
} | ||
const level = (_d = levels[entry.level]) !== null && _d !== void 0 ? _d : console.log; | ||
if (typeof ((_e = entry.meta) === null || _e === void 0 ? void 0 : _e.callSite) === 'function') { | ||
entry.meta.callSite(level, args); | ||
} | ||
else { | ||
level(...args); | ||
} | ||
}; | ||
exports.BROWSER_PROCESSOR = BROWSER_PROCESSOR; | ||
//# sourceMappingURL=browser-processor.js.map |
@@ -78,3 +78,6 @@ "use strict"; | ||
if (context instanceof Error) { | ||
parts.context = (0, node_util_1.inspect)(level === config_1.LogLevel.ERROR ? context : String(context), { colors: true }); | ||
// Additional context from Error. | ||
const c = context.context; | ||
// If ERROR then show stacktrace. | ||
parts.context = (0, node_util_1.inspect)(level === config_1.LogLevel.ERROR ? context : { error: String(context), ...c }, { colors: true }); | ||
} | ||
@@ -81,0 +84,0 @@ else if (context && Object.keys(context).length > 0) { |
{ | ||
"name": "@dxos/log", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"description": "Logger", | ||
@@ -41,6 +41,3 @@ "homepage": "https://dxos.org", | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"check": "true" | ||
} | ||
} |
@@ -27,2 +27,8 @@ // | ||
bugcheck?: string; | ||
/** | ||
* A callback that will invoke the provided function with provided arguments. | ||
* Useful in the browser to force a `console.log` call to have a certain stack-trace. | ||
*/ | ||
callSite?: (fn: Function, args: any[]) => void; | ||
} | ||
@@ -29,0 +35,0 @@ |
@@ -10,6 +10,23 @@ // | ||
class LogError extends Error { | ||
constructor(message: string, private readonly context?: any) { | ||
super(message); | ||
// Restore prototype chain. | ||
// https://stackoverflow.com/a/48342359 | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
override toString() { | ||
return `LogError: ${this.message}`; | ||
} | ||
} | ||
log.config({ | ||
filter: LogLevel.DEBUG | ||
}); | ||
describe('log', function () { | ||
it('throws an error', function () { | ||
try { | ||
throw new Error('Test failed'); | ||
throw new LogError('Test failed', { value: 1 }); | ||
} catch (err: any) { | ||
@@ -20,5 +37,13 @@ log.warn('failed', err); | ||
it('throws an error showing stacktrace', function () { | ||
try { | ||
throw new LogError('Test failed', { value: 2 }); | ||
} catch (err: any) { | ||
log.error('failed', err); | ||
} | ||
}); | ||
it('catches an error', function () { | ||
try { | ||
throw new Error('ERROR ON LINE 21'); | ||
throw new LogError('ERROR ON LINE 21', { value: 3 }); | ||
} catch (err: any) { | ||
@@ -25,0 +50,0 @@ log.catch(err); |
@@ -12,16 +12,21 @@ // | ||
*/ | ||
type Logger = (message: string, context?: LogContext, meta?: LogMetadata) => void; | ||
type LogFunction = (message: string, context?: LogContext, meta?: LogMetadata) => void; | ||
/** | ||
* Logging methods. | ||
*/ | ||
interface LogMethods { | ||
debug: LogFunction; | ||
info: LogFunction; | ||
warn: LogFunction; | ||
error: LogFunction; | ||
catch: (error: Error | any, context?: LogContext, meta?: LogMetadata) => void; | ||
break: () => void; | ||
} | ||
/** | ||
* Properties accessible on the logging function. | ||
*/ | ||
interface Log extends Logger { | ||
interface Log extends LogMethods, LogFunction { | ||
config: (options: LogOptions) => void; | ||
debug: Logger; | ||
info: Logger; | ||
warn: Logger; | ||
error: Logger; | ||
catch: (error: Error | any, context?: LogContext, meta?: LogMetadata) => void; | ||
} | ||
@@ -51,5 +56,8 @@ | ||
// TODO(burdon): Not required since can determine value. | ||
log.catch = (error: Error | any, context, meta) => processLog(LogLevel.ERROR, error.message, context, meta, error); | ||
// Catch only shows error message, not stacktrace. | ||
log.catch = (error: Error | any, context, meta) => processLog(LogLevel.ERROR, error.message, context, meta); | ||
// Show break. | ||
log.break = () => log.info('——————————————————————————————————————————————————'); | ||
/** | ||
@@ -59,9 +67,3 @@ * Process the current log call. | ||
const processLog = (level: LogLevel, message: string, context?: LogContext, meta?: LogMetadata, error?: Error) => { | ||
log._config.processor(log._config, { | ||
level, | ||
message, | ||
context, | ||
meta, | ||
error | ||
}); | ||
log._config.processor(log._config, { level, message, context, meta, error }); | ||
}; | ||
@@ -75,5 +77,7 @@ | ||
*/ | ||
// TODO(burdon): Instance loggers? (e.g., provide additional displayed logging context/filtering). | ||
export const log: Log = ((globalThis as any).dx_log ??= createLog()); | ||
/** | ||
* Accessible from browser console. | ||
*/ | ||
declare global { | ||
@@ -80,0 +84,0 @@ // eslint-disable-next-line camelcase |
@@ -44,3 +44,3 @@ // | ||
const args = []; | ||
let args = []; | ||
args.push(entry.message); | ||
@@ -57,8 +57,14 @@ if (entry.context && Object.keys(entry.context).length > 0) { | ||
const level = levels[entry.level] ?? console.log; | ||
if (LOG_BROWSER_CSS?.length) { | ||
level.call(level, `%c${link}\n%c${args.join(' ')}`, ...LOG_BROWSER_CSS); | ||
args = [`%c${link}\n%c${args.join(' ')}`, ...LOG_BROWSER_CSS]; | ||
} else { | ||
level.call(level, link + '\n', ...args); | ||
args = [link + '\n', ...args]; | ||
} | ||
const level = levels[entry.level] ?? console.log; | ||
if (typeof entry.meta?.callSite === 'function') { | ||
entry.meta.callSite(level, args); | ||
} else { | ||
level(...args); | ||
} | ||
}; |
@@ -97,3 +97,6 @@ // | ||
if (context instanceof Error) { | ||
parts.context = inspect(level === LogLevel.ERROR ? context : String(context), { colors: true }); | ||
// Additional context from Error. | ||
const c = (context as any).context; | ||
// If ERROR then show stacktrace. | ||
parts.context = inspect(level === LogLevel.ERROR ? context : { error: String(context), ...c }, { colors: true }); | ||
} else if (context && Object.keys(context).length > 0) { | ||
@@ -100,0 +103,0 @@ // Remove undefined fields. |
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
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
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
86162
1644