{"id":20774031,"url":"https://github.com/secjs/logger","last_synced_at":"2025-07-05T20:06:15.198Z","repository":{"id":43353437,"uuid":"371013062","full_name":"SecJS/Logger","owner":"SecJS","description":"🔍 Simple logger for Node.js","archived":false,"fork":false,"pushed_at":"2022-03-06T13:01:59.000Z","size":1158,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-10T09:38:36.139Z","etag":null,"topics":["log-channel","logger","logging","nodejs"],"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/SecJS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"open_collective":"secjs"}},"created_at":"2021-05-26T11:42:25.000Z","updated_at":"2022-01-07T17:25:24.000Z","dependencies_parsed_at":"2022-09-03T16:10:46.987Z","dependency_job_id":null,"html_url":"https://github.com/SecJS/Logger","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SecJS%2FLogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SecJS%2FLogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SecJS%2FLogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SecJS%2FLogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SecJS","download_url":"https://codeload.github.com/SecJS/Logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225036957,"owners_count":17410961,"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":["log-channel","logger","logging","nodejs"],"created_at":"2024-11-17T12:28:10.518Z","updated_at":"2024-11-17T12:28:11.206Z","avatar_url":"https://github.com/SecJS.png","language":"TypeScript","funding_links":["https://opencollective.com/secjs","https://www.buymeacoffee.com/secjs"],"categories":[],"sub_categories":[],"readme":"# Logger 🔍\n\n\u003e Logger to any NodeJS project\n\n[![GitHub followers](https://img.shields.io/github/followers/jlenon7.svg?style=social\u0026label=Follow\u0026maxAge=2592000)](https://github.com/jlenon7?tab=followers)\n[![GitHub stars](https://img.shields.io/github/stars/secjs/logger.svg?style=social\u0026label=Star\u0026maxAge=2592000)](https://github.com/secjs/logger/stargazers/)\n\n\u003cp\u003e\n    \u003ca href=\"https://www.buymeacoffee.com/secjs\" target=\"_blank\"\u003e\u003cimg src=\"https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;\" \u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp\u003e\n  \u003cimg alt=\"GitHub language count\" src=\"https://img.shields.io/github/languages/count/secjs/logger?style=for-the-badge\u0026logo=appveyor\"\u003e\n\n  \u003cimg alt=\"Repository size\" src=\"https://img.shields.io/github/repo-size/secjs/logger?style=for-the-badge\u0026logo=appveyor\"\u003e\n\n  \u003cimg alt=\"License\" src=\"https://img.shields.io/badge/license-MIT-brightgreen?style=for-the-badge\u0026logo=appveyor\"\u003e\n\n  \u003cimg alt=\"Commitizen\" src=\"https://img.shields.io/badge/commitizen-friendly-brightgreen?style=for-the-badge\u0026logo=appveyor\"\u003e\n\u003c/p\u003e\n\nThe intention behind this repository is to always maintain a `Logger` package to any NodeJS project.\n\n\u003cimg src=\".github/logger.png\" width=\"200px\" align=\"right\" hspace=\"30px\" vspace=\"100px\"\u003e\n\n## Installation\n\n```bash\nnpm install @secjs/logger\n```\n\n## Usage\n\n### Config logging template\n\n\u003e First you need to create the configuration file logging in the config folder on project root path. Is extremely important to use export default in these configurations.\n\n```ts\nimport { Env } from '@secjs/env'\nimport { Path } from '@secjs/utils'\n\nexport default {\n  /*\n  |--------------------------------------------------------------------------\n  | Default Log Channel\n  |--------------------------------------------------------------------------\n  |\n  | This option defines the default log channel that gets used when writing\n  | messages to the logs. The name specified in this option should match\n  | one of the channels defined in the \"channels\" configuration object.\n  |\n  */\n\n  default: Env('LOGGING_CHANNEL', 'application'),\n\n  /*\n  |--------------------------------------------------------------------------\n  | Log Channels\n  |--------------------------------------------------------------------------\n  |\n  | Here you may configure the log channels for your application.\n  |\n  | Available Drivers: \"console\", \"debug\", \"file\".\n  | Available Formatters: \"context\", \"debug\", \"json\", \"log\".\n  |\n  */\n\n  channels: {\n    application: {\n      driver: 'console',\n      context: 'Logger',\n      formatter: 'context',\n    },\n    debug: {\n      driver: 'debug',\n      context: 'Debugger',\n      formatter: 'context',\n      namespace: 'api:main',\n    },\n    file: {\n      driver: 'file',\n      context: 'Logger',\n      formatter: 'log',\n      filePath: Path.noBuild().logs('secjs.log'),\n    },\n  },\n}\n```\n\n### Log / Logger\n\n\u003e With the config/logging file created you can use Log and Logger classes to start logging.\n\n```ts\nimport { Log, Logger, Color } from '@secjs/logger'\n\n// Log and Logger will always use the default values of channel inside config/logging, the default channel in here is \"application\".\nLog.log('Hello World!')\n// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms\n\nconst logger = new Logger()\n\nlogger.success('Hello World!')\n// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms\n\n// You can pass options to formatters and drivers as second parameter\nlogger.warn('Hello World!', { color: Color.purple, context: 'LogController' })\n// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [LogController] Hello World! +0ms\n```\n\n### Using other channels\n\n\u003e You can use any channel that you configure inside config/logging, SecJS has default channels inside the template file.\n\n```ts\nLog.channel('debug').log('Hello debug world!', { namespace: 'api:example' })\n// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello debug world! +0ms\n```\n\n### Extending drivers, channels and formatters\n\n\u003e Nowadays, @secjs/logger has only FileDriver, DebugDriver and ConsoleDriver support, but you can extend the drivers for Logger class if you implement DriverContract interface.\n\n```ts\nimport { DriverContract, FormatterContract, format } from '@secjs/logger'\n\ninterface CustomDriverOpts {}\n\nclass CustomDriver implements DriverContract {\n  private readonly _level: string\n  private readonly _context: string\n  private readonly _formatter: string\n  \n  constructor(channel: string) {\n    const config = Config.get(`logging.channels.${channel}`)\n    \n    this._level = config.level || 'INFO'\n    this._context = config.context || 'CustomDriver'\n    this._formatter = config.formatter || 'context'\n  }\n\n  transport(message: string, options?: CustomDriverOpts): void {\n    options = Object.assign(\n      {},\n      {\n        level: this._level,\n        context: this._context,\n        streamType: this._streamType,\n      },\n      options,\n    )\n\n    message = format(this._formatter, message, options)\n\n    process[this._streamType].write(`${message}\\n`)\n  }\n}\n```\n\n\u003e Same to extend formatters\n\n```ts\nclass CustomFormatter implements FormatterContract {\n  // all the methods implemented from FormatterContract...\n}\n```\n\n\u003e Constructor is extremely important in your CustomDriver class, it's the constructor that will use the values from config/logging channels to manipulate your CustomDriver using channel and channels method from logger. \n\u003e So if you are building a CustomDriver, and you want to use it, you can create a new channel inside config/logging channels or change the driver from an existing channel.\n\n```ts\n// extending channels\n// config/logging file\n\nexport default {\n  // default etc...\n  \n  channels: {\n    mychannel: {\n      driver: 'custom',\n      level: 'INFO',\n      formatter: 'context',\n      context: 'Logger',\n    }\n    // ... other disks\n  }\n}\n```\n\n\u003e Now you can build your new driver using Logger class\n\n```ts\nconst driverName = 'custom'\nconst formatterName = 'custom'\nconst driver = CustomDriver\nconst formatter = CustomFormatter\n\nLogger.buildDriver(driverName, driver)\nLogger.buildFormatter(formatterName, CustomFormatter)\n\nconsole.log(Logger.drivers) // ['console', 'debug', 'file', 'custom']\nconsole.log(Logger.formatters) // ['context', 'debug', 'json', 'log', 'custom']\n```\n\n\u003e Now, if you have implemented your channel in config/logging, you can use him inside logger\n\n```ts\n// options of your driver and formatter\nconst options = {}\n\n// Will use CustomDriver to handle the log actions\nlogger.channel('mychannel').success('Hello World!!', options)\n```\n\n---\n\nMade with 🖤 by [jlenon7](https://github.com/jlenon7) :wave:\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsecjs%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsecjs%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsecjs%2Flogger/lists"}