npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-logs

v0.0.2

Published

在nestjs中实现日志打印功能

Downloads

50

Readme

一、Nestjs中日志管理包

  • 1、在你的nestjs项目中安装依赖包

    npm install nest-logs
  • 2、在app.module.ts中导入日志模块包

    import { NestLogsModule} from 'nest-logs';
      
    @Module({
      imports: [
        NestLogsModule
      ],
      controllers: [],
      providers: [
         
      ],
    })
    export class AppModule { }

二、基本使用

  • 1、如果你想拦截每次请求,你可以在控制器上加上nest-logs包中提供的装饰器

    import { NestLogger } from 'nest-logs';
    @NestLogger()
    @Controller()
    export class AppController {}
    # 运行后的效果,可以将一系列的参数打印出来
    2021-04-25 09:20:30 [INFO] [MacBook-Pro-6.local] app.controller.js: 
      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      当前类名: AppController
      当前方法名: hello
      当前请求: POST - /
      IP: ::ffff:127.0.0.1
      当前Params参数: {}
      当前Query参数: {}
      当前请求体: {
      "username": "admin",
      "password": "123456"
    }
      >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  • 2、在别的地方使用日志打印(比如在控制器中)

    默认会存储到日志文件中,如果你不想存储仅仅是想在控制台上显示日志,可以加上第二个参数为false

    import { Logger } from 'nest-logs';
      
    ...
    @Get()
    getHello(): string {
      // 会存储到日志文件中和在控制台上打印
      Logger.info("测试代码")
      // 仅仅在控制台打印
      Logger.info("测试代码", false) 
      return this.appService.getHello();
    }
    ...
    2021-04-25 09:19:45 [INFO] [MacBook-Pro-6.local] [app.controller.js]-[AppController.getHello]: 
    "测试代码"

三、生产上线结合PM2来使用

  • 1、在项目的根目录下创建一个ecosystem.config.js

    可以直接拷贝我这份过去使用

    /* eslint-disable @typescript-eslint/camelcase */
    module.exports = {
      apps: [{
        name: 'nest-server', // 项目名字,启动后的名字
        script: './dist/main.js', // 执行的文件
        cwd: './', // 根目录
        args: '', // 传递给脚本的参数
        watch: true, // 开启监听文件变动重启
        ignore_watch: ['node_modules', 'public', 'logs'], // 不用监听的文件
        exec_mode: 'cluster_mode',
        instances: '2', // max表示最大的 应用启动实例个数,仅在 cluster 模式有效 默认为 fork
        autorestart: true, // 默认为 true, 发生异常的情况下自动重启
        max_memory_restart: '1G',
        instance_var: "INSTANCE_ID", // 添加这一行可以实现使用nest-logs包的日志管理
        // error_file: './logs/app-err.log', // 错误日志文件
        // out_file: './logs/app-out.log', // 正常日志文件
        // merge_logs: true, // 设置追加日志而不是新建日志
        // log_date_format: 'YYYY-MM-DD HH:mm:ss', // 指定日志文件的时间格式
        min_uptime: '60s', // 应用运行少于时间被认为是异常启动
        max_restarts: 30, // 最大异常重启次数
        restart_delay: 60, // 异常重启情况下,延时重启时间
        env: { // 环境参数,当前指定为开发环境
          NODE_ENV: 'development'
        },
        env_production: { // 环境参数,当前指定为生产环境
          NODE_ENV: 'production'
        },
        env_test: { // 环境参数,当前为测试环境
          NODE_ENV: 'test'
        }
      }],
      
      deploy: {
        production: {
          user: 'root',
          host: '39.**.99.86',
          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'
        }
      }
    }
  • 2、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"
    },