我们可以自己实现一个log插件,当然sword默认已经包含了一个log插件,所以当你需要定制log功能的时候,你再使用它:

  1. import chalk from 'chalk';
  2. import type { Plugin } from '../../../typings/index';
  3. const now = () => {
  4. const _ = new Date();
  5. return `${_.getFullYear()}-${_.getMonth() + 1}-${_.getDate()} ${_.getHours()}:${_.getMinutes()}:${_.getSeconds()}`;
  6. };
  7. export const log = {
  8. err: (v: string | Error): void => {
  9. console.log(`${chalk.gray(now())} ${v}`);
  10. },
  11. info: (v: string): void => {
  12. console.log(`${chalk.gray(now())} ${chalk.yellow(v)}`);
  13. },
  14. success: (v: string): void => {
  15. console.log(`${chalk.gray(now())} ${chalk.green(v)}`);
  16. }
  17. };
  18. /**
  19. * 定义终端打印日志插件
  20. * @preset true
  21. * @return {*}
  22. */
  23. export const useLogPlugin = (): Plugin => {
  24. return {
  25. name: 'log',
  26. log
  27. };
  28. };

使用这个插件:

  1. const plugin = usePlugin();
  2. plugin.add(useLogPlugin);

类型

  1. log?: {
  2. err: (v: string | Error) => void;
  3. info: (v: string) => void;
  4. success: (v: string) => void;
  5. };