Logging
SALVO-TS provides a standardized interface for logging in your project.
Do not use console.log and related methods in your code. Always use the SALVO logging functions.
The reason for this is because SALVO automatically suppresses most logging in production (and has features for re-enabling it for debugging - read on.) This helps keep things clean, but is also a technical requirement for some clients.
In components and pages
In components and pages you can log via this.$log:
When using this interface, log messages automatically indicate the name of the component or page that they were called in.
See the API docs for TaggedLog (which this.$log is an instance of) for detailed docs.
typescript// Log a simple message at various log levelsthis.$log.debug('Hello world!'); // Detailed info for debugging - hidden in productionthis.$log.info('Hello world!'); // General info - hidden in productionthis.$log.warning('Hello world!'); // Non-error warnings - shown in productionthis.$log.error('Hello world!'); // Errors! - shown in productionthis.$log.fatal('Hello world!'); // Errors which are likely to cause fundemental issues with the site - shown in production// All of the above also support passing an object as the second parameterthis.$log.debug('Hello world!', {foo: 'bar'});this.$log.info('Hello world!', {foo: 'bar'});this.$log.warning('Hello world!', {foo: 'bar'});this.$log.error('Hello world!', {foo: 'bar'});this.$log.fatal('Hello world!', {foo: 'bar'});// Timers (debug)this.$log.time('timer name'); // Starts a timer - disabled in productionthis.$log.timeLog('timer name'); // Log a timer's time - disabled in productionthis.$log.timeEnd('timer name'); // Stop a timer and log it's time - disabled in production// Other helper functionsthis.$log.table('Hello world!', [{foo: 'bar'}, {foo: 'baz'}]); // Similar to console.table() - debug - hidden in production
typescript// Log a simple message at various log levelsthis.$log.debug('Hello world!'); // Detailed info for debugging - hidden in productionthis.$log.info('Hello world!'); // General info - hidden in productionthis.$log.warning('Hello world!'); // Non-error warnings - shown in productionthis.$log.error('Hello world!'); // Errors! - shown in productionthis.$log.fatal('Hello world!'); // Errors which are likely to cause fundemental issues with the site - shown in production// All of the above also support passing an object as the second parameterthis.$log.debug('Hello world!', {foo: 'bar'});this.$log.info('Hello world!', {foo: 'bar'});this.$log.warning('Hello world!', {foo: 'bar'});this.$log.error('Hello world!', {foo: 'bar'});this.$log.fatal('Hello world!', {foo: 'bar'});// Timers (debug)this.$log.time('timer name'); // Starts a timer - disabled in productionthis.$log.timeLog('timer name'); // Log a timer's time - disabled in productionthis.$log.timeEnd('timer name'); // Stop a timer and log it's time - disabled in production// Other helper functionsthis.$log.table('Hello world!', [{foo: 'bar'}, {foo: 'baz'}]); // Similar to console.table() - debug - hidden in production
Elsewhere
Outside of component and page classes, you can get a reference to the general Logger by importing the log object.
Note that this interface is identical to the above, but takes an additional 'tag' parameter. The tag should be something that indicates where this log was called from - i.e. a class or file name.
typescriptimport {log} from 'salvo-ts'// Log a simple message at various log levelslog.debug('MyExample', 'Hello world!'); // Detailed info for debugging - hidden in productionlog.info('MyExample', 'Hello world!'); // General info - hidden in productionlog.warning('MyExample', 'Hello world!'); // Non-error warnings - shown in productionlog.error('MyExample', 'Hello world!'); // Errors! - shown in productionlog.fatal('MyExample', 'Hello world!'); // Errors which are likely to cause fundemental issues with the site - shown in production// All of the above also support passing an object as the second parameterlog.debug('MyExample', 'Hello world!', {foo: 'bar'});log.info('MyExample', 'Hello world!', {foo: 'bar'});log.warning('MyExample', 'Hello world!', {foo: 'bar'});log.error('MyExample', 'Hello world!', {foo: 'bar'});log.fatal('MyExample', 'Hello world!', {foo: 'bar'});// Timers ('MyExample', debug)log.time('MyExample', 'timer name'); // Starts a timer - disabled in productionlog.timeLog('MyExample', 'timer name'); // Log a timer's time - disabled in productionlog.timeEnd('MyExample', 'timer name'); // Stop a timer and log it's time - disabled in production// Other helper functionslog.table('MyExample', 'Hello world!', [{foo: 'bar'}, {foo: 'baz'}]); // Similar to console.table() - debug - hidden in production
typescriptimport {log} from 'salvo-ts'// Log a simple message at various log levelslog.debug('MyExample', 'Hello world!'); // Detailed info for debugging - hidden in productionlog.info('MyExample', 'Hello world!'); // General info - hidden in productionlog.warning('MyExample', 'Hello world!'); // Non-error warnings - shown in productionlog.error('MyExample', 'Hello world!'); // Errors! - shown in productionlog.fatal('MyExample', 'Hello world!'); // Errors which are likely to cause fundemental issues with the site - shown in production// All of the above also support passing an object as the second parameterlog.debug('MyExample', 'Hello world!', {foo: 'bar'});log.info('MyExample', 'Hello world!', {foo: 'bar'});log.warning('MyExample', 'Hello world!', {foo: 'bar'});log.error('MyExample', 'Hello world!', {foo: 'bar'});log.fatal('MyExample', 'Hello world!', {foo: 'bar'});// Timers ('MyExample', debug)log.time('MyExample', 'timer name'); // Starts a timer - disabled in productionlog.timeLog('MyExample', 'timer name'); // Log a timer's time - disabled in productionlog.timeEnd('MyExample', 'timer name'); // Stop a timer and log it's time - disabled in production// Other helper functionslog.table('MyExample', 'Hello world!', [{foo: 'bar'}, {foo: 'baz'}]); // Similar to console.table() - debug - hidden in production
Enabling full logging in production
A theme that has been built in production mode will not include most logging by default.
You can enable logging in your browser by running the following in your console:
typescript__SALVO__.overrideLogLevel('DEBUG');
typescript__SALVO__.overrideLogLevel('DEBUG');
This setting will persist until your localStorage is cleared, so feel free to reload the page.
If you would like to clear this, run the following to reset:
typescript__SALVO__.overrideLogLevel(null);
typescript__SALVO__.overrideLogLevel(null);