🚀 DAY 1 OF LAUNCH WEEK: Reachability for Ruby Now in Beta.Learn more
Socket
Book a DemoInstallSign in
Socket

malagu-winston

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

malagu-winston

该组件是基于[malagu](https://github.com/muxiangqiu/malagu)框架,结合[winston](https://github.com/winstonjs/winston)开发 的一款组件。

latest
Source
npmnpm
Version
2.22.11
Version published
Maintainers
1
Created
Source

Malagu - Winston Logger

该组件是基于malagu框架,结合winston开发 的一款组件。

组件安装

$ yarn add malagu-winston

$ npm install malagu-winston

组件配置

因为是基于winston的,所以winston的所有配置,该组件都支持,比如:

malagu:
  logger:
    winstonConfig:
      level: 'info'

所有的配置都挂在malagu.logger.winstonConfig,如果配置是个函数类型的,该组件还提供依赖注入的方式导入配置,比如winston的 transport配置。该组件默认只引入ConsoleTransport

举个例子(示例来源于doumi-blog),我们需要引入winston-daily-rotate-file这个transport,那么配置如下:

malagu:
  logger:
    winstonConfig:
      level: 'info'
    dailyRotateConfig:
      frequency: '24h'
      filename: 'doumi-blog-%DATE%.log'
      dirname: './log'

然后定义一个config的文件,如下:

import { Component, Value, LOGGER_CONFIG } from '@malagu/core';
import { WinstonConfig } from 'malagu-winston';
import { format, transports } from 'winston';
import * as Transport from 'winston-transport';

const DailyRotateFile = require('winston-daily-rotate-file');

@Component(WinstonConfig)
export class WinstonConfigImpl implements WinstonConfig {
  transports: Transport[];

  constructor(
    @Value(LOGGER_CONFIG)
    protected readonly config: any,
    @Value('mode')
    protected readonly mode: string
  ) {
    const { dailyRotateConfig } = this.config;
    this.transports = [
      new DailyRotateFile({
        ...dailyRotateConfig,
        format: format.combine(
          format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss,SSS'}),
          format.simple(),
          format.printf(msg =>
            `${msg.timestamp} - ${msg.level}: ${msg.message}`
          )
        ),
      }),
    ];
    if (this.mode.includes('local')) {
      this.transports.push(new transports.Console({
        format: format.combine(
          format.colorize(),
          format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss,SSS'}),
          format.simple(),
          format.printf(msg =>
            `${msg.timestamp} - ${msg.level}: ${msg.message}`
          )
        ),
      }));
    };
  }
}

于是就可以开心地使用该组件了

组件的使用

直接依赖注入该组件即可:

import { Autowired, Logger } from '@malagu/core';
import { WinstonLogger } from 'malagu-winston';

export class Test {
  @Autowired(Logger) logger: WinstonLogger;

  testMe() {
    this.logger.info('test me!'); // INFO  test me
  }
}

更多使用示例可以参考豆米博客源码:传送门

Keywords

malagu-component

FAQs

Package last updated on 28 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts