{"id":16995189,"url":"https://github.com/jalik/js-logger","last_synced_at":"2025-04-12T05:39:01.730Z","repository":{"id":28602593,"uuid":"118824745","full_name":"jalik/js-logger","owner":"jalik","description":"A logging utility to log messages to anywhere.","archived":false,"fork":false,"pushed_at":"2024-02-26T20:08:41.000Z","size":1203,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T05:38:53.013Z","etag":null,"topics":["logger","logging","logs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/jalik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","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}},"created_at":"2018-01-24T21:31:42.000Z","updated_at":"2023-08-29T05:37:22.000Z","dependencies_parsed_at":"2024-01-29T03:31:42.624Z","dependency_job_id":null,"html_url":"https://github.com/jalik/js-logger","commit_stats":{"total_commits":153,"total_committers":1,"mean_commits":153.0,"dds":0.0,"last_synced_commit":"5bba4f461ab948be4a682945f2e79ea7e6e46ebf"},"previous_names":["jalik/jk-logger"],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jalik%2Fjs-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jalik","download_url":"https://codeload.github.com/jalik/js-logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248525170,"owners_count":21118617,"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":["logger","logging","logs"],"created_at":"2024-10-14T03:47:49.631Z","updated_at":"2025-04-12T05:39:01.697Z","avatar_url":"https://github.com/jalik.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jalik/logger\n\n![GitHub package.json version](https://img.shields.io/github/package-json/v/jalik/js-logger.svg)\n![Build Status](https://github.com/jalik/js-logger/actions/workflows/node.js.yml/badge.svg)\n![Last commit](https://img.shields.io/github/last-commit/jalik/js-logger.svg)\n[![GitHub issues](https://img.shields.io/github/issues/jalik/js-logger.svg)](https://github.com/jalik/js-logger/issues)\n![GitHub](https://img.shields.io/github/license/jalik/js-logger.svg)\n![npm](https://img.shields.io/npm/dt/@jalik/logger.svg)\n\nEasy and customizable logging for your apps.\n\n## Features\n\n* Enabling/disabling logging anytime\n* Naming of the logger\n* Filtering of log events using a custom function\n* Support multiple outputs (works sequentially)\n* Customizing format of log events\n* Ability to pass a context for each log event\n* Ability to set a default context for log events\n* Use of standards logging levels (debug, info, warning, error, fatal)\n* TypeScript declarations ♥\n\n## Sandbox\n\nPlay with the lib here:\nhttps://codesandbox.io/s/jalik-logger-default-example-75o5hx\n\n## Installing\n\n```shell\nnpm i -P @jalik/logger\n```\n```shell\nyarn add @jalik/logger\n```\n\n## Creating a logger\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger();\n```\n\nWhich is equivalent to a production default setup:\n\n```js\nimport {\n  Logger,\n  INFO\n} from '@jalik/logger';\n\nconst logger = new Logger({\n  // Enable the logger\n  active: true,\n  // Only log events with an INFO level or more\n  level: INFO,\n  // Set the name of this logger (auto-generated if not set)\n  name: 'logger_123456789',\n  // Set logging outputs\n  outputs: [\n    // Output logs to the console\n    consoleOutput()\n  ]\n});\n```\n\n## Logging levels\n\nThe following levels are available (ordered from the **less important to the most\nimportant**).\n\n- `debug`: used for debugging messages\n- `info`: used for informational messages\n- `warn`: used for warning messages\n- `error`: used for error messages\n- `fatal`: used for fatal error messages\n\nThey can be imported individually.\n\n```js\nimport {\n  DEBUG,\n  INFO,\n  WARN,\n  ERROR,\n  FATAL\n} from '@jalik/logger';\n```\n\nOr they can be imported as an array.\n\n```js\nimport { levels } from '@jalik/logger';\n```\n\n## Logging messages\n\n### `debug(message: string, context?: any)`\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({ name: 'main' });\nconst a = 2;\nconst b = 4;\nconst result = a + b;\n\n// Logs a message with a context\nlogger.debug(`result = ${result}`, { a, b });\n// or without context\nlogger.debug(`result = ${result}`);\n```\n\n### `info(message: string, context?: any)`\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({ name: 'main' });\nconst bootTime = 1337;\n\n// Log the message with a context\nlogger.info(`Application started in ${bootTime} ms`, { bootTime, tags: ['boot'] });\n// or without context\nlogger.info(`Application started in ${bootTime} ms`);\n```\n\n### `warn(message: string, context?: any)`\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({ name: 'main' });\nconst diskUsage = 93.6;\n\n// Log the message with a context\nlogger.warn('Disk usage is above 90%', { diskUsage });\n// or without context\nlogger.warn('Disk usage is above 90%');\n```\n\n### `error(message: string, context?: any)`\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({ name: 'main' });\nconst error = new Error('Forbidden');\n\n// Log the message with a context\nlogger.error('Forbidden', { error });\n// or without context\nlogger.error('Forbidden');\n// or simply (it will use error.message)\nlogger.error(error);\n```\n\n### `fatal(message: string, context?: any)`\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({ name: 'main' });\nconst error = new Error('app crashed');\n\n// Log the message with a context\nlogger.fatal('app crashed', { error });\n// or without context\nlogger.fatal('app crashed');\n// or simply (it will use error.message)\nlogger.fatal(error);\n```\n\n### `log(level: string, message: string, context?: any)`\n\nThis is the \"low level\" function called by other logging functions.\n\n```js\nimport {\n  Logger,\n  INFO\n} from '@jalik/logger';\n\nconst logger = new Logger({ name: 'main' });\nconst ipAddress = '6.6.6.6';\n\n// Logs an informational message with a context.\nlogger.log(INFO, `The IP address ${ipAddress} has failed to login 3 times`, { ipAddress });\n```\n\n## Enabling or disabling logging\n\nLogging is enabled by default if you don't set `active: false` in Logger options. However, you can\nchange logging status at anytime with the `setActive(boolean)` method.\n\n### `setActive(boolean)`\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({\n  // Enable logger on production environment only.\n  active: process.env.NODE_ENV === 'PRODUCTION'\n});\n\n// To check if the logger is active.\nlogger.isActive();\n\n// Disable logger after 30 seconds.\nsetTimeout(() =\u003e {\n  // Anything that is logged after this line will be ignored.\n  logger.setActive(false);\n  logger.info('Sky is blue');\n}, 30000)\n```\n\n### `isActive(): boolean`\n\nThis method tells you if the logger is enabled.\n\n## Setting a default context\n\nIt is possible to set `defaultContext` when creating the logger.  \nThis context will be passed to all log events and may be overridden for each log.\n\n```js\nimport { Logger } from '@jalik/logger';\n\nconst logger = new Logger({\n  defaultContext: {\n    host: process.env.HOST\n  }\n});\n\n// then logging a message will automatically use the default context.\nlogger.info('Application started.');\n\n// you can even add a context over a default context (attributes will be merged and/or replaced).\nlogger.info('Something happened', { tag: 'something-event' });\n```\n\n## Filtering log events\n\nYou can filter the logs that are processed by using the `filter` option when creating a logger.\n\n```js\nimport {\n  DEBUG,\n  Logger\n} from '@jalik/logger';\n\nconst cronFilter = (event) =\u003e {\n  return (event.context \u0026\u0026 event.context.tag === 'cron') || /cron/g.test(event.message)\n}\n\nconst logger = new Logger({\n  level: DEBUG,\n  filter: cronFilter\n});\n\n// this will be logged.\nlogger.info('Cron jobs executed.', { tag: 'cron' });\n\n// this will not be logged.\nlogger.info('Application started.');\n```\n\n## Logging outputs\n\nA logger can be configured with several `outputs`, all of them are executed sequentially.\nBy default, a logger is configured to output messages to the console with `consoleOutput()`.\n\n### `consoleOutput(options)`\n\nThe console output displays logs in the console (browser and nodejs).\n\n```js\nimport {\n  Logger,\n  consoleOutput\n} from '@jalik/logger';\n\nconst logger = new Logger({\n  name: 'main',\n  outputs: [\n    consoleOutput(),\n  ],\n});\n\nlogger.info('Hello World', { number: 42 });\n// will log:\n// 2021-05-27T02:40:06.957Z INFO [main] : Hello World ; {\"number\":42}\n```\n\nBy default, `consoleOutput()` uses the `defaultFormatter()` function to format log events, but you can provide your own formatter.\n\n```js\nimport {\n  Logger,\n  consoleOutput,\n} from '@jalik/logger';\n\nfunction customFormatter(event) {\n  // format: \"LEVEL [LOGGER] : MESSAGE\"\n  const { level, logger, message } = event;\n  return [level.toUpperCase(), `[${logger}]`, ':', message].join(' ');\n}\n\nconst logger = new Logger({\n  name: 'main',\n  outputs: [\n    consoleOutput({ formatter: customFormatter }),\n  ],\n});\n\nlogger.info('Hello World', { number: 42 });\n// will log:\n// INFO [main] : Hello World\n```\n\n### `fileOutput(options)`\n\nThe file output writes log events to a file, so it can only be used on NodeJS.\n\n```js\nimport {  Logger,} from '@jalik/logger';\nimport fileOutput from '@jalik/logger/dist/outputs/fileOutput.js'\n\nconst logger = new Logger({\n  name: 'main',\n  outputs: [\n    fileOutput({\n      // the logs destination file\n      path: 'logs.txt',\n      // the formatter to use\n      formatter: JSON.stringify,\n      // improve performances by flushing (writing) logs at interval\n      // instead of writing logs every time\n      flushInterval: 1000\n    }),\n  ],\n});\n\nlogger.info('Hello World', { number: 42 });\n// will log:\n// {\"timestamp\":1682982410055,\"level\":\"INFO\",\"logger\":\"main\",\"message\":\"Hello World\",\"context\":{\"number\":42}}\n```\n\n## Changelog\n\nHistory of releases is in the [changelog](./CHANGELOG.md) on github.\n\n## License\n\nThe code is released under the [MIT License](http://www.opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalik%2Fjs-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjalik%2Fjs-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjalik%2Fjs-logger/lists"}