3.1.0 • Published 5 months ago

@jeffchi/logger v3.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

A log print output javascript tool library that can be used at the front and back ends

tested with jest test code style: prettier actions-cool

支持环境

  • 现代浏览器。
  • 支持服务端使用, 在服务端使用时默认写入 %root%/logs/yyyy-mm-dd.log 日志文件。
IE / EdgeFirefoxChromeSafariOpera
Edgelast 2 versionslast 2 versionslast 2 versionslast 2 versions

安装

使用 npm 或 yarn 安装

$ npm install @jeffchi/logger --save
$ yarn add @jeffchi/logger

如果你的网络环境不佳,推荐使用 cnpm

用法

import { loggerWithTags } from '@jeffchi/logger';
import { LogLevel, LogMode } from '@jeffchi/logger/lib/interface';
const { debug, error, info, log, warn } = loggerWithTags(
  'api', // ['api','get'] 一个或多个tag
  {
    level: LogLevel.LOG, // 日志级别只有指定级别的日志才会输出至日志文件
    date: 'MMM dd, yyyy HH:mm:ss.sss', // 日志输出的时间戳格式,true表示使用默认utc IOS日期时间格式,false表示不输出时间戳
    env: LogMode.ALL, // 环境参数,开发版本或生产版本,如果指定开发环境,则生产环境不输出任何内容,也不会写入到日志文件
    disableWarn: false, // 禁用Warn输出,用debug代替,仅影响前端控制台和后端终端,不影响实际内容打印.在跑测试时应该启用,避免影响测试结果
    disableError: false, // 禁用Error输出,用debug代替,仅影响前端控制台和后端终端,不影响实际内容打印.在跑测试时应该启用,避免影响测试结果
    ignoreThrow: false, // 默认Error输出后,会抛出异常,从而打断后端执行流程,可指定true取消该行为,在跑测试时应该启用,避免影响测试测试流程
    outputFile: 'logs/<yyyy-MM-dd>.log', // 输出日志文件的位置,仅服务端使用时有效
  },
);

参数

loggerWithTags( tags:LogTags, options:ILogOptions );

tags

/** 日志标签 */
export type LogTags = string | string[];

options

/** 日志配置选项 */
export interface ILogOptions {
  /** 日志级别
   * @default LogLevel.LOG
   */
  level?: LogLevel;
  /**
   * 是否支持输出时间戳及时间戳格式
   *
   * 字符串格式参见: https://github.com/date-fns/date-fns/blob/main/src/format/index.ts
   * @default true 开启后默认IOS格式 'yyyy-MM-ddTHH:mm:ss.SSSZ'
   */
  date?: boolean | string;
  /**
   * 日志输出条件,默认全部输出
   * @default 'all'
   */
  env?: LogMode;
  /** 禁用warn输出,避免在测试场景下影响测试结果
   * @default false
   */
  disableWarn?: boolean;
  /** 禁用error输出,避免在测试场景下打断正常测试流程
   * @default false
   */
  disableError?: boolean;
  /** 调用error输出错误信息后,禁止继续抛出异常错误
   * @description
   * 调用error输出错误信息后,默认继续抛出异常错误,在测试环境下可以临地禁用,避免影响正常的测试流程
   */
  ignoreThrow?: boolean;
  /**
   * 基于当前工程根目录下的日志输出文件
   *
   * @description
   * 浏览器环境:自动忽略该选项;
   *
   * node环境下默认 logs/xxx.log
   */
  outputFile?: string;
}

测试

src/__test__ 目录下的 *.spec.ts*.test.ts 是测试文件.

  • *.spec.ts 特定于指定版本的测试文件,需要切换到指定版本分支才能保证测试通过
  • *.test.ts 当前版本的测试文件

测试基于 ts-jest,终端窗口通过指定参数,来运行特定的测试文件

npm run test 1.0.3  # 1.0.3的测试文件
npm run test main   # 当前测试文件
npm run test        # 所有测试文件

License

MIT

4.0.0-alpha.2

5 months ago

4.0.1-alpha.2

5 months ago

4.1.0-rc.0

5 months ago

3.0.1-alpha.6

1 year ago

3.0.1-alpha.5

1 year ago

3.0.1-alpha.2

1 year ago

3.0.1-alpha.1

1 year ago

3.0.1-alpha.3

1 year ago

3.1.0

1 year ago

3.1.0-rc.0

1 year ago

3.0.0

1 year ago

2.9.0-rc.1

1 year ago

2.5.1-alpha.0

1 year ago

2.5.0

1 year ago

2.4.0-rc.0

1 year ago

2.3.4-rc.0

1 year ago

2.1.1

1 year ago

2.0.0

1 year ago

1.1.11-rc.2

1 year ago

1.1.1

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago