{"name":"loglevel-plugin-prefix","description":"Plugin for loglevel message prefixing","author":{"name":"kutuluk"},"keywords":["log","logger","logging","browser","node","prefix","colored","loglevel","plugin"],"homepage":"https://github.com/kutuluk/loglevel-plugin-prefix","bugs":{"url":"https://github.com/kutuluk/loglevel-plugin-prefix/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/kutuluk/loglevel-plugin-prefix.git"},"license":"MIT","version":"0.8.0","main":"./lib/loglevel-plugin-prefix","scripts":{"eslint":"eslint ./lib/loglevel-plugin-prefix.js","test":"mocha","example":"node ./example/example.js","uglifyjs":"uglifyjs ./lib/loglevel-plugin-prefix.js -o ./dist/loglevel-plugin-prefix.min.js -c -m","build":"npm install && npm run eslint && npm run test && npm run uglifyjs"},"devDependencies":{"chai":"^4.1.2","chalk":"^2.3.0","eslint":"^3.19.0","eslint-config-airbnb-base":"^11.3.2","eslint-plugin-chai-friendly":"^0.3.6","eslint-plugin-import":"^2.8.0","loglevel":"^1.6.1","loglevel-plugin-mock":"^0.1.0","mocha":"^3.5.3","sinon":"^2.4.1","uglify-js":"^3.3.5"},"gitHead":"161686c95f5536705f9a6e748b1ec04621d764da","_id":"loglevel-plugin-prefix@0.8.0","_npmVersion":"5.6.0","_nodeVersion":"8.9.1","_npmUser":{"name":"kutuluk","email":"blooderast@gmail.com"},"dist":{"integrity":"sha512-lDxYA0UHX1O3E5TAnM2gzZmLGSNDI+rZUUwg5NWohMFiZBas2ZhQd1+Y2mJ6ADkLT2RNsS4zVc7+gi9WNDvfnQ==","shasum":"f90ee8e8583a19016f54f25144ade66b612bc43b","tarball":"http://registry-npm.ywwl.com/loglevel-plugin-prefix/download/loglevel-plugin-prefix-0.8.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDGRWDLiGRYEY5KyBL0cYB0d/jwL9Kyhi44meSKUNn5TgIhAKXsxECXQfWo0q0LRw51YnE+Bao4s3sCQvPx1sFTu5VS"}],"size":62613,"noattachment":false,"key":"/loglevel-plugin-prefix/-/loglevel-plugin-prefix-0.8.0.tgz"},"maintainers":[{"name":"kutuluk","email":"blooderast@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/loglevel-plugin-prefix-0.8.0.tgz_1516959204848_0.2715069307014346"},"directories":{},"readme":"# loglevel-plugin-prefix\r\n\r\nPlugin for [loglevel](https://github.com/pimterry/loglevel) message prefixing.\r\n\r\n[![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)\r\n\r\n## Installation\r\n\r\n```sh\r\nnpm install loglevel-plugin-prefix\r\n```\r\n\r\n## API\r\n\r\n**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.**\r\n\r\n#### `reg(loglevel)`\r\n\r\nThis 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.\r\n\r\n#### Parameters\r\n\r\n`loglevel` - the root logger, imported from loglevel module\r\n\r\n#### `apply(logger, options)`\r\n\r\nThis 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.**\r\n\r\n#### Parameters\r\n\r\n`logger` - any logger of loglevel\r\n\r\n`options` - an optional configuration object\r\n\r\n```javascript\r\nvar defaults = {\r\n  template: '[%t] %l:',\r\n  levelFormatter: function (level) {\r\n    return level.toUpperCase();\r\n  },\r\n  nameFormatter: function (name) {\r\n    return name || 'root';\r\n  },\r\n  timestampFormatter: function (date) {\r\n    return date.toTimeString().replace(/.*(\\d{2}:\\d{2}:\\d{2}).*/, '$1');\r\n  },\r\n  format: undefined\r\n};\r\n```\r\n\r\nPlugin 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:\r\n\r\n- `%l` - level of message\r\n- `%n` - name of logger\r\n- `%t` - timestamp of message\r\n\r\nThe `levelFormatter`, `nameFormatter` and `timestampFormatter` is a functions for formatting corresponding values.\r\n\r\nAlternatively, you can use `format` option. This is a function that receives formatted values (level, name, timestamp) and should returns a prefix string.\r\n\r\nIf 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.\r\n\r\n## Usage\r\n\r\n### Browser directly\r\n```html\r\n<script src=\"https://unpkg.com/loglevel/dist/loglevel.min.js\"></script>\r\n<script src=\"https://unpkg.com/loglevel-plugin-prefix@^0.8/dist/loglevel-plugin-prefix.min.js\"></script>\r\n\r\n<script>\r\n  var logger = log.noConflict();\r\n  var prefixer = prefix.noConflict();\r\n  prefixer.reg(logger);\r\n  prefixer.apply(logger);\r\n  logger.warn('prefixed message');\r\n</script>\r\n```\r\n\r\nOutput\r\n```\r\n[16:53:46] WARN: prefixed message\r\n```\r\n\r\n### Node\r\n```javascript\r\nconst chalk = require('chalk');\r\nconst log = require('loglevel');\r\nconst prefix = require('loglevel-plugin-prefix');\r\n\r\nconst colors = {\r\n  TRACE: chalk.magenta,\r\n  DEBUG: chalk.cyan,\r\n  INFO: chalk.blue,\r\n  WARN: chalk.yellow,\r\n  ERROR: chalk.red,\r\n};\r\n\r\nprefix.reg(log);\r\nlog.enableAll();\r\n\r\nprefix.apply(log, {\r\n  format(level, name, timestamp) {\r\n    return `${chalk.gray(`[${timestamp}]`)} ${colors[level.toUpperCase()](level)} ${chalk.green(`${name}:`)}`;\r\n  },\r\n});\r\n\r\nprefix.apply(log.getLogger('critical'), {\r\n  format(level, name, timestamp) {\r\n    return chalk.red.bold(`[${timestamp}] ${level} ${name}:`);\r\n  },\r\n});\r\n\r\nlog.trace('trace');\r\nlog.debug('debug');\r\nlog.getLogger('critical').info('Something significant happened');\r\nlog.log('log');\r\nlog.info('info');\r\nlog.warn('warn');\r\nlog.error('error');\r\n```\r\n\r\nOutput\r\n\r\n![output](https://raw.githubusercontent.com/kutuluk/loglevel-plugin-prefix/master/colored.png \"output\")\r\n\r\n## Custom options\r\n\r\n```javascript\r\nconst log = require('loglevel');\r\nconst prefix = require('loglevel-plugin-prefix');\r\n\r\nprefix.reg(log);\r\nlog.enableAll();\r\n\r\nprefix.apply(log, {\r\n  template: '[%t] %l (%n) static text:',\r\n  levelFormatter(level) {\r\n    return level.toUpperCase();\r\n  },\r\n  nameFormatter(name) {\r\n    return name || 'global';\r\n  },\r\n  timestampFormatter(date) {\r\n    return date.toISOString();\r\n  },\r\n});\r\n\r\nlog.info('%s prefix', 'template');\r\n\r\nconst fn = (level, name, timestamp) => `[${timestamp}] ${level} (${name}) static text:`;\r\n\r\nprefix.apply(log, { format: fn });\r\n\r\nlog.info('%s prefix', 'functional');\r\n\r\nprefix.apply(log, { template: '[%t] %l (%n) static text:' });\r\n\r\nlog.info('again %s prefix', 'template');\r\n```\r\n\r\nOutput\r\n```\r\n[2017-05-29T12:53:46.000Z] INFO (global) static text: template prefix\r\n[2017-05-29T12:53:46.000Z] INFO (global) static text: functional prefix\r\n[2017-05-29T12:53:46.000Z] INFO (global) static text: again template prefix\r\n```\r\n\r\n## Option inheritance\r\n\r\n```javascript\r\nconst log = require('loglevel');\r\nconst prefix = require('loglevel-plugin-prefix');\r\n\r\nprefix.reg(log);\r\nlog.enableAll();\r\n\r\nlog.info('root');\r\n\r\nconst chicken = log.getLogger('chicken');\r\nchicken.info('chicken');\r\n\r\nprefix.apply(chicken, { template: '%l (%n):' });\r\nchicken.info('chicken');\r\n\r\nprefix.apply(log);\r\nlog.info('root');\r\n\r\nconst egg = log.getLogger('egg');\r\negg.info('egg');\r\n\r\nconst fn = (level, name) => `${level} (${name}):`;\r\n\r\nprefix.apply(egg, { format: fn });\r\negg.info('egg');\r\n\r\nprefix.apply(egg, {\r\n  levelFormatter(level) {\r\n    return level.toLowerCase();\r\n  },\r\n});\r\negg.info('egg');\r\n\r\nchicken.info('chicken');\r\nlog.info('root');\r\n```\r\n\r\nOutput\r\n```\r\nroot\r\nchicken\r\nINFO (chicken): chicken\r\n[16:53:46] INFO: root\r\n[16:53:46] INFO: egg\r\nINFO (egg): egg\r\ninfo (egg): egg\r\nINFO (chicken): chicken\r\n[16:53:46] INFO: root\r\n```\r\n","publish_time":1516959206014,"_cnpm_publish_time":1516959206014,"dist-tags":{"latest":"0.8.4"}}