@nebulaaa/journal 0.0.0-stage → 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/dist/journal.cjs +173 -0
- package/dist/journal.d.ts +127 -0
- package/dist/journal.min.cjs +1 -0
- package/dist/journal.min.mjs +1 -0
- package/dist/journal.mjs +166 -0
- package/dist/package.json +34 -0
- package/package.json +66 -6
- package/README.md +0 -3
package/dist/journal.cjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*****************************************************************************
|
|
4
|
+
Author: DoooReyn
|
|
5
|
+
Feature: journal
|
|
6
|
+
Descriptions: 日志常量与默认配置
|
|
7
|
+
*****************************************************************************/
|
|
8
|
+
/** 全部分级,按权重由低到高排列 */
|
|
9
|
+
const JOURNAL_LEVELS = ['debug', 'info', 'warn', 'error', 'disable'];
|
|
10
|
+
/** 分级权重表:数值越大,输出门槛越高 */
|
|
11
|
+
const JOURNAL_LEVEL_WEIGHTS = {
|
|
12
|
+
debug: 0,
|
|
13
|
+
info: 1,
|
|
14
|
+
warn: 2,
|
|
15
|
+
error: 3,
|
|
16
|
+
disable: 4,
|
|
17
|
+
};
|
|
18
|
+
/** 默认分级样式 */
|
|
19
|
+
const JOURNAL_DEFAULT_LEVEL_STYLES = {
|
|
20
|
+
debug: { label: '[Debug]', color: '#8C8C8C' },
|
|
21
|
+
info: { label: '[Info]', color: '#0986FF' },
|
|
22
|
+
warn: { label: '[Warn]', color: '#E6A23C' },
|
|
23
|
+
error: { label: '[Error]', color: '#D93026' },
|
|
24
|
+
disable: { label: '[Disable]', color: '' },
|
|
25
|
+
};
|
|
26
|
+
/** 默认记录器名称 */
|
|
27
|
+
const JOURNAL_DEFAULT_LOGGER_NAME = 'journal';
|
|
28
|
+
|
|
29
|
+
/*****************************************************************************
|
|
30
|
+
Author: DoooReyn
|
|
31
|
+
Feature: journal
|
|
32
|
+
Descriptions: 日志管理器与记录器
|
|
33
|
+
*****************************************************************************/
|
|
34
|
+
/** 各分级对应的控制台输出方法(debug 走 log,兼容性更好) */
|
|
35
|
+
const CONSOLE_METHODS = {
|
|
36
|
+
debug: 'log',
|
|
37
|
+
info: 'info',
|
|
38
|
+
warn: 'warn',
|
|
39
|
+
error: 'error',
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* 日志记录器
|
|
43
|
+
*
|
|
44
|
+
* 由所属管理器创建,持有独立分级;输出记录时受双重限制:
|
|
45
|
+
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
46
|
+
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
47
|
+
*/
|
|
48
|
+
class JournalLogger {
|
|
49
|
+
constructor(manager, options) {
|
|
50
|
+
var _a;
|
|
51
|
+
this._manager = manager;
|
|
52
|
+
this.name = options.name;
|
|
53
|
+
this.level = (_a = options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
54
|
+
}
|
|
55
|
+
/** 输出 debug 级别记录 */
|
|
56
|
+
debug(message, ...args) {
|
|
57
|
+
this.log('debug', message, ...args);
|
|
58
|
+
}
|
|
59
|
+
/** 输出 info 级别记录 */
|
|
60
|
+
info(message, ...args) {
|
|
61
|
+
this.log('info', message, ...args);
|
|
62
|
+
}
|
|
63
|
+
/** 输出 warn 级别记录 */
|
|
64
|
+
warn(message, ...args) {
|
|
65
|
+
this.log('warn', message, ...args);
|
|
66
|
+
}
|
|
67
|
+
/** 输出 error 级别记录 */
|
|
68
|
+
error(message, ...args) {
|
|
69
|
+
this.log('error', message, ...args);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 输出一条记录,分级规则见类说明
|
|
73
|
+
* @param level 记录分级
|
|
74
|
+
* @param message 记录内容
|
|
75
|
+
* @param args 附加内容,透传给 console
|
|
76
|
+
*/
|
|
77
|
+
log(level, message, ...args) {
|
|
78
|
+
if (level === 'disable') {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[this._manager.level]) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.level]) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const style = this._manager.getLevelStyle(level);
|
|
88
|
+
const text = `[${this.name}] ${String(message)}`;
|
|
89
|
+
const method = CONSOLE_METHODS[level];
|
|
90
|
+
if (style.color) {
|
|
91
|
+
console[method](`%c${style.label}%c ${text}`, `color:${style.color};font-weight:bold;`, 'color:inherit;font-weight:inherit;', ...args);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
console[method](`${style.label} ${text}`, ...args);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 日志管理器
|
|
100
|
+
*
|
|
101
|
+
* 通过配置初始化,负责维护分级样式与记录器集合;
|
|
102
|
+
* 自身持有独立分级,作为全部记录器的输出门槛:
|
|
103
|
+
* 记录器分级低于管理器分级时,该记录器的记录一律忽略。
|
|
104
|
+
* 内置一个默认记录器,用于便捷记录,不占用记录器表。
|
|
105
|
+
*/
|
|
106
|
+
class Journal {
|
|
107
|
+
constructor(options) {
|
|
108
|
+
var _a, _b, _c;
|
|
109
|
+
this.level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
110
|
+
this.logger = new JournalLogger(this, { name: JOURNAL_DEFAULT_LOGGER_NAME, level: this.level });
|
|
111
|
+
this._styles = Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES);
|
|
112
|
+
const overrides = options === null || options === void 0 ? void 0 : options.levels;
|
|
113
|
+
if (overrides) {
|
|
114
|
+
for (const level of JOURNAL_LEVELS) {
|
|
115
|
+
const style = overrides[level];
|
|
116
|
+
if (style) {
|
|
117
|
+
this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
this._loggers = new Map();
|
|
122
|
+
for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
|
|
123
|
+
this.createLogger(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** 获取分级样式 */
|
|
127
|
+
getLevelStyle(level) {
|
|
128
|
+
return this._styles[level];
|
|
129
|
+
}
|
|
130
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
131
|
+
setLevelStyle(level, style) {
|
|
132
|
+
this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
136
|
+
* @param name 记录器名称
|
|
137
|
+
* @param level 记录器分级,默认为 debug
|
|
138
|
+
*/
|
|
139
|
+
createLogger(name, level = 'debug') {
|
|
140
|
+
const existing = this._loggers.get(name);
|
|
141
|
+
if (existing) {
|
|
142
|
+
existing.level = level;
|
|
143
|
+
return existing;
|
|
144
|
+
}
|
|
145
|
+
const logger = new JournalLogger(this, { name, level });
|
|
146
|
+
this._loggers.set(name, logger);
|
|
147
|
+
return logger;
|
|
148
|
+
}
|
|
149
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
150
|
+
getLogger(name) {
|
|
151
|
+
var _a;
|
|
152
|
+
return (_a = this._loggers.get(name)) !== null && _a !== void 0 ? _a : this.createLogger(name, this.level);
|
|
153
|
+
}
|
|
154
|
+
/** 移除记录器,返回是否移除成功 */
|
|
155
|
+
removeLogger(name) {
|
|
156
|
+
return this._loggers.delete(name);
|
|
157
|
+
}
|
|
158
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
159
|
+
clearLoggers() {
|
|
160
|
+
this._loggers.clear();
|
|
161
|
+
}
|
|
162
|
+
/** 获取全部记录器,不含默认记录器 */
|
|
163
|
+
getLoggers() {
|
|
164
|
+
return [...this._loggers.values()];
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
exports.JOURNAL_DEFAULT_LEVEL_STYLES = JOURNAL_DEFAULT_LEVEL_STYLES;
|
|
169
|
+
exports.JOURNAL_DEFAULT_LOGGER_NAME = JOURNAL_DEFAULT_LOGGER_NAME;
|
|
170
|
+
exports.JOURNAL_LEVELS = JOURNAL_LEVELS;
|
|
171
|
+
exports.JOURNAL_LEVEL_WEIGHTS = JOURNAL_LEVEL_WEIGHTS;
|
|
172
|
+
exports.Journal = Journal;
|
|
173
|
+
exports.JournalLogger = JournalLogger;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/*****************************************************************************
|
|
2
|
+
Author: DoooReyn
|
|
3
|
+
Feature: journal
|
|
4
|
+
Descriptions: 日志类型定义
|
|
5
|
+
*****************************************************************************/
|
|
6
|
+
/** 日志分级,权重由低到高,disable 表示不输出任何日志 */
|
|
7
|
+
type JournalLevel = 'debug' | 'info' | 'warn' | 'error' | 'disable';
|
|
8
|
+
/** 可用于输出记录的分级(不含 disable) */
|
|
9
|
+
type JournalPrintLevel = Exclude<JournalLevel, 'disable'>;
|
|
10
|
+
/** 分级样式:标记与颜色 */
|
|
11
|
+
interface JournalLevelStyle {
|
|
12
|
+
/** 分级标记,输出于日志头部,如 "[Debug]" */
|
|
13
|
+
label: string;
|
|
14
|
+
/** 分级颜色,支持任意 CSS 颜色值;留空表示不着色 */
|
|
15
|
+
color: string;
|
|
16
|
+
}
|
|
17
|
+
/** 记录器配置 */
|
|
18
|
+
interface JournalLoggerOptions {
|
|
19
|
+
/** 记录器名称,需非空且在所属管理器内唯一 */
|
|
20
|
+
name: string;
|
|
21
|
+
/** 记录器分级,默认为 debug */
|
|
22
|
+
level?: JournalLevel;
|
|
23
|
+
}
|
|
24
|
+
/** 日志管理器配置 */
|
|
25
|
+
interface JournalManagerOptions {
|
|
26
|
+
/** 管理器分级,默认为 debug */
|
|
27
|
+
level?: JournalLevel;
|
|
28
|
+
/** 分级样式配置,覆盖对应分级的默认标记与颜色 */
|
|
29
|
+
levels?: {
|
|
30
|
+
[L in JournalLevel]?: Partial<JournalLevelStyle>;
|
|
31
|
+
};
|
|
32
|
+
/** 初始记录器配置列表 */
|
|
33
|
+
loggers?: JournalLoggerOptions[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/*****************************************************************************
|
|
37
|
+
Author: DoooReyn
|
|
38
|
+
Feature: journal
|
|
39
|
+
Descriptions: 日志常量与默认配置
|
|
40
|
+
*****************************************************************************/
|
|
41
|
+
|
|
42
|
+
/** 全部分级,按权重由低到高排列 */
|
|
43
|
+
declare const JOURNAL_LEVELS: readonly ["debug", "info", "warn", "error", "disable"];
|
|
44
|
+
/** 分级权重表:数值越大,输出门槛越高 */
|
|
45
|
+
declare const JOURNAL_LEVEL_WEIGHTS: Record<JournalLevel, number>;
|
|
46
|
+
/** 默认分级样式 */
|
|
47
|
+
declare const JOURNAL_DEFAULT_LEVEL_STYLES: Record<JournalLevel, JournalLevelStyle>;
|
|
48
|
+
/** 默认记录器名称 */
|
|
49
|
+
declare const JOURNAL_DEFAULT_LOGGER_NAME = "journal";
|
|
50
|
+
|
|
51
|
+
/*****************************************************************************
|
|
52
|
+
Author: DoooReyn
|
|
53
|
+
Feature: journal
|
|
54
|
+
Descriptions: 日志管理器与记录器
|
|
55
|
+
*****************************************************************************/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 日志记录器
|
|
59
|
+
*
|
|
60
|
+
* 由所属管理器创建,持有独立分级;输出记录时受双重限制:
|
|
61
|
+
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
62
|
+
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
63
|
+
*/
|
|
64
|
+
declare class JournalLogger {
|
|
65
|
+
/** 记录器名称 */
|
|
66
|
+
readonly name: string;
|
|
67
|
+
/** 记录器分级 */
|
|
68
|
+
level: JournalLevel;
|
|
69
|
+
/** 所属管理器 */
|
|
70
|
+
private _manager;
|
|
71
|
+
constructor(manager: Journal, options: JournalLoggerOptions);
|
|
72
|
+
/** 输出 debug 级别记录 */
|
|
73
|
+
debug(message: unknown, ...args: unknown[]): void;
|
|
74
|
+
/** 输出 info 级别记录 */
|
|
75
|
+
info(message: unknown, ...args: unknown[]): void;
|
|
76
|
+
/** 输出 warn 级别记录 */
|
|
77
|
+
warn(message: unknown, ...args: unknown[]): void;
|
|
78
|
+
/** 输出 error 级别记录 */
|
|
79
|
+
error(message: unknown, ...args: unknown[]): void;
|
|
80
|
+
/**
|
|
81
|
+
* 输出一条记录,分级规则见类说明
|
|
82
|
+
* @param level 记录分级
|
|
83
|
+
* @param message 记录内容
|
|
84
|
+
* @param args 附加内容,透传给 console
|
|
85
|
+
*/
|
|
86
|
+
log(level: JournalLevel, message: unknown, ...args: unknown[]): void;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 日志管理器
|
|
90
|
+
*
|
|
91
|
+
* 通过配置初始化,负责维护分级样式与记录器集合;
|
|
92
|
+
* 自身持有独立分级,作为全部记录器的输出门槛:
|
|
93
|
+
* 记录器分级低于管理器分级时,该记录器的记录一律忽略。
|
|
94
|
+
* 内置一个默认记录器,用于便捷记录,不占用记录器表。
|
|
95
|
+
*/
|
|
96
|
+
declare class Journal {
|
|
97
|
+
/** 管理器分级 */
|
|
98
|
+
level: JournalLevel;
|
|
99
|
+
/** 默认记录器:初始分级与管理器一致,同样受管理器分级约束 */
|
|
100
|
+
readonly logger: JournalLogger;
|
|
101
|
+
/** 分级样式表 */
|
|
102
|
+
private _styles;
|
|
103
|
+
/** 记录器表 */
|
|
104
|
+
private _loggers;
|
|
105
|
+
constructor(options?: JournalManagerOptions);
|
|
106
|
+
/** 获取分级样式 */
|
|
107
|
+
getLevelStyle(level: JournalLevel): Readonly<JournalLevelStyle>;
|
|
108
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
109
|
+
setLevelStyle(level: JournalLevel, style: Partial<JournalLevelStyle>): void;
|
|
110
|
+
/**
|
|
111
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
112
|
+
* @param name 记录器名称
|
|
113
|
+
* @param level 记录器分级,默认为 debug
|
|
114
|
+
*/
|
|
115
|
+
createLogger(name: string, level?: JournalLevel): JournalLogger;
|
|
116
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
117
|
+
getLogger(name: string): JournalLogger;
|
|
118
|
+
/** 移除记录器,返回是否移除成功 */
|
|
119
|
+
removeLogger(name: string): boolean;
|
|
120
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
121
|
+
clearLoggers(): void;
|
|
122
|
+
/** 获取全部记录器,不含默认记录器 */
|
|
123
|
+
getLoggers(): JournalLogger[];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, Journal, JournalLogger };
|
|
127
|
+
export type { JournalLevel, JournalLevelStyle, JournalLoggerOptions, JournalManagerOptions, JournalPrintLevel };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const e=["debug","info","warn","error","disable"],l={debug:0,info:1,warn:2,error:3,disable:4},r={debug:{label:"[Debug]",color:"#8C8C8C"},info:{label:"[Info]",color:"#0986FF"},warn:{label:"[Warn]",color:"#E6A23C"},error:{label:"[Error]",color:"#D93026"},disable:{label:"[Disable]",color:""}},o="journal",s={debug:"log",info:"info",warn:"warn",error:"error"};class t{constructor(e,l){var r;this._manager=e,this.name=l.name,this.level=null!==(r=l.level)&&void 0!==r?r:"debug"}debug(e,...l){this.log("debug",e,...l)}info(e,...l){this.log("info",e,...l)}warn(e,...l){this.log("warn",e,...l)}error(e,...l){this.log("error",e,...l)}log(e,r,...o){if("disable"===e)return;if(l[this.level]<l[this._manager.level])return;if(l[e]<l[this.level])return;const t=this._manager.getLevelStyle(e),i=`[${this.name}] ${String(r)}`,n=s[e];t.color?console[n](`%c${t.label}%c ${i}`,`color:${t.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...o):console[n](`${t.label} ${i}`,...o)}}exports.JOURNAL_DEFAULT_LEVEL_STYLES=r,exports.JOURNAL_DEFAULT_LOGGER_NAME=o,exports.JOURNAL_LEVELS=e,exports.JOURNAL_LEVEL_WEIGHTS=l,exports.Journal=class{constructor(l){var s,i,n;this.level=null!==(s=null==l?void 0:l.level)&&void 0!==s?s:"debug",this.logger=new t(this,{name:o,level:this.level}),this._styles=Object.assign({},r);const g=null==l?void 0:l.levels;if(g)for(const l of e){const e=g[l];e&&(this._styles[l]=Object.assign(Object.assign({},this._styles[l]),e))}this._loggers=new Map;for(const e of null!==(i=null==l?void 0:l.loggers)&&void 0!==i?i:[])this.createLogger(e.name,null!==(n=e.level)&&void 0!==n?n:"debug")}getLevelStyle(e){return this._styles[e]}setLevelStyle(e,l){this._styles[e]=Object.assign(Object.assign({},this._styles[e]),l)}createLogger(e,l="debug"){const r=this._loggers.get(e);if(r)return r.level=l,r;const o=new t(this,{name:e,level:l});return this._loggers.set(e,o),o}getLogger(e){var l;return null!==(l=this._loggers.get(e))&&void 0!==l?l:this.createLogger(e,this.level)}removeLogger(e){return this._loggers.delete(e)}clearLoggers(){this._loggers.clear()}getLoggers(){return[...this._loggers.values()]}},exports.JournalLogger=t;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=["debug","info","warn","error","disable"],l={debug:0,info:1,warn:2,error:3,disable:4},r={debug:{label:"[Debug]",color:"#8C8C8C"},info:{label:"[Info]",color:"#0986FF"},warn:{label:"[Warn]",color:"#E6A23C"},error:{label:"[Error]",color:"#D93026"},disable:{label:"[Disable]",color:""}},o="journal",s={debug:"log",info:"info",warn:"warn",error:"error"};class t{constructor(e,l){var r;this._manager=e,this.name=l.name,this.level=null!==(r=l.level)&&void 0!==r?r:"debug"}debug(e,...l){this.log("debug",e,...l)}info(e,...l){this.log("info",e,...l)}warn(e,...l){this.log("warn",e,...l)}error(e,...l){this.log("error",e,...l)}log(e,r,...o){if("disable"===e)return;if(l[this.level]<l[this._manager.level])return;if(l[e]<l[this.level])return;const t=this._manager.getLevelStyle(e),i=`[${this.name}] ${String(r)}`,n=s[e];t.color?console[n](`%c${t.label}%c ${i}`,`color:${t.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...o):console[n](`${t.label} ${i}`,...o)}}class i{constructor(l){var s,i,n;this.level=null!==(s=null==l?void 0:l.level)&&void 0!==s?s:"debug",this.logger=new t(this,{name:o,level:this.level}),this._styles=Object.assign({},r);const g=null==l?void 0:l.levels;if(g)for(const l of e){const e=g[l];e&&(this._styles[l]=Object.assign(Object.assign({},this._styles[l]),e))}this._loggers=new Map;for(const e of null!==(i=null==l?void 0:l.loggers)&&void 0!==i?i:[])this.createLogger(e.name,null!==(n=e.level)&&void 0!==n?n:"debug")}getLevelStyle(e){return this._styles[e]}setLevelStyle(e,l){this._styles[e]=Object.assign(Object.assign({},this._styles[e]),l)}createLogger(e,l="debug"){const r=this._loggers.get(e);if(r)return r.level=l,r;const o=new t(this,{name:e,level:l});return this._loggers.set(e,o),o}getLogger(e){var l;return null!==(l=this._loggers.get(e))&&void 0!==l?l:this.createLogger(e,this.level)}removeLogger(e){return this._loggers.delete(e)}clearLoggers(){this._loggers.clear()}getLoggers(){return[...this._loggers.values()]}}export{r as JOURNAL_DEFAULT_LEVEL_STYLES,o as JOURNAL_DEFAULT_LOGGER_NAME,e as JOURNAL_LEVELS,l as JOURNAL_LEVEL_WEIGHTS,i as Journal,t as JournalLogger};
|
package/dist/journal.mjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/*****************************************************************************
|
|
2
|
+
Author: DoooReyn
|
|
3
|
+
Feature: journal
|
|
4
|
+
Descriptions: 日志常量与默认配置
|
|
5
|
+
*****************************************************************************/
|
|
6
|
+
/** 全部分级,按权重由低到高排列 */
|
|
7
|
+
const JOURNAL_LEVELS = ['debug', 'info', 'warn', 'error', 'disable'];
|
|
8
|
+
/** 分级权重表:数值越大,输出门槛越高 */
|
|
9
|
+
const JOURNAL_LEVEL_WEIGHTS = {
|
|
10
|
+
debug: 0,
|
|
11
|
+
info: 1,
|
|
12
|
+
warn: 2,
|
|
13
|
+
error: 3,
|
|
14
|
+
disable: 4,
|
|
15
|
+
};
|
|
16
|
+
/** 默认分级样式 */
|
|
17
|
+
const JOURNAL_DEFAULT_LEVEL_STYLES = {
|
|
18
|
+
debug: { label: '[Debug]', color: '#8C8C8C' },
|
|
19
|
+
info: { label: '[Info]', color: '#0986FF' },
|
|
20
|
+
warn: { label: '[Warn]', color: '#E6A23C' },
|
|
21
|
+
error: { label: '[Error]', color: '#D93026' },
|
|
22
|
+
disable: { label: '[Disable]', color: '' },
|
|
23
|
+
};
|
|
24
|
+
/** 默认记录器名称 */
|
|
25
|
+
const JOURNAL_DEFAULT_LOGGER_NAME = 'journal';
|
|
26
|
+
|
|
27
|
+
/*****************************************************************************
|
|
28
|
+
Author: DoooReyn
|
|
29
|
+
Feature: journal
|
|
30
|
+
Descriptions: 日志管理器与记录器
|
|
31
|
+
*****************************************************************************/
|
|
32
|
+
/** 各分级对应的控制台输出方法(debug 走 log,兼容性更好) */
|
|
33
|
+
const CONSOLE_METHODS = {
|
|
34
|
+
debug: 'log',
|
|
35
|
+
info: 'info',
|
|
36
|
+
warn: 'warn',
|
|
37
|
+
error: 'error',
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 日志记录器
|
|
41
|
+
*
|
|
42
|
+
* 由所属管理器创建,持有独立分级;输出记录时受双重限制:
|
|
43
|
+
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
44
|
+
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
45
|
+
*/
|
|
46
|
+
class JournalLogger {
|
|
47
|
+
constructor(manager, options) {
|
|
48
|
+
var _a;
|
|
49
|
+
this._manager = manager;
|
|
50
|
+
this.name = options.name;
|
|
51
|
+
this.level = (_a = options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
52
|
+
}
|
|
53
|
+
/** 输出 debug 级别记录 */
|
|
54
|
+
debug(message, ...args) {
|
|
55
|
+
this.log('debug', message, ...args);
|
|
56
|
+
}
|
|
57
|
+
/** 输出 info 级别记录 */
|
|
58
|
+
info(message, ...args) {
|
|
59
|
+
this.log('info', message, ...args);
|
|
60
|
+
}
|
|
61
|
+
/** 输出 warn 级别记录 */
|
|
62
|
+
warn(message, ...args) {
|
|
63
|
+
this.log('warn', message, ...args);
|
|
64
|
+
}
|
|
65
|
+
/** 输出 error 级别记录 */
|
|
66
|
+
error(message, ...args) {
|
|
67
|
+
this.log('error', message, ...args);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 输出一条记录,分级规则见类说明
|
|
71
|
+
* @param level 记录分级
|
|
72
|
+
* @param message 记录内容
|
|
73
|
+
* @param args 附加内容,透传给 console
|
|
74
|
+
*/
|
|
75
|
+
log(level, message, ...args) {
|
|
76
|
+
if (level === 'disable') {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[this._manager.level]) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.level]) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const style = this._manager.getLevelStyle(level);
|
|
86
|
+
const text = `[${this.name}] ${String(message)}`;
|
|
87
|
+
const method = CONSOLE_METHODS[level];
|
|
88
|
+
if (style.color) {
|
|
89
|
+
console[method](`%c${style.label}%c ${text}`, `color:${style.color};font-weight:bold;`, 'color:inherit;font-weight:inherit;', ...args);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
console[method](`${style.label} ${text}`, ...args);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 日志管理器
|
|
98
|
+
*
|
|
99
|
+
* 通过配置初始化,负责维护分级样式与记录器集合;
|
|
100
|
+
* 自身持有独立分级,作为全部记录器的输出门槛:
|
|
101
|
+
* 记录器分级低于管理器分级时,该记录器的记录一律忽略。
|
|
102
|
+
* 内置一个默认记录器,用于便捷记录,不占用记录器表。
|
|
103
|
+
*/
|
|
104
|
+
class Journal {
|
|
105
|
+
constructor(options) {
|
|
106
|
+
var _a, _b, _c;
|
|
107
|
+
this.level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
108
|
+
this.logger = new JournalLogger(this, { name: JOURNAL_DEFAULT_LOGGER_NAME, level: this.level });
|
|
109
|
+
this._styles = Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES);
|
|
110
|
+
const overrides = options === null || options === void 0 ? void 0 : options.levels;
|
|
111
|
+
if (overrides) {
|
|
112
|
+
for (const level of JOURNAL_LEVELS) {
|
|
113
|
+
const style = overrides[level];
|
|
114
|
+
if (style) {
|
|
115
|
+
this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
this._loggers = new Map();
|
|
120
|
+
for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
|
|
121
|
+
this.createLogger(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** 获取分级样式 */
|
|
125
|
+
getLevelStyle(level) {
|
|
126
|
+
return this._styles[level];
|
|
127
|
+
}
|
|
128
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
129
|
+
setLevelStyle(level, style) {
|
|
130
|
+
this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
134
|
+
* @param name 记录器名称
|
|
135
|
+
* @param level 记录器分级,默认为 debug
|
|
136
|
+
*/
|
|
137
|
+
createLogger(name, level = 'debug') {
|
|
138
|
+
const existing = this._loggers.get(name);
|
|
139
|
+
if (existing) {
|
|
140
|
+
existing.level = level;
|
|
141
|
+
return existing;
|
|
142
|
+
}
|
|
143
|
+
const logger = new JournalLogger(this, { name, level });
|
|
144
|
+
this._loggers.set(name, logger);
|
|
145
|
+
return logger;
|
|
146
|
+
}
|
|
147
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
148
|
+
getLogger(name) {
|
|
149
|
+
var _a;
|
|
150
|
+
return (_a = this._loggers.get(name)) !== null && _a !== void 0 ? _a : this.createLogger(name, this.level);
|
|
151
|
+
}
|
|
152
|
+
/** 移除记录器,返回是否移除成功 */
|
|
153
|
+
removeLogger(name) {
|
|
154
|
+
return this._loggers.delete(name);
|
|
155
|
+
}
|
|
156
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
157
|
+
clearLoggers() {
|
|
158
|
+
this._loggers.clear();
|
|
159
|
+
}
|
|
160
|
+
/** 获取全部记录器,不含默认记录器 */
|
|
161
|
+
getLoggers() {
|
|
162
|
+
return [...this._loggers.values()];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, Journal, JournalLogger };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nebulaaa/journal",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "日志记录扩展包",
|
|
5
|
+
"author": "doooreyn",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"journal",
|
|
9
|
+
"nebulaaa"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=16.0.0",
|
|
13
|
+
"npm": ">=10.0.0"
|
|
14
|
+
},
|
|
15
|
+
"main": "./journal.cjs",
|
|
16
|
+
"module": "./journal.mjs",
|
|
17
|
+
"types": "./journal.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./journal.d.ts",
|
|
21
|
+
"require": "./journal.cjs",
|
|
22
|
+
"import": "./journal.mjs",
|
|
23
|
+
"default": "./journal.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./min": {
|
|
26
|
+
"require": "./journal.min.cjs",
|
|
27
|
+
"import": "./journal.min.mjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"registry": "https://registry.npmjs.org/",
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@nebulaaa/journal",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@nebulaaa/journal",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "日志记录扩展包",
|
|
5
|
+
"main": "./dist/journal.cjs",
|
|
6
|
+
"module": "./dist/journal.mjs",
|
|
7
|
+
"types": "./dist/journal.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/journal.cjs",
|
|
11
|
+
"import": "./dist/journal.mjs",
|
|
12
|
+
"types": "./dist/journal.d.ts",
|
|
13
|
+
"default": "./dist/journal.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./min": {
|
|
16
|
+
"require": "./dist/journal.min.cjs",
|
|
17
|
+
"import": "./dist/journal.min.mjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
25
|
+
"build": "npm run clean && rollup -c rollup.config.mjs && node ../scripts/write-dist-package.mjs journal",
|
|
26
|
+
"test": "jest --config jest.config.mjs"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"journal",
|
|
30
|
+
"nebulaaa"
|
|
31
|
+
],
|
|
32
|
+
"author": "doooreyn",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"devEngines": {
|
|
35
|
+
"packageManager": {
|
|
36
|
+
"name": "npm",
|
|
37
|
+
"version": ">=10.0.0",
|
|
38
|
+
"onFail": "warn"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=16.0.0",
|
|
43
|
+
"npm": ">=10.0.0"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"registry": "https://registry.npmjs.org/",
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"allowScripts": {
|
|
50
|
+
"unrs-resolver@1.12.2": true,
|
|
51
|
+
"@parcel/watcher@2.6.0": true
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
55
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
56
|
+
"@types/node": "^22.10.2",
|
|
57
|
+
"@types/jest": "^30.0.0",
|
|
58
|
+
"ts-jest": "^29.4.12",
|
|
59
|
+
"jest": "^30.5.2",
|
|
60
|
+
"rollup": "^4.28.1",
|
|
61
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
62
|
+
"ts-node": "^10.9.2",
|
|
63
|
+
"typescript": "5.7.3"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {}
|
|
66
|
+
}
|
package/README.md
DELETED