plug_log.js

  1. // Modifications copyright 2020 Caf.js Labs and contributors
  2. /*!
  3. Copyright 2013 Hewlett-Packard Development Company, L.P.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. 'use strict';
  15. /**
  16. * A logger component.
  17. *
  18. * Properties:
  19. *
  20. * {logLevel: string}
  21. *
  22. * where `logLevel` sets the threshold for logging as one of
  23. * `OFF`, `FATAL`, `ERROR`, `WARN`, `INFO`, `DEBUG` and `TRACE` (in that order)
  24. *
  25. * @module caf_components/plug_log
  26. * @augments module:caf_components/gen_plug
  27. *
  28. */
  29. // @ts-ignore: augments not attached to a class
  30. const assert = require('assert');
  31. const genPlug = require('./gen_plug');
  32. /**
  33. * Factory method to create a log plug component.
  34. *
  35. * @param {ctxType} $ A context containing references to other components.
  36. * @param {specType} spec Configuration data for this component.
  37. *
  38. * @return {Promise<Array.<any>>} A tuple array returning an optional `Error`
  39. * in the first argument, or the new component in the second.
  40. */
  41. exports.newInstance = async function($, spec) {
  42. try {
  43. const levels = {
  44. 'OFF': -1, 'FATAL': 0, 'ERROR': 1, 'WARN': 2, 'INFO': 3, 'DEBUG': 4,
  45. 'TRACE': 5
  46. };
  47. const levelNames = [
  48. 'OFF', 'FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
  49. ];
  50. const that = genPlug.create($, spec);
  51. assert.equal(typeof(spec.env.logLevel), 'string',
  52. "'logLevel' is not an string");
  53. var currentLevel = levels[spec.env.logLevel];
  54. assert.equal(typeof(currentLevel), 'number',
  55. "'currentLevel' is not a number");
  56. /**
  57. * Sets the threshold for logging events.
  58. *
  59. * @param {string} newLogLevel A new logging level.
  60. * @return {string} The previous logging level.
  61. *
  62. * @memberof! module:caf_components/plug_log#
  63. * @alias setLevel
  64. *
  65. */
  66. that.setLevel = function(newLogLevel) {
  67. assert.equal(typeof(newLogLevel), 'string',
  68. "'newLogLevel' is not a string");
  69. var newLevel = levels[newLogLevel];
  70. assert.equal(typeof(newLevel), 'number',
  71. "'newLevel' is not a number");
  72. const oldLevel = currentLevel;
  73. currentLevel = newLevel;
  74. return levelNames[oldLevel+1];
  75. };
  76. /**
  77. * Gets the current threshold for logging events.
  78. *
  79. * @return {string} The current threshold for logging events.
  80. *
  81. * @memberof! module:caf_components/plug_log#
  82. * @alias currentLevel
  83. */
  84. that.currentLevel = function() {
  85. return levelNames[currentLevel+1];
  86. };
  87. /**
  88. * Checks if a candidate level would log with current settings.
  89. *
  90. * @param {string} candidateLevel A candidate level.
  91. * @return {boolean} True if that level is logged.
  92. *
  93. * @memberof! module:caf_components/plug_log#
  94. * @alias isActive
  95. *
  96. */
  97. that.isActive = function(candidateLevel) {
  98. const candidate = (candidateLevel && levels[candidateLevel]);
  99. assert.equal(typeof(candidate), 'number',
  100. "'candidate' is not a number");
  101. return (candidate <= currentLevel);
  102. };
  103. const log = function(level, msg) {
  104. /* eslint-disable */
  105. (level <= currentLevel) && console.log(msg);
  106. /* eslint-enable */
  107. };
  108. /**
  109. * Logs msg at FATAL level.
  110. *
  111. * @param {string} msg A message to be logged.
  112. *
  113. * @memberof! module:caf_components/plug_log#
  114. * @alias fatal
  115. *
  116. */
  117. that.fatal = function(msg) {
  118. log(0, msg);
  119. };
  120. /**
  121. * Logs msg at ERROR level.
  122. *
  123. * @param {string} msg A message to be logged.
  124. *
  125. * @memberof! module:caf_components/plug_log#
  126. * @alias error
  127. *
  128. */
  129. that.error = function(msg) {
  130. log(1, msg);
  131. };
  132. /**
  133. * Logs msg at WARN level.
  134. *
  135. * @param {string} msg A message to be logged.
  136. *
  137. * @memberof! module:caf_components/plug_log#
  138. * @alias warn
  139. *
  140. */
  141. that.warn = function(msg) {
  142. log(2, msg);
  143. };
  144. /**
  145. * Logs msg at INFO level.
  146. *
  147. * @param {string} msg A message to be logged.
  148. *
  149. * @memberof! module:caf_components/plug_log#
  150. * @alias info
  151. *
  152. */
  153. that.info = function(msg) {
  154. log(3, msg);
  155. };
  156. /**
  157. * Logs msg at DEBUG level.
  158. *
  159. * @param {string} msg A message to be logged.
  160. *
  161. * @memberof! module:caf_components/plug_log#
  162. * @alias debug
  163. *
  164. */
  165. that.debug = function(msg) {
  166. log(4, msg);
  167. };
  168. /**
  169. * Logs msg at TRACE level.
  170. *
  171. * @param {string} msg A message to be logged.
  172. *
  173. * @memberof! module:caf_components/plug_log#
  174. * @alias trace
  175. *
  176. */
  177. that.trace = function(msg) {
  178. log(5, msg);
  179. };
  180. that.debug('New logger plug');
  181. // loader is created first but needs to log messages
  182. $._.$.loader && $._.$.loader.__ca_registerLogger__(that);
  183. return [null, that];
  184. } catch (err) {
  185. return [err];
  186. }
  187. };