{"id":26343667,"url":"https://github.com/jukkahell/nestlogger","last_synced_at":"2025-03-16T05:17:42.806Z","repository":{"id":34076284,"uuid":"168750942","full_name":"jukkahell/nestlogger","owner":"jukkahell","description":"Logger library for NestJs services","archived":false,"fork":false,"pushed_at":"2023-01-07T04:01:58.000Z","size":1578,"stargazers_count":28,"open_issues_count":15,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-15T04:36:37.150Z","etag":null,"topics":["daily-rotate","logger","logging","nest","nestjs","winston"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jukkahell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-01T19:39:47.000Z","updated_at":"2022-08-03T07:36:16.000Z","dependencies_parsed_at":"2023-01-15T04:27:18.781Z","dependency_job_id":null,"html_url":"https://github.com/jukkahell/nestlogger","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jukkahell%2Fnestlogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jukkahell%2Fnestlogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jukkahell%2Fnestlogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jukkahell%2Fnestlogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jukkahell","download_url":"https://codeload.github.com/jukkahell/nestlogger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354222,"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":["daily-rotate","logger","logging","nest","nestjs","winston"],"created_at":"2025-03-16T05:17:42.241Z","updated_at":"2025-03-16T05:17:42.792Z","avatar_url":"https://github.com/jukkahell.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Install\n```\nnpm i nest-logger\n```\n\n# TL;DR\n\nAdd this in your logger module's useFactory, inject LoggerService and start logging beautiful log entries.\n\n```javascript\nconst options: LoggerOptions = {\n  fileOptions: {\n    filename: `logs/my-service-%DATE%.log`,\n  },\n  colorize: config.colorize,\n};\nconst loggers = LoggerService.getLoggers(\n  [LoggerTransport.CONSOLE, LoggerTransport.ROTATE],\n  options,\n);\n\nreturn new LoggerService(\n  config.logLevel,\n  loggers,\n);\n```\n\n# Detailed usage examples\n\nUse in your project by creating a logger.module.ts with content like this:\n\n```javascript\nimport { Module } from \"@nestjs/common\";\nimport { ConfigModule } from \"../config/config.module\";\nimport { LoggerService, LoggerOptions } from \"nest-logger\";\nimport { ConfigService } from \"../config/config.service\";\n\n@Module({\n  imports: [ConfigModule],\n  providers: [\n    {\n      provide: LoggerService,\n      useFactory: (config: ConfigService) =\u003e {\n        // getLoggers() is a helper function to get configured console and/or rotate logger transports.\n        // It takes takes two parameters:\n        // 1: Appenders where to log to: console or rotate or both in array\n        //    (eg. [LoggerTransport.CONSOLE, LoggerTransport.ROTATE])\n        // 2: Logger options object that contains the following properties:\n        //    timeFormat?: winston's time format syntax. Defaults to \"HH:mm:ss\".\n        //    colorize?: whether to colorize the log output. Defaults to true.\n        //    consoleOptions?: see Winston's ConsoleTransportOptions interface\n        //    fileOptions?: see Winston Daily Rotate File's DailyRotateFile.DailyRotateFileTransportOptions\n        const options: LoggerOptions = {\n          fileOptions: {\n            filename: `${config.logger.path}/${config.serviceName}-%DATE%.log`,\n          },\n          colorize: config.colorize,\n        };\n        const loggers = LoggerService.getLoggers(\n          config.logAppenders,\n          options,\n        );\n        // LoggerService constructor will take two parameters:\n        // 1. Log level: debug, info, warn or error\n        // 2. List of logger transport objects.\n        return new LoggerService(\n          config.logLevel,\n          loggers,\n        );\n      },\n      inject: [ConfigService],\n    },\n  ],\n  exports: [LoggerService],\n})\nexport class LoggerModule {}\n```\n\n## Overriding transport options per logger\nYou can do it like this if you want different options for console and file:\n```javascript\n   const loggers = [\n      LoggerService.console({\n        timeFormat: \"HH:mm\",\n        consoleOptions: {\n          level: \"info\",\n        },\n      }),\n      LoggerService.rotate({\n        colorize: false,\n        fileOptions: {\n          filename: `${config.logger.path}/${config.serviceName}-%DATE%.log`,\n          level: \"error\",\n        },\n      }),\n   ];\n   return new LoggerService(\n      config.logger.defaultLevel,\n      loggers,\n   );\n```\n\n## Overriding formatter function\n\nWrite a function that returns logform.Format object (it's the same what Winston uses):\n\n```javascript\nconst customFormatter = (options: LoggerOptions) =\u003e {\n  const format = winston.format.printf(info =\u003e {\n    const level = options.colorize ? this.colorizeLevel(info.level) : `[${info.level.toUpperCase()}]`.padEnd(7);\n    return `${info.timestamp} ${context}${level}${reqId} ${info.message}`;\n  });\n\n  return winston.format.combine(\n    winston.format.timestamp({\n      format: options.timeFormat,\n    }),\n    format,\n  );\n}\n```\n\nPass the formatter function as the third param of LoggerService's constructor\n```javascript\n\nreturn new LoggerService(\n  config.logLevel,\n  loggers,\n  customFormatter\n);\n```\n\n## Overriding winston logger options\n\nReplace level parameter in the first argument of LoggerService's constructor.  \nNote that the transports option will be set from the loggers (second constructor parameter) so there's no use to override those here.\n```javascript\nconst myCustomLevels = {\n  levels: {\n    info: 0,\n    warning: 1,\n    error: 2,\n    apocalypse: 3\n  },\n  colors: {\n    info: 'blue',\n    warning: 'green',\n    error: 'yellow',\n    apocalypse: 'red'\n  }\n};\nconst loggerOptions: winston.LoggerOptions = {\n  level: config.logLevel,\n  levels: customLevels,\n}\n\nreturn new LoggerService(\n  loggerOptions,\n  loggers,\n  customFormatter\n);\n```\n\n## Using the logger\n\nImport logger module wherever you need it:\n\n```javascript\n...\nimport { LoggerModule } from \"../logging/logger.module\";\n\n@Module({\n  imports: [\n    LoggerModule,\n    DBModule,\n  ],\n  controllers: [ItemController],\n  providers: [ItemService],\n})\nexport class ItemModule {}\n```\n\nAnd log stuff:\n```javascript\nimport { LoggerService } from \"nest-logger\";\nconstructor(private readonly logger: LoggerService) {}\n\npublic logStuff() {\n  this.logger.debug(`Found ${result.rowCount} items from db`, ItemService.name);\n  this.logger.error(`Error while getting items from db`, err.stack, ItemService.name);\n}\n```\n\n# Release Notes\n\n## 7.0.0\n- NestJS 7.6.11 -\u003e 7.6.15\n- Merge 1.2.1 -\u003e 2.1.1\n- RxJS 6.6.3 -\u003e 7.0.0\n- etc. libs to the latest\n- Moment only needed in devDependencies\n\n## 6.2.0\n- NestJS 7.4.4 -\u003e 7.6.11\n- Axios 0.20.0 -\u003e 0.21.1\n\n## 6.1.0\n- Pass Winston logger options as an optional constructor parameter for LoggerService\n- Pass custom formatter function as an optional constructor parameter for LoggerService\n- NestJS 7.0.9 -\u003e 7.4.4\n- Moment 2.25.3 -\u003e 2.29.0\n- rxjs 6.5.4 -\u003e 6.6.3\n- Typescript 3.8.3 -\u003e 4.0.3\n- Winston 3.2.1 -\u003e 3.3.3\n- Winston Daily Rotate File 4.4.2 -\u003e 4.5.0\n\n## 6.0.0\n- NestJS 6.10.14 -\u003e 7.0.9\n- Updated other deps to the latest\n\n## 5.0.1\n- Nullpointer fix to init loggers without passing any options (credits to OpportunityLiu)\n\n## 5.0.0\n- Support for all winston logger options for console and rotate transports\n\n## 4.0.1\n- Fixed a bug where default options were overridden after the first transport creation\n\n## 4.0.0\n- More configurable way of initializing the logger\n\n   Options can be passed as an object  \n   Colors can be disabled/enabled from the options\n- RxJs 6.5.2 -\u003e 6.5.4\n- Nest 6.10.6 -\u003e 6.10.14\n- Winston Daily Rotate File 4.3.0 -\u003e 4.4.1\n\n## 3.0.0\n- Log Map objects as key-value-pairs\n- Dependency upgrades\n- NestJS 6.3.2 -\u003e 6.10.6\n- Winston Daily Rotate File 3.9.0 -\u003e 4.3.0\n- Typescript 3.5.2 -\u003e 3.7.3\n\n## 2.1.0\n- NestJS 6.2.4 -\u003e 6.3.2\n\n## 2.0.0\n- Nest 5.6.2 -\u003e 6.2.4\n- RxJs 6.4.0 -\u003e 6.5.2\n- Winston Daily Rotate File 3.6.0 -\u003e 3.9.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjukkahell%2Fnestlogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjukkahell%2Fnestlogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjukkahell%2Fnestlogger/lists"}