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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Console-stamp 3
  2. [![npm][npm-image]][npm-url]
  3. [![downloads][downloads-image]][npm-url]
  4. [![build][build-img]][npm-url]
  5. [npm-image]: https://img.shields.io/npm/v/console-stamp.svg?style=flat-square
  6. [npm-url]: https://npmjs.org/package/console-stamp
  7. [build-img]: https://img.shields.io/circleci/project/github/starak/node-console-stamp/3.0.0.svg?style=flat-square
  8. [downloads-image]: https://img.shields.io/npm/dm/console-stamp.svg?style=flat-square
  9. This module lets you take control over the output from `console` logging methods in Node.js. Such as prefixing the log statement with timestamp information, log levels, add coloured output and much more.
  10. ## Usage ##
  11. ### Install
  12. ```console
  13. npm install console-stamp
  14. ```
  15. ### Patching the console
  16. You need to provide the console object to `console-stamp` in order to patch the builtin console.
  17. ```js
  18. require( 'console-stamp' )( console );
  19. console.log('Hello, World!');
  20. ```
  21. The default behaviour is to add a prefix to each log statement with timestamp information and log level.
  22. ```terminal
  23. [10.02.2019 15:37:43.452] [LOG] Hello, World!
  24. ```
  25. You can change this by provinding an [options](#options) object as the second parameter.
  26. ```js
  27. require('console-stamp')(console, {
  28. format: ':date(yyyy/mm/dd HH:MM:ss.l)'
  29. } );
  30. console.log('Hello, World!');
  31. ```
  32. ```terminal
  33. [2020/01/19 13:56:49.383] Hello, World!
  34. ```
  35. Notice how the log level is suddenly missing. You need to add it specifically to the format string.
  36. ```js
  37. require('console-stamp')(console, {
  38. format: ':date(yyyy/mm/dd HH:MM:ss.l) :label'
  39. } );
  40. console.log('Hello, World!');
  41. ```
  42. ```terminal
  43. [2020/01/19 23:20:30.371] [LOG] Hello, World!
  44. ```
  45. [Read more](#configuration) about how to customize the formatting of the log statement below.
  46. <a name="customconsole"></a>
  47. ### Patch your own console
  48. You can also provide a custom console with its own `stdout` and `stderr` like this:
  49. ```js
  50. const fs = require('fs');
  51. const output = fs.createWriteStream('./stdout.log');
  52. const errorOutput = fs.createWriteStream('./stderr.log');
  53. const logger = new console.Console(output, errorOutput);
  54. require('console-stamp')(logger, {
  55. stdout: output,
  56. stderr: errorOutput
  57. });
  58. ```
  59. Everything is then written to the files.
  60. **NOTE:** If `stderr` isn't passed, warning and error output will be sent to the given `stdout`.
  61. ### Backwards incompatibility with 2.x versions
  62. `console-stamp` v3 has been rewritten adding [tokens](#tokens) as a new and easier way to customize and extend your logging output.
  63. With that in mind, some consessions has been made and you will probably need to update your `console-stamp` integration.
  64. #### `options.pattern` is replaced by `options.format`
  65. `options.format` is now the place where you provide the format of the logging prefix using [tokens](#tokens).
  66. For example, `{ pattern: 'dd.mm.yyyy HH:MM:ss.l'}` is replaced by `{ format: ':date(dd.mm.yyyy HH:MM:ss.l)' }`.
  67. PS: Providing a string with a date format based on [dateformat](https://www.npmjs.com/package/dateformat) as a second parameter is still supported.
  68. #### `options.label` is gone
  69. The log level label (INFO, DEBUG etc.) is now only shown if the token `:label` is part of the format string in `options.format`. It is part of the default format.
  70. `options.labelSuffix` and `options.labelPrefix` are also gone as now you can provide these values directly in the `options.format` string.
  71. <a name="configuration"></a>
  72. ### Configuration
  73. Here are some examples on how to customize your log statements with `console-stamp`.
  74. #### Only update timestamp format
  75. Without any other customizations you can provide the timestamp format directly.
  76. ```js
  77. require('console-stamp')( console, 'yyyy/mm/dd HH:MM:ss.l' );
  78. ```
  79. To set the timestamp format using the [options](#options) object you can use the `date` token.
  80. ```js
  81. require('console-stamp')(console, {
  82. format: ':date(yyyy/mm/dd HH:MM:ss.l)'
  83. } );
  84. console.log('Hello, World!');
  85. ```
  86. ```
  87. [2020/01/19 23:08:39.202] Hello, World!
  88. ```
  89. #### Add coloured output
  90. `console-stamp` uses the excellent [chalk](https://www.npmjs.com/package/chalk) library to provide coloured output and other styling.
  91. ```js
  92. require( 'console-stamp' )( console, {
  93. format: ':date().blue.bgWhite.underline :label(7)'
  94. } );
  95. ```
  96. You can also simply place some text in parenthesis, and then add your styling to that.
  97. ```js
  98. require( 'console-stamp' )( console, {
  99. format: '(->).yellow :date().blue.bgWhite.underline :label(7)'
  100. } );
  101. ```
  102. **Note** that by sending the parameter `--no-color` when you start your node app, will prevent any colors from console.
  103. ```console
  104. $ node my-app.js --no-color
  105. ```
  106. For more examples on styling, check out the [chalk](https://www.npmjs.com/package/chalk) documentation.
  107. <a name="tokens"></a>
  108. ### Tokens
  109. There are only three predefined tokens registered by default. These are:
  110. :date([format][,utc])[.color]
  111. :label([padding])[.color]
  112. :msg[.color]
  113. **:date([format][,utc])**
  114. * **format** {String}<br>
  115. Containing the date format based on [dateformat](https://www.npmjs.com/package/dateformat)<br>
  116. **Default**: 'dd.mm.yyyy HH:MM:ss.l'
  117. * **utc** {Boolean}<br>
  118. Set to `true` will return UTC-time <br>
  119. **Default**: false
  120. **:label([padding])**
  121. * **padding** {Number}<br>
  122. The total length of the label, including the brackets and padding<br>
  123. **Default:** 7
  124. **:msg**
  125. * If the `:msg` token is provided in `format`, the output from the console will be returned in its place, otherwise the console output will be added as the last output, with no formatting.
  126. #### Create a custom token
  127. To define your own token, simply add a callback function with the token name to the tokens option. This callback function is expected to return a string. The value returned is then available as ":foo()" in this case:
  128. ```javascript
  129. require( 'console-stamp' )( console, {
  130. format: ':foo() :label(7)',
  131. tokens:{
  132. foo: () => {
  133. return '[my prefix]';
  134. }
  135. }
  136. } );
  137. console.log("Bar");
  138. ```
  139. ```terminal
  140. [my prefix] [LOG] Bar
  141. ```
  142. The token callback function is called with one argument, representing an Object with the following properties:
  143. * `method` {String} <br>
  144. The invoked method
  145. * `msg` {String} <br>
  146. The console output as a string
  147. * `params` {Array} <br>
  148. The token parameters (ex: The token call `:label(7)` will have params `[7]`)
  149. * `tokens` {Object} <br>
  150. All the defined tokens, incl. the defaults
  151. * `defaultTokens` {Object} <br>
  152. Only the default tokens, even if it's been redefined in options
  153. ##### Example
  154. Here we are making a custom date token called `mydate` using moment.js to format the date
  155. ```js
  156. const moment = require('moment');
  157. moment.locale('ja');
  158. require( 'console-stamp' )( console, {
  159. format: ':mydate() :label(7)',
  160. tokens:{
  161. mydate: () => {
  162. return `[${moment().format('LLLL')}]`;
  163. }
  164. }
  165. } );
  166. console.log('This is a console.log message');
  167. console.info('This is a console.info message');
  168. console.debug('This is a console.debug message');
  169. console.warn('This is a console.warn message');
  170. console.error('This is a console.error message');
  171. ```
  172. Result:
  173. ```terminal
  174. [2016年5月12日午前11時10分 木曜日] [LOG] This is a console.log message
  175. [2016年5月12日午前11時10分 木曜日] [INFO] This is a console.info message
  176. [2016年5月12日午前11時10分 木曜日] [DEBUG] This is a console.debug message
  177. [2016年5月12日午前11時10分 木曜日] [WARN] This is a console.warn message
  178. [2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message
  179. ```
  180. <a name="custommethods"></a>
  181. ### Custom Methods
  182. The **option.extend** option enables the extension or modification of the logging methods and their associated log levels:
  183. The default logging methods and their log levels are as follows:
  184. ```js
  185. levels = {
  186. error: 1,
  187. warn: 2,
  188. info: 3,
  189. log: 4,
  190. debug: 4
  191. };
  192. ```
  193. The **extend** option enables the usage of custom console logging methods to be
  194. used with this module, for example:
  195. ```js
  196. // Extending the console with a custom method
  197. console.fatal = function(msg) {
  198. console.org.error(msg);
  199. process.exit(1);
  200. }
  201. // Initialising the output formatter
  202. require( 'console-stamp' )( console, {
  203. extend: {
  204. fatal: 1
  205. }
  206. } );
  207. ```
  208. **Note** how the `console.org.error` method used in the custom method. This is to prevent circular calls to `console.error`
  209. -------------
  210. ### API
  211. ```js
  212. require( 'console-stamp' )( console, [options] );
  213. ```
  214. #### console
  215. The global console or [custom console](#customconsole).
  216. <a name="options"></a>
  217. #### options {Object|String}
  218. The second parameter is an object with several options. As a feature this parameter can be a string containing the date-format.
  219. * **options.format** {String}<br>A string with date format based on [dateformat](https://www.npmjs.com/package/dateformat)<br>
  220. **Default**: ':date(dd.mm.yyyy HH:MM:ss.l) :label'
  221. * **options.tokens** {Object}<br>Containing token-functions. See example [here](#tokens).
  222. * **options.include** {Array}<br>An array containing the methods to include in the patch<br>
  223. **Default**: ["debug", "log", "info", "warn", "error"]
  224. * **options.level** {String}<br>A string choosing the most verbose logging function to allow.<br>
  225. **Default**: `log`
  226. * **options.extend** {Object}<br>An object describing methods and their associated log level,
  227. to extend the existing `method <-> log level` pairs.<br>
  228. For an example see [Custom methods](#custommethods).
  229. * **options.stdout** {WritableStream}<br>A custom `stdout` to use with [custom console](#customconsole).<br>
  230. **Default:** `process.stdout`
  231. * **options.stderr** {WritableStream}<br>A custom `stderr` to use with [custom console](#customconsole).<br>
  232. **Default:** `options.stdout` or `process.stderr`
  233. * **options.preventDefaultMessage** {Boolean}<br>If set to `true` Console-stamp will not print out the standard output from the console. This can be used in combination with a custom message token.<br>**Default:** `false`