@jeffchi/logger 1.0.0

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.
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { ILogger, ILogOptions } from './interface';
2
+ export declare const loggerWithTags: (tags: string | string[], options?: ILogOptions) => ILogger;
package/lib/index.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loggerWithTags = void 0;
4
+ const date_fns_1 = require("date-fns");
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
+ };
11
+ const loggerWithTags = (tags, options) => {
12
+ if (!Array.isArray(tags)) {
13
+ tags = [tags];
14
+ }
15
+ const { env, ...mergedOptions } = { ...defOptions, ...(options || {}) };
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);
19
+ };
20
+ 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
+ };
24
+ 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));
28
+ };
29
+ 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);
32
+ };
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
+ }
39
+ };
40
+ return { debug, info, log, warn, error };
41
+ };
42
+ exports.loggerWithTags = loggerWithTags;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * 日志输出打印的模式
3
+ * @default LogMode.ALL
4
+ */
5
+ export declare enum LogMode {
6
+ ALL = "all",
7
+ PRODUCTION = "production",
8
+ DEVELOPMENET = "development",
9
+ NONE = "none"
10
+ }
11
+ /** 日志输入配置选项 */
12
+ export interface ILogOptions {
13
+ /**
14
+ * 是否支持输出时间戳及时间戳格式
15
+ *
16
+ * 字符串格式参见: https://github.com/date-fns/date-fns/blob/main/src/format/index.ts
17
+ * @default true - 'MMM dd, yyyy HH:mm:ss.SSS'
18
+ */
19
+ date?: boolean | string;
20
+ /**
21
+ * 日志输出条件,默认全部输出
22
+ * @default 'all'
23
+ */
24
+ env?: LogMode;
25
+ }
26
+ export interface ILogger {
27
+ debug: (...rest: any[]) => void;
28
+ info: (...rest: any[]) => void;
29
+ log: (...rest: any[]) => void;
30
+ warn: (...rest: any[]) => void;
31
+ error: (msg: string) => void;
32
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LogMode = void 0;
4
+ /**
5
+ * 日志输出打印的模式
6
+ * @default LogMode.ALL
7
+ */
8
+ var LogMode;
9
+ (function (LogMode) {
10
+ LogMode["ALL"] = "all";
11
+ LogMode["PRODUCTION"] = "production";
12
+ LogMode["DEVELOPMENET"] = "development";
13
+ LogMode["NONE"] = "none";
14
+ })(LogMode = exports.LogMode || (exports.LogMode = {}));
package/lib/utils.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /// <reference types="node" />
2
+ import { ILogOptions, LogMode } from './interface';
3
+ export declare const isUndefined: (v: any) => boolean;
4
+ export declare const isString: (v: any) => boolean;
5
+ export declare const buildOutputPreffix: (tags: string[], options: ILogOptions) => (string | Date)[];
6
+ export declare const checkEnv: (env: LogMode) => boolean;
7
+ export declare const checkPlatform: () => Promise<typeof import("fs").writeFile>;
8
+ export declare const noop: () => undefined;
package/lib/utils.js ADDED
@@ -0,0 +1,57 @@
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.noop = exports.checkPlatform = exports.checkEnv = exports.buildOutputPreffix = exports.isString = exports.isUndefined = void 0;
27
+ const date_fns_1 = require("date-fns");
28
+ const toString = (v) => Object.prototype.toString.call(v);
29
+ const isUndefined = (v) => v === undefined;
30
+ exports.isUndefined = isUndefined;
31
+ const isString = (v) => typeof v === 'string' || toString(v) === '[object String]';
32
+ exports.isString = isString;
33
+ const buildOutputPreffix = (tags, options) => {
34
+ const prefix = [];
35
+ const { date } = options;
36
+ if (date) {
37
+ let now = new Date();
38
+ if ((0, exports.isString)(date)) {
39
+ now = (0, date_fns_1.format)(now, date);
40
+ }
41
+ prefix.unshift(now);
42
+ }
43
+ if (tags.length) {
44
+ prefix.unshift(tags.map((t) => `[${t.toUpperCase()}]`).join(' '));
45
+ }
46
+ return prefix;
47
+ };
48
+ exports.buildOutputPreffix = buildOutputPreffix;
49
+ const checkEnv = (env) => (env === 'all' || process.env.NODE_ENV === env) && console !== undefined;
50
+ exports.checkEnv = checkEnv;
51
+ const checkPlatform = () => Promise.resolve().then(() => __importStar(require('fs'))).then((fs) => {
52
+ // fs.opendir('logs')
53
+ return fs?.writeFile;
54
+ });
55
+ exports.checkPlatform = checkPlatform;
56
+ const noop = () => void 0;
57
+ exports.noop = noop;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@jeffchi/logger",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "lib/index.d.ts",
6
+ "description": "A log print output javascript tool library that can be used at the front and back ends",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/poechiang/jeffchi-logger.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/poechiang/jeffchi-logger/issues"
13
+ },
14
+ "homepage": "https://github.com/poechiang/jeffchi-logger#readme",
15
+ "keywords": [
16
+ "typescript",
17
+ "javascript",
18
+ "logger",
19
+ "log",
20
+ "warn",
21
+ "info",
22
+ "error",
23
+ "debug"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "prepare": "npm run build",
28
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
29
+ "lint": "tslint -p tsconfig.json",
30
+ "test": "jest --config jestconfig.json",
31
+ "prepublishOnly": "npm run test && npm run lint",
32
+ "preversion": "npm run lint",
33
+ "version": "npm run format && git add -A src",
34
+ "postversion": "git push && git push --tags"
35
+ },
36
+ "author": "jeffery·chiang",
37
+ "license": "MIT",
38
+ "files": [
39
+ "lib/**/*"
40
+ ],
41
+ "peerDependencies": {
42
+ "date-fns": "^2.29.3"
43
+ },
44
+ "devDependencies": {
45
+ "@types/jest": "^29.2.5",
46
+ "@types/node": "^18.11.18",
47
+ "jest": "^29.3.1",
48
+ "prettier": "^2.8.2",
49
+ "ts-jest": "^29.0.5",
50
+ "tslint": "^6.1.3",
51
+ "tslint-config-prettier": "^1.18.0",
52
+ "typescript": "^4.9.4"
53
+ }
54
+ }