This module provides logging functionality relying up the built in console
operations. This is achieved via AST transformations. The code is rewritten at compile time to transform the console
operations into proper logging commands. In addition to the transformation, class name and line number are added to the log messages to provide additional context.
The supported operations are:
console.fatal
which logs at theFATAL
levelconsole.error
which logs at theERROR
levelconsole.warn
which logs at theWARN
levelconsole.info
which logs at theINFO
levelconsole.debug
which logs at theDEBUG
levelconsole.trace
which logs at theTRACE
levelconsole.log
which logs at theINFO
level
Note: In production mode, all console.debug
and console.trace
invocations are compiled away for performance/security reasons. This means that the code is actually removed, and will not execute.
A sample of the transformation would be:
function work() {
console.trace('Start Work');
...
try {
... bad invocation ...
} catch (e) {
console.error(e);
}
console.trace('End Work');
}
Which, when in dev mode transforms into:
function work() {
Logger.log('trace', 'Start Work');
try {
... bad invocation ...
} catch (e) {
Logger.log('error', e);
}
Logger.log('trace', 'End Work');
}
And when in prod mode transforms into:
function work() {
try {
... bad invocation ...
} catch (e) {
Logger.log('error', e);
}
}
The logging output, as indicated provides context for location of invocation. Given the file test/simple.ts
:
console.log('Hello World');
console.log('Woah!', { a: { b: { c: { d: 10 } } } });
console.info('Woah!');
console.debug('Test');
console.fatal('hi');
The corresponding output would be
2018-06-23T16:57:58 info [test.simple: 5] Hello World
2018-06-23T16:57:58 info [test.simple: 7] Woah! { a: { b: { c: [Object] } } }
2018-06-23T16:57:58 info [test.simple: 9] Woah!
2018-06-23T16:57:58 debug [test.simple: 11] Test
2018-06-23T16:57:58 fatal [test.simple: 13] hi