preface
Nest – Logs log package management, mainly for the convenience of development and online search code bugs and prepared, the log management by date and category, the log in the way of file storage, easy to view, when the time point is automatically deleted, save disk space.
Github source address, welcome to the big guy Star.
A,Nestjs
Log management package
-
Install dependencies in your nestjs project
npm install nest-logs Copy the code
-
2. Import the log module package in app.module.ts
import { NestLogsModule} from 'nest-logs'; @Module({ imports: [ NestLogsModule ], controllers: [].providers: [],})export class AppModule {}Copy the code
Two, basic use
-
1. If you want to intercept every request, you can add a decorator to the controller provided in the Nest -logs package
import { NestLogger } from 'nest-logs'; @NestLogger(a)@Controller(a)export class AppController {} Copy the code
After running, you can print a series of parameters 2021-04-25 09:20:30 [INFO] [MacBook-Pro-6.local] app.controller.js: <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The name of the class: AppController Current method name: hello The current request: POST - / IP: : : FFFF: 127.0.0.1 Current Params parameter: {} Current Query parameter: {} Current request body: { "username": "admin", "password": "123456" } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Copy the code
-
2. Use log printing elsewhere (such as in the controller)
By default, it is stored in a log file. If you do not want to store the log file and only want to display the log file on the console, you can add the second parameter to false
import { Logger } from 'nest-logs'; .@Get() getHello(): string { // Will be stored in a log file and printed on the console Logger.info("Test code") // Print only in the console Logger.info("Test code".false) return this.appService.getHello(); }...Copy the code
2021-04-25 09:19:45 [INFO] [MacBook-Pro-6.local] [app.controller.js]-[AppController.getHello]: "Test code" Copy the code
Three, production on-line combinationPM2
To use the
-
1. Create an ecosystem. Config.js in the root directory of the project
You can just copy my past for use
/* eslint-disable @typescript-eslint/camelcase */ module.exports = { apps: [{ name: 'nest-server'.// The name of the project script: './dist/main.js'.// The file to execute cwd: '/'./ / root directory args: ' '.// The arguments passed to the script watch: true.// Start listening for file changes to restart ignore_watch: ['node_modules'.'public'.'logs'].// Files not to be monitored exec_mode: 'cluster_mode'.instances: '2'.Max indicates the maximum number of startup instances. This parameter is valid only in cluster mode. The default value is fork autorestart: true.// The default value is true and automatically restarts in case of an exception max_memory_restart: '1G'.instance_var: "INSTANCE_ID".// Add this line to enable log management using the nest-Logs package // error_file: './logs/app-err.log', // Error log file // out_file: './logs/app-out.log', // Normal log files // merge_logs: true, // append logs instead of creating new logs // log_date_format: 'YYYY-MM-dd HH: MM :ss', // Specify the time format of the log file min_uptime: '60s'.// An application running for less than a few minutes is considered abnormal startup max_restarts: 30.// Maximum number of abnormal restarts restart_delay: 60.// In the case of an abnormal restart, delay the restart time env: { // Environment parameter, currently specified as development environment NODE_ENV: 'development' }, env_production: { // Environment parameter, currently specified as production environment NODE_ENV: 'production' }, env_test: { // Environment parameters, currently the test environment NODE_ENV: 'test'}}].deploy: { production: { user: 'root'.host: '99.86' 39. * *..ref: 'origin/master'.repo: '[email protected]:repo.git'.path: '/var/www/nest-test'.'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env production'}}}Copy the code
-
2. Configure the start deployment script in package.json
"scripts": {..."pm2:prod": "pm2 start ecosystem.config.js --env production"."pm2:dev": "pm2 start ecosystem.config.js --env development"."pm2:test": "pm2 start ecosystem.config.js --env test" }, Copy the code
-
3. Online view Dynamically view logs
tail -f Specifies the log file name Copy the code