Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # loglevel-plugin-prefix
  2. Plugin for [loglevel](https://github.com/pimterry/loglevel) message prefixing.
  3. [![NPM version](https://img.shields.io/npm/v/loglevel-plugin-prefix.svg?style=flat-square)](https://www.npmjs.com/package/loglevel-plugin-prefix)[![Build Status](https://img.shields.io/travis/kutuluk/loglevel-plugin-prefix/master.svg?style=flat-square)](https://travis-ci.org/kutuluk/loglevel-plugin-prefix)
  4. ## Installation
  5. ```sh
  6. npm install loglevel-plugin-prefix
  7. ```
  8. ## API
  9. **This plugin is under active development and should be considered as an unstable. No guarantees regarding API stability are made. Backward compatibility is guaranteed only by path releases.**
  10. #### `reg(loglevel)`
  11. This method registers plugin for loglevel. This method must be called at least once before any call to the apply method. Repeated calls to this method are ignored.
  12. #### Parameters
  13. `loglevel` - the root logger, imported from loglevel module
  14. #### `apply(logger, options)`
  15. This method applies the plugin to the logger. Before using this method, the `reg` method must be called, otherwise a warning will be logged. **From the next release, the call apply before reg will throw an error.**
  16. #### Parameters
  17. `logger` - any logger of loglevel
  18. `options` - an optional configuration object
  19. ```javascript
  20. var defaults = {
  21. template: '[%t] %l:',
  22. levelFormatter: function (level) {
  23. return level.toUpperCase();
  24. },
  25. nameFormatter: function (name) {
  26. return name || 'root';
  27. },
  28. timestampFormatter: function (date) {
  29. return date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1');
  30. },
  31. format: undefined
  32. };
  33. ```
  34. Plugin formats the prefix using `template` option as a printf-like format. The `template` is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the value from loglevel messages parameters. Supported placeholders are:
  35. - `%l` - level of message
  36. - `%n` - name of logger
  37. - `%t` - timestamp of message
  38. The `levelFormatter`, `nameFormatter` and `timestampFormatter` is a functions for formatting corresponding values.
  39. Alternatively, you can use `format` option. This is a function that receives formatted values (level, name, timestamp) and should returns a prefix string.
  40. If both `format` and` template` are present in the configuration, the `template` parameter is ignored. When both these parameters are missing in the configuration, the inherited behavior is used.
  41. ## Usage
  42. ### Browser directly
  43. ```html
  44. <script src="https://unpkg.com/loglevel/dist/loglevel.min.js"></script>
  45. <script src="https://unpkg.com/loglevel-plugin-prefix@^0.8/dist/loglevel-plugin-prefix.min.js"></script>
  46. <script>
  47. var logger = log.noConflict();
  48. var prefixer = prefix.noConflict();
  49. prefixer.reg(logger);
  50. prefixer.apply(logger);
  51. logger.warn('prefixed message');
  52. </script>
  53. ```
  54. Output
  55. ```
  56. [16:53:46] WARN: prefixed message
  57. ```
  58. ### Node
  59. ```javascript
  60. const chalk = require('chalk');
  61. const log = require('loglevel');
  62. const prefix = require('loglevel-plugin-prefix');
  63. const colors = {
  64. TRACE: chalk.magenta,
  65. DEBUG: chalk.cyan,
  66. INFO: chalk.blue,
  67. WARN: chalk.yellow,
  68. ERROR: chalk.red,
  69. };
  70. prefix.reg(log);
  71. log.enableAll();
  72. prefix.apply(log, {
  73. format(level, name, timestamp) {
  74. return `${chalk.gray(`[${timestamp}]`)} ${colors[level.toUpperCase()](level)} ${chalk.green(`${name}:`)}`;
  75. },
  76. });
  77. prefix.apply(log.getLogger('critical'), {
  78. format(level, name, timestamp) {
  79. return chalk.red.bold(`[${timestamp}] ${level} ${name}:`);
  80. },
  81. });
  82. log.trace('trace');
  83. log.debug('debug');
  84. log.getLogger('critical').info('Something significant happened');
  85. log.log('log');
  86. log.info('info');
  87. log.warn('warn');
  88. log.error('error');
  89. ```
  90. Output
  91. ![output](https://raw.githubusercontent.com/kutuluk/loglevel-plugin-prefix/master/colored.png "output")
  92. ## Custom options
  93. ```javascript
  94. const log = require('loglevel');
  95. const prefix = require('loglevel-plugin-prefix');
  96. prefix.reg(log);
  97. log.enableAll();
  98. prefix.apply(log, {
  99. template: '[%t] %l (%n) static text:',
  100. levelFormatter(level) {
  101. return level.toUpperCase();
  102. },
  103. nameFormatter(name) {
  104. return name || 'global';
  105. },
  106. timestampFormatter(date) {
  107. return date.toISOString();
  108. },
  109. });
  110. log.info('%s prefix', 'template');
  111. const fn = (level, name, timestamp) => `[${timestamp}] ${level} (${name}) static text:`;
  112. prefix.apply(log, { format: fn });
  113. log.info('%s prefix', 'functional');
  114. prefix.apply(log, { template: '[%t] %l (%n) static text:' });
  115. log.info('again %s prefix', 'template');
  116. ```
  117. Output
  118. ```
  119. [2017-05-29T12:53:46.000Z] INFO (global) static text: template prefix
  120. [2017-05-29T12:53:46.000Z] INFO (global) static text: functional prefix
  121. [2017-05-29T12:53:46.000Z] INFO (global) static text: again template prefix
  122. ```
  123. ## Option inheritance
  124. ```javascript
  125. const log = require('loglevel');
  126. const prefix = require('loglevel-plugin-prefix');
  127. prefix.reg(log);
  128. log.enableAll();
  129. log.info('root');
  130. const chicken = log.getLogger('chicken');
  131. chicken.info('chicken');
  132. prefix.apply(chicken, { template: '%l (%n):' });
  133. chicken.info('chicken');
  134. prefix.apply(log);
  135. log.info('root');
  136. const egg = log.getLogger('egg');
  137. egg.info('egg');
  138. const fn = (level, name) => `${level} (${name}):`;
  139. prefix.apply(egg, { format: fn });
  140. egg.info('egg');
  141. prefix.apply(egg, {
  142. levelFormatter(level) {
  143. return level.toLowerCase();
  144. },
  145. });
  146. egg.info('egg');
  147. chicken.info('chicken');
  148. log.info('root');
  149. ```
  150. Output
  151. ```
  152. root
  153. chicken
  154. INFO (chicken): chicken
  155. [16:53:46] INFO: root
  156. [16:53:46] INFO: egg
  157. INFO (egg): egg
  158. info (egg): egg
  159. INFO (chicken): chicken
  160. [16:53:46] INFO: root
  161. ```