@jeffchi/logger 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import { ILogOptions } from '../interface';
2
+ export declare const defOptions: ILogOptions;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defOptions = void 0;
4
+ const interface_1 = require("../interface");
5
+ exports.defOptions = {
6
+ date: true,
7
+ env: interface_1.LogMode.ALL,
8
+ };
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { ILogger, ILogOptions } from './interface';
2
- export declare const loggerWithTags: (tags: string | string[], options?: ILogOptions) => ILogger;
1
+ import { ILogger, ILogOptions, LogTags } from './interface';
2
+ export declare const loggerWithTags: (tags: LogTags, options?: ILogOptions) => ILogger;
package/lib/index.js CHANGED
@@ -1,41 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loggerWithTags = void 0;
4
- const date_fns_1 = require("date-fns");
4
+ const defOptions_1 = require("./consts/defOptions");
5
5
  const interface_1 = require("./interface");
6
- const utils_1 = require("./utils");
7
- const defOptions = {
8
- date: true,
9
- env: interface_1.LogMode.ALL,
10
- };
6
+ const buildLogPreffix_1 = require("./utils/buildLogPreffix");
7
+ const checkLogMode_1 = require("./utils/checkLogMode");
8
+ const checkPlateform_1 = require("./utils/checkPlateform");
9
+ const type_1 = require("./utils/type");
10
+ const writeFile_1 = require("./utils/writeFile");
11
11
  const loggerWithTags = (tags, options) => {
12
- if (!Array.isArray(tags)) {
12
+ if (!(0, type_1.isArray)(tags)) {
13
13
  tags = [tags];
14
14
  }
15
- const { env, ...mergedOptions } = { ...defOptions, ...(options || {}) };
15
+ const { env, ...mergedOptions } = { ...defOptions_1.defOptions, ...(options || {}) };
16
16
  const debug = (...rest) => {
17
- const prefix = (0, utils_1.buildOutputPreffix)(tags, mergedOptions);
18
- (0, utils_1.checkEnv)(env || interface_1.LogMode.ALL) && console.debug(prefix, ...rest);
17
+ const prefix = (0, buildLogPreffix_1.buildLogPreffix)(tags, { level: interface_1.LogLevel.DEBUG, ...mergedOptions });
18
+ const content = [...prefix, ...rest];
19
+ (0, checkLogMode_1.checkLogMode)(env || interface_1.LogMode.ALL) && console.debug(...content);
20
+ (0, checkPlateform_1.checkPlatform)().then((writer) => (0, writeFile_1.writeLogFile)(writer, content));
19
21
  };
20
22
  const info = (...rest) => {
21
- const prefix = (0, utils_1.buildOutputPreffix)(tags, mergedOptions);
22
- (0, utils_1.checkEnv)(env || interface_1.LogMode.ALL) && console.info(prefix, ...rest);
23
+ const prefix = (0, buildLogPreffix_1.buildLogPreffix)(tags, { level: interface_1.LogLevel.INFO, ...mergedOptions });
24
+ const content = [...prefix, ...rest];
25
+ (0, checkLogMode_1.checkLogMode)(env || interface_1.LogMode.ALL) && console.info(...content);
26
+ (0, checkPlateform_1.checkPlatform)().then((writer) => (0, writeFile_1.writeLogFile)(writer, content));
23
27
  };
24
28
  const log = (...rest) => {
25
- const prefix = (0, utils_1.buildOutputPreffix)(tags, mergedOptions);
26
- (0, utils_1.checkEnv)(env || interface_1.LogMode.ALL) && console.log(...prefix, ...rest);
27
- (0, utils_1.checkPlatform)().then((write) => write?.(`logs/${(0, date_fns_1.format)(Date.now(), 'yyyy-MM-dd')}.log`, [...prefix, ...rest.map(JSON.stringify), '\n'].join(' '), { flag: 'a' }, utils_1.noop));
29
+ const prefix = (0, buildLogPreffix_1.buildLogPreffix)(tags, mergedOptions);
30
+ const content = [...prefix, ...rest];
31
+ (0, checkLogMode_1.checkLogMode)(env || interface_1.LogMode.ALL) && console.log(...content);
32
+ (0, checkPlateform_1.checkPlatform)().then((writer) => (0, writeFile_1.writeLogFile)(writer, content));
28
33
  };
29
34
  const warn = (...rest) => {
30
- const prefix = (0, utils_1.buildOutputPreffix)(tags, mergedOptions);
31
- (0, utils_1.checkEnv)(env || interface_1.LogMode.ALL) && console.warn(prefix, ...rest);
35
+ const prefix = (0, buildLogPreffix_1.buildLogPreffix)(tags, { level: interface_1.LogLevel.WARN, ...mergedOptions });
36
+ const content = [...prefix, ...rest];
37
+ (0, checkLogMode_1.checkLogMode)(env || interface_1.LogMode.ALL) && console.warn(...content);
38
+ (0, checkPlateform_1.checkPlatform)().then((writer) => (0, writeFile_1.writeLogFile)(writer, content));
32
39
  };
33
- const error = (msg) => {
34
- const prefix = (0, utils_1.buildOutputPreffix)(tags, mergedOptions);
35
- if ((0, utils_1.checkEnv)(env || interface_1.LogMode.ALL)) {
36
- console.warn(prefix);
37
- throw new Error(msg);
38
- }
40
+ const error = (msg, cause) => {
41
+ const prefix = (0, buildLogPreffix_1.buildLogPreffix)(tags, { level: interface_1.LogLevel.ERROR, ...mergedOptions });
42
+ const content = [...prefix, msg];
43
+ (0, checkLogMode_1.checkLogMode)(env || interface_1.LogMode.ALL) && console.error(...content);
44
+ (0, checkPlateform_1.checkPlatform)().then((writer) => (0, writeFile_1.writeLogFile)(writer, content));
45
+ throw new Error(msg, { cause });
39
46
  };
40
47
  return { debug, info, log, warn, error };
41
48
  };
@@ -8,8 +8,23 @@ export declare enum LogMode {
8
8
  DEVELOPMENET = "development",
9
9
  NONE = "none"
10
10
  }
11
+ /**
12
+ * 日志级别
13
+ */
14
+ export declare enum LogLevel {
15
+ LOG = "LOG",
16
+ WARN = "WARN",
17
+ INFO = "INFO",
18
+ ERROR = "ERROR",
19
+ DEBUG = "DEBUG"
20
+ }
21
+ export type LogTags = string | string[];
11
22
  /** 日志输入配置选项 */
12
23
  export interface ILogOptions {
24
+ /** 日志级别
25
+ * @default LogLevel.LOG
26
+ */
27
+ level?: LogLevel;
13
28
  /**
14
29
  * 是否支持输出时间戳及时间戳格式
15
30
  *
@@ -23,6 +38,7 @@ export interface ILogOptions {
23
38
  */
24
39
  env?: LogMode;
25
40
  }
41
+ export type LogContent = any[];
26
42
  export interface ILogger {
27
43
  debug: (...rest: any[]) => void;
28
44
  info: (...rest: any[]) => void;
package/lib/interface.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LogMode = void 0;
3
+ exports.LogLevel = exports.LogMode = void 0;
4
4
  /**
5
5
  * 日志输出打印的模式
6
6
  * @default LogMode.ALL
@@ -12,3 +12,14 @@ var LogMode;
12
12
  LogMode["DEVELOPMENET"] = "development";
13
13
  LogMode["NONE"] = "none";
14
14
  })(LogMode = exports.LogMode || (exports.LogMode = {}));
15
+ /**
16
+ * 日志级别
17
+ */
18
+ var LogLevel;
19
+ (function (LogLevel) {
20
+ LogLevel["LOG"] = "LOG";
21
+ LogLevel["WARN"] = "WARN";
22
+ LogLevel["INFO"] = "INFO";
23
+ LogLevel["ERROR"] = "ERROR";
24
+ LogLevel["DEBUG"] = "DEBUG";
25
+ })(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
@@ -0,0 +1,2 @@
1
+ import { ILogOptions } from '../interface';
2
+ export declare const buildLogPreffix: (tags: string[], options: ILogOptions) => any[];
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildLogPreffix = void 0;
4
+ const date_fns_1 = require("date-fns");
5
+ const interface_1 = require("../interface");
6
+ const type_1 = require("./type");
7
+ const buildLogPreffix = (tags, options) => {
8
+ const { level = interface_1.LogLevel.LOG, date } = options;
9
+ const prefix = [level.toString()];
10
+ if (tags.length) {
11
+ prefix.unshift(tags.map((t) => `[${t.toUpperCase()}]`).join(' '));
12
+ }
13
+ if (date) {
14
+ let now = new Date();
15
+ if ((0, type_1.isString)(date)) {
16
+ now = (0, date_fns_1.format)(now, date);
17
+ }
18
+ prefix.unshift(now);
19
+ }
20
+ return prefix;
21
+ };
22
+ exports.buildLogPreffix = buildLogPreffix;
@@ -0,0 +1,2 @@
1
+ import { LogMode } from '../interface';
2
+ export declare const checkLogMode: (env: LogMode) => boolean;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkLogMode = void 0;
4
+ const checkLogMode = (env) => (env === 'all' || process.env.NODE_ENV === env) && console !== undefined;
5
+ exports.checkLogMode = checkLogMode;
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * 检查当前运行环境,node环境下返回node的fs模块,浏览器环境返回null
4
+ * @returns fs
5
+ */
6
+ export declare const checkPlatform: () => Promise<typeof import("fs").writeFile | null>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.checkPlatform = void 0;
27
+ /**
28
+ * 检查当前运行环境,node环境下返回node的fs模块,浏览器环境返回null
29
+ * @returns fs
30
+ */
31
+ const checkPlatform = () => Promise.resolve().then(() => __importStar(require('fs'))).then((fs) => {
32
+ // 如果fs为空则判定当前为浏览器环境
33
+ if (!fs)
34
+ return null;
35
+ if (!fs.existsSync('logs')) {
36
+ fs.mkdirSync('logs');
37
+ }
38
+ return fs?.writeFile;
39
+ });
40
+ exports.checkPlatform = checkPlatform;
@@ -0,0 +1,2 @@
1
+ declare const _default: () => undefined;
2
+ export default _default;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = () => void 0;
@@ -0,0 +1,3 @@
1
+ export declare const isUndefined: (v: any) => boolean;
2
+ export declare const isString: (v: any) => boolean;
3
+ export declare const isArray: (arg: any) => arg is any[];
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isArray = exports.isString = exports.isUndefined = void 0;
4
+ const toString = (v) => Object.prototype.toString.call(v);
5
+ const isUndefined = (v) => v === undefined;
6
+ exports.isUndefined = isUndefined;
7
+ const isString = (v) => typeof v === 'string' || toString(v) === '[object String]';
8
+ exports.isString = isString;
9
+ exports.isArray = Array.isArray;
@@ -0,0 +1,2 @@
1
+ import { LogContent } from '../interface';
2
+ export declare const writeLogFile: (writer: any, data: LogContent) => void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.writeLogFile = void 0;
7
+ const date_fns_1 = require("date-fns");
8
+ const noop_1 = __importDefault(require("./noop"));
9
+ const writeLogFile = (writer, data) => writer?.(`logs/${(0, date_fns_1.format)(Date.now(), 'yyyy-MM-dd')}.log`, data.map(JSON.stringify).join(' ') + '\n', { flag: 'a' }, noop_1.default);
10
+ exports.writeLogFile = writeLogFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeffchi/logger",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "description": "A log print output javascript tool library that can be used at the front and back ends",