All files LoggerWrapper.js

100% Statements 12/12
100% Branches 4/4
100% Functions 9/9
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                              9x                           7x     1x         6x                   1x                   1x                   1x                   1x                   1x                   1x                     1x       1x  
'use strict';
 
/**
 * This is a wrapper for the logger provided by service runner.
 * It provides more user-friendly logging APIs and better error
 * signaling for when it is used incorrectly.
 *
 * This is the logger that other scripts in this project will interact with.
 * Usage:
 * const logger = new LoggerWrapper( <somelogger> );
 * logger.log('warn', 'hello this is a message');
 * logger.warn('hello this is also a message');
 */
class LoggerWrapper {
	constructor( logger ) {
		this._logger = logger;
	}
 
	/**
	 * Logs a message on a given severity level.
	 * Acceptable levels: trace, debug, info, warn, error, and fatal.
	 *
	 * @param {string} level Severity level and components.
	 *     Level options: trace, debug, info, warn, error, and fatal.
	 *     E.g. trace or trace/request.
	 * @param {string} message A string message for the log.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	log( level, message, data ) {
		if ( !level || !message ) {
			// The service runner implementation will just silently no-op
			// in this situation. We want to alert the caller here.
			throw new Error(
				`Incorrect usage of the logger. Both arguments need to be
				present. E.g. logger.log(level, message). Alternatively you can
				use the logger.level() API.` );
		}
		this._logger.log( level, message, data );
	}
 
	/**
	 * Logs a tracing message.
	 *
	 * @param {string} message Trace message.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	trace( message, data ) {
		this.log( 'trace', message, data );
	}
 
	/**
	 * Logs a debug message.
	 *
	 * @param {string} message Debug message.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	debug( message, data ) {
		this.log( 'debug', message, data );
	}
 
	/**
	 * Logs a info message.
	 *
	 * @param {string} message Info message.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	info( message, data ) {
		this.log( 'info', message, data );
	}
 
	/**
	 * Logs a warning message.
	 *
	 * @param {string} message Warning message.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	warn( message, data ) {
		this.log( 'warn', message, data );
	}
 
	/**
	 * Logs a error message.
	 *
	 * @param {string} message Error message.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	error( message, data ) {
		this.log( 'error', message, data );
	}
 
	/**
	 * Logs a fatal message.
	 *
	 * @param {string} message Fatal message.
	 * @param {Object} data Any relevant data for the log entry.
	 */
	fatal( message, data ) {
		this.log( 'fatal', message, data );
	}
 
	/**
	 * Creates a child logger for a sub-component of your application.
	 * This directly wraps its core logger obj's implementation.
	 *
	 * @param {*} args arguments for the child wrapper.
	 * @return {LoggerWrapper} A new logger for the sub-component.
	 */
	child( args ) {
		return new LoggerWrapper( this._logger.child( args ) );
	}
}
 
module.exports = { LoggerWrapper };