{"id":13526854,"url":"https://github.com/starak/node-console-stamp","last_synced_at":"2025-04-01T08:30:28.873Z","repository":{"id":6908315,"uuid":"8158679","full_name":"starak/node-console-stamp","owner":"starak","description":"Patch NodeJS console methods in order to add timestamp information by pattern","archived":false,"fork":false,"pushed_at":"2024-06-19T07:21:29.000Z","size":946,"stargazers_count":172,"open_issues_count":2,"forks_count":20,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-21T09:36:08.882Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-02-12T12:52:33.000Z","updated_at":"2025-03-20T08:51:36.000Z","dependencies_parsed_at":"2024-04-15T20:24:05.311Z","dependency_job_id":"aa8cca27-3b20-4017-8a5f-ceb5fde2554b","html_url":"https://github.com/starak/node-console-stamp","commit_stats":{"total_commits":129,"total_committers":16,"mean_commits":8.0625,"dds":0.3643410852713178,"last_synced_commit":"eadc3836781e29937085acd5aa87f9514653a0a1"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starak%2Fnode-console-stamp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starak%2Fnode-console-stamp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starak%2Fnode-console-stamp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starak%2Fnode-console-stamp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starak","download_url":"https://codeload.github.com/starak/node-console-stamp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246580455,"owners_count":20800106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:36.015Z","updated_at":"2025-04-01T08:30:28.380Z","avatar_url":"https://github.com/starak.png","language":"JavaScript","readme":"# Console-stamp 3\n\n[![npm][npm-image]][npm-url]\n[![Downloads][downloads-image]][npm-url]\n[![Build Status][build-img]][build-url]\n\n[npm-url]: https://npmjs.org/package/console-stamp\n[build-url]: https://github.com/starak/node-console-stamp/actions\n[npm-image]: https://img.shields.io/npm/v/console-stamp.svg?style=flat-square\n[build-img]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fstarak%2Fnode-console-stamp%2Fbadge%3Fref%3Dmain\u0026style=flat-square\n[downloads-image]: https://img.shields.io/npm/dm/console-stamp.svg?style=flat-square\n\nThis 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.\n\n## Usage ##\n\n### Install\n```console\nnpm install console-stamp\n```\n\n### Patching the console\n\nYou need to provide the console object to `console-stamp` in order to patch the builtin console.\n\n```js\nrequire( 'console-stamp' )( console );\n\nconsole.log('Hello, World!');\n```\nThe default behaviour is to add a prefix to each log statement with timestamp information and log level.\n```terminal\n[10.02.2019 15:37:43.452] [LOG]   Hello, World!\n```\n\nYou can change this by provinding an [options](#options) object as the second parameter.\n\n```js\nrequire('console-stamp')(console, { \n    format: ':date(yyyy/mm/dd HH:MM:ss.l)' \n} );\n\nconsole.log('Hello, World!');\n```\n\n```terminal\n[2020/01/19 13:56:49.383] Hello, World!\n```\n\nNotice how the log level is suddenly missing. You need to add it specifically to the format string.\n\n```js\nrequire('console-stamp')(console, { \n    format: ':date(yyyy/mm/dd HH:MM:ss.l) :label' \n} );\n\nconsole.log('Hello, World!');\n```\n\n```terminal\n[2020/01/19 23:20:30.371] [LOG] Hello, World!\n```\n\n[Read more](#configuration) about how to customize the formatting of the log statement below.\n\n\u003ca name=\"customconsole\"\u003e\u003c/a\u003e\n### Patch your own console\n\nYou can also provide a custom console with its own `stdout` and `stderr` like this:\n\n```js\nconst fs = require('fs');\nconst output = fs.createWriteStream('./stdout.log');\nconst errorOutput = fs.createWriteStream('./stderr.log');\nconst logger = new console.Console(output, errorOutput);\n\nrequire('console-stamp')(logger, {\n    stdout: output,\n    stderr: errorOutput\n});\n```\n\nEverything is then written to the files.\n\n**NOTE:** If `stderr` isn't passed, warning and error output will be sent to the given `stdout`.\n\n### Backwards incompatibility with 2.x versions\n\n`console-stamp` v3 has been rewritten adding [tokens](#tokens) as a new and easier way to customize and extend your logging output.\n\nWith that in mind, some consessions has been made and you will probably need to update your `console-stamp` integration.\n\n#### `options.pattern` is replaced by `options.format`\n\n`options.format` is now the place where you provide the format of the logging prefix using [tokens](#tokens).\n\nFor example, `{ pattern: 'dd.mm.yyyy HH:MM:ss.l'}` is replaced by `{ format: ':date(dd.mm.yyyy HH:MM:ss.l)' }`.\n\nPS: Providing a string with a date format based on [dateformat](https://www.npmjs.com/package/dateformat) as a second parameter is still supported. \n\n#### `options.label` is gone\n\nThe 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.\n\n`options.labelSuffix` and `options.labelPrefix` are also gone as now you can provide these values directly in the `options.format` string.\n\n\u003ca name=\"configuration\"\u003e\u003c/a\u003e\n### Configuration\n\nHere are some examples on how to customize your log statements with `console-stamp`.\n\n#### Only update timestamp format\n\nWithout any other customizations you can provide the timestamp format directly.\n\n```js\nrequire('console-stamp')( console, 'yyyy/mm/dd HH:MM:ss.l' );\n```\nTo set the timestamp format using the [options](#options) object you can use the `date` token.\n\n```js\nrequire('console-stamp')(console, { \n    format: ':date(yyyy/mm/dd HH:MM:ss.l)' \n} );\n\nconsole.log('Hello, World!');\n```\n\n```\n[2020/01/19 23:08:39.202] Hello, World!\n```\n\n#### Add coloured output\n\n`console-stamp` uses the excellent [chalk](https://www.npmjs.com/package/chalk) library to provide coloured output and other styling.\n\n```js\nrequire( 'console-stamp' )( console, {\n    format: ':date().blue.bgWhite.underline :label(7)'\n} );\n```\nYou can also simply place some text in parenthesis, and then add your styling to that.\n\n```js\nrequire( 'console-stamp' )( console, {\n    format: '(-\u003e).yellow :date().blue.bgWhite.underline :label(7)'\n} );\n```\n\n**Note** that by sending the parameter `--no-color` when you start your node app, will prevent any colors from console.\n```console\n$ node my-app.js --no-color\n```\nFor more examples on styling, check out the [chalk](https://www.npmjs.com/package/chalk) documentation.\n\n\n\u003ca name=\"tokens\"\u003e\u003c/a\u003e\n### Tokens\n\nThere are only three predefined tokens registered by default. These are:\n\n    :date([format][,utc])[.color]\n    :label([padding])[.color]\n    :msg[.color]\n\n**:date([format][,utc])**\n* **format** {String}\u003cbr\u003e\n    Containing the date format based on [dateformat](https://www.npmjs.com/package/dateformat)\u003cbr\u003e\n    **Default**: 'dd.mm.yyyy HH:MM:ss.l' \n* **utc** {Boolean}\u003cbr\u003e\n    Set to `true` will return UTC-time \u003cbr\u003e\n    **Default**: false\n    \n**:label([padding])**\n* **padding** {Number}\u003cbr\u003e\n    The total length of the label, including the brackets and padding\u003cbr\u003e\n    **Default:** 7\n    \n**:msg**\n* 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.\n\n#### Create a custom token\nTo 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:\n\n```javascript\nrequire( 'console-stamp' )( console, {\n    format: ':foo() :label(7)',\n    tokens:{\n        foo: () =\u003e {\n            return '[my prefix]';\n        }\n    }\n} );\n\nconsole.log(\"Bar\");\n```\n```terminal\n[my prefix] [LOG]   Bar\n```\n\nThe token callback function is called with one argument, representing an Object with the following properties:\n* `method` {String} \u003cbr\u003e\n    The invoked method\n* `msg` {String} \u003cbr\u003e\n    The console output as a string\n* `params` {Array} \u003cbr\u003e\n    The token parameters (ex: The token call `:label(7)` will have params `[7]`)\n* `tokens` {Object} \u003cbr\u003e\n    All the defined tokens, incl. the defaults \n* `defaultTokens` {Object} \u003cbr\u003e\n    Only the default tokens, even if it's been redefined in options\n\n##### Example\nHere we are making a custom date token called `mydate` using moment.js to format the date\n```js\nconst moment = require('moment');\nmoment.locale('ja');\n\nrequire( 'console-stamp' )( console, {\n    format: ':mydate() :label(7)',\n    tokens:{\n        mydate: () =\u003e {\n            return `[${moment().format('LLLL')}]`;\n        }\n    }\n} );\n\nconsole.log('This is a console.log message');\nconsole.info('This is a console.info message');\nconsole.debug('This is a console.debug message');\nconsole.warn('This is a console.warn message');\nconsole.error('This is a console.error message');\n```\n\nResult:\n```terminal\n[2016年5月12日午前11時10分 木曜日] [LOG]   This is a console.log message\n[2016年5月12日午前11時10分 木曜日] [INFO]  This is a console.info message\n[2016年5月12日午前11時10分 木曜日] [DEBUG] This is a console.debug message\n[2016年5月12日午前11時10分 木曜日] [WARN]  This is a console.warn message\n[2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message\n```\n\n\n\u003ca name=\"custommethods\"\u003e\u003c/a\u003e\n### Custom Methods\n\nThe **option.extend** option enables the extension or modification of the logging methods and their associated log levels:\n\nThe default logging methods and their log levels are as follows:\n\n```js\nlevels = {\n    error: 1,\n    warn: 2,\n    info: 3,\n    log: 4,\n    debug: 4\n};\n```\n\nThe **extend** option enables the usage of custom console logging methods to be \nused with this module, for example:\n\n```js\n// Extending the console with a custom method\nconsole.fatal = function(msg) {\n    console.org.error(msg);\n    process.exit(1);\n}\n\n// Initialising the output formatter\nrequire( 'console-stamp' )( console, {\n    extend: {\n        fatal: 1\n    }\n} );\n```\n\n**Note** how the `console.org.error` method used in the custom method. This is to prevent circular calls to `console.error`\n\n-------------\n\n### API\n```js\nrequire( 'console-stamp' )( console, [options] );\n```\n\n#### console\nThe global console or [custom console](#customconsole).\n\n\u003ca name=\"options\"\u003e\u003c/a\u003e\n#### options {Object|String}\n\nThe second parameter is an object with several options. As a feature this parameter can be a string containing the date-format.\n\n* **options.format** {String}\u003cbr\u003eA string with date format based on [dateformat](https://www.npmjs.com/package/dateformat)\u003cbr\u003e\n    **Default**: ':date(dd.mm.yyyy HH:MM:ss.l) :label'\n\n* **options.tokens** {Object}\u003cbr\u003eContaining token-functions. See example [here](#tokens).\n\n* **options.include** {Array}\u003cbr\u003eAn array containing the methods to include in the patch\u003cbr\u003e\n    **Default**: [\"debug\", \"log\", \"info\", \"warn\", \"error\"]\n\n* **options.level** {String}\u003cbr\u003eA string choosing the most verbose logging function to allow.\u003cbr\u003e\n    **Default**: `log`\n\n* **options.extend** {Object}\u003cbr\u003eAn object describing methods and their associated log level, \n    to extend the existing `method \u003c-\u003e log level` pairs.\u003cbr\u003e\n    For an example see [Custom methods](#custommethods).\n\n* **options.stdout** {WritableStream}\u003cbr\u003eA custom `stdout` to use with [custom console](#customconsole).\u003cbr\u003e\n    **Default:** `process.stdout`\n\n* **options.stderr** {WritableStream}\u003cbr\u003eA custom `stderr` to use with [custom console](#customconsole).\u003cbr\u003e\n    **Default:** `options.stdout` or `process.stderr`\n    \n* **options.preventDefaultMessage** {Boolean}\u003cbr\u003eIf 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.\u003cbr\u003e**Default:** `false`\n\n","funding_links":[],"categories":["Repository"],"sub_categories":["Command-line Utilities"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarak%2Fnode-console-stamp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarak%2Fnode-console-stamp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarak%2Fnode-console-stamp/lists"}