@nebulaaa/journal 1.0.0 → 1.1.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 +128 -65
- package/dist/journal.d.ts +50 -40
- package/dist/journal.min.cjs +1 -1
- package/dist/journal.min.mjs +1 -1
- package/dist/journal.mjs +128 -65
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/journal.cjs
CHANGED
|
@@ -41,14 +41,13 @@ const CONSOLE_METHODS = {
|
|
|
41
41
|
/**
|
|
42
42
|
* 日志记录器
|
|
43
43
|
*
|
|
44
|
-
*
|
|
44
|
+
* 由 Journal 创建,持有独立分级;输出记录时受双重限制:
|
|
45
45
|
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
46
46
|
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
47
47
|
*/
|
|
48
48
|
class JournalLogger {
|
|
49
|
-
constructor(
|
|
49
|
+
constructor(options) {
|
|
50
50
|
var _a;
|
|
51
|
-
this._manager = manager;
|
|
52
51
|
this.name = options.name;
|
|
53
52
|
this.level = (_a = options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
54
53
|
}
|
|
@@ -78,13 +77,13 @@ class JournalLogger {
|
|
|
78
77
|
if (level === 'disable') {
|
|
79
78
|
return;
|
|
80
79
|
}
|
|
81
|
-
if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[
|
|
80
|
+
if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[GetLevel()]) {
|
|
82
81
|
return;
|
|
83
82
|
}
|
|
84
83
|
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.level]) {
|
|
85
84
|
return;
|
|
86
85
|
}
|
|
87
|
-
const style =
|
|
86
|
+
const style = GetLevelStyle(level);
|
|
88
87
|
const text = `[${this.name}] ${String(message)}`;
|
|
89
88
|
const method = CONSOLE_METHODS[level];
|
|
90
89
|
if (style.color) {
|
|
@@ -96,78 +95,142 @@ class JournalLogger {
|
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* 通过配置初始化,负责维护分级样式与记录器集合;
|
|
102
|
-
* 自身持有独立分级,作为全部记录器的输出门槛:
|
|
103
|
-
* 记录器分级低于管理器分级时,该记录器的记录一律忽略。
|
|
104
|
-
* 内置一个默认记录器,用于便捷记录,不占用记录器表。
|
|
98
|
+
* 日志记录器环境
|
|
105
99
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
100
|
+
const JournalEnv = {
|
|
101
|
+
_initialized: false,
|
|
102
|
+
_level: 'disable',
|
|
103
|
+
_logger: null,
|
|
104
|
+
_styles: Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES),
|
|
105
|
+
_loggers: new Map(),
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* 初始化;仅首次调用生效,重复调用将被警告并忽略
|
|
109
|
+
* @param options 管理器配置
|
|
110
|
+
*/
|
|
111
|
+
function JournalInit(options) {
|
|
112
|
+
var _a, _b, _c;
|
|
113
|
+
if (JournalEnv._initialized) {
|
|
114
|
+
console.warn('[Journal] 已初始化,请勿重复调用');
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
JournalEnv._initialized = true;
|
|
118
|
+
JournalEnv._level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
119
|
+
JournalEnv._logger = new JournalLogger({
|
|
120
|
+
name: JOURNAL_DEFAULT_LOGGER_NAME,
|
|
121
|
+
level: JournalEnv._level,
|
|
122
|
+
});
|
|
123
|
+
const overrides = options === null || options === void 0 ? void 0 : options.levels;
|
|
124
|
+
if (overrides) {
|
|
125
|
+
for (const level of JOURNAL_LEVELS) {
|
|
126
|
+
const style = overrides[level];
|
|
127
|
+
if (style) {
|
|
128
|
+
JournalEnv._styles[level] = Object.assign(Object.assign({}, JournalEnv._styles[level]), style);
|
|
119
129
|
}
|
|
120
130
|
}
|
|
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
131
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
|
|
132
|
+
for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
|
|
133
|
+
Create(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
|
|
133
134
|
}
|
|
134
135
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
136
|
+
* 日志管理器
|
|
137
|
+
*
|
|
138
|
+
* 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
|
|
138
139
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
140
|
+
return {
|
|
141
|
+
Acquire,
|
|
142
|
+
Create,
|
|
143
|
+
Remove,
|
|
144
|
+
Clear,
|
|
145
|
+
All,
|
|
146
|
+
GetLevel,
|
|
147
|
+
SetLevel,
|
|
148
|
+
GetLevelStyle,
|
|
149
|
+
SetLevelStyle,
|
|
150
|
+
Debug,
|
|
151
|
+
Info,
|
|
152
|
+
Warn,
|
|
153
|
+
Error,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/** 获取管理器分级 */
|
|
157
|
+
function GetLevel() {
|
|
158
|
+
return JournalEnv._level;
|
|
159
|
+
}
|
|
160
|
+
/** 设置管理器分级 */
|
|
161
|
+
function SetLevel(level) {
|
|
162
|
+
JournalEnv._level = level;
|
|
163
|
+
JournalEnv._logger.level = level;
|
|
164
|
+
}
|
|
165
|
+
/** 输出 debug 级别记录(默认记录器) */
|
|
166
|
+
function Debug(message, ...args) {
|
|
167
|
+
Log('debug', message, ...args);
|
|
168
|
+
}
|
|
169
|
+
/** 输出 info 级别记录(默认记录器) */
|
|
170
|
+
function Info(message, ...args) {
|
|
171
|
+
Log('info', message, ...args);
|
|
172
|
+
}
|
|
173
|
+
/** 输出 warn 级别记录(默认记录器) */
|
|
174
|
+
function Warn(message, ...args) {
|
|
175
|
+
Log('warn', message, ...args);
|
|
176
|
+
}
|
|
177
|
+
/** 输出 error 级别记录(默认记录器) */
|
|
178
|
+
function Error(message, ...args) {
|
|
179
|
+
Log('error', message, ...args);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 输出一条记录(默认记录器)
|
|
183
|
+
* @param level 记录分级
|
|
184
|
+
* @param message 记录内容
|
|
185
|
+
* @param args 附加内容,透传给 console
|
|
186
|
+
*/
|
|
187
|
+
function Log(level, message, ...args) {
|
|
188
|
+
JournalEnv._logger.log(level, message, ...args);
|
|
189
|
+
}
|
|
190
|
+
/** 获取分级样式 */
|
|
191
|
+
function GetLevelStyle(level) {
|
|
192
|
+
return JournalEnv._styles[level];
|
|
193
|
+
}
|
|
194
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
195
|
+
function SetLevelStyle(level, style) {
|
|
196
|
+
JournalEnv._styles[level] = Object.assign(Object.assign({}, JournalEnv._styles[level]), style);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
200
|
+
* @param name 记录器名称
|
|
201
|
+
* @param level 记录器分级,默认为 debug
|
|
202
|
+
*/
|
|
203
|
+
function Create(name, level = 'debug') {
|
|
204
|
+
const existing = JournalEnv._loggers.get(name);
|
|
205
|
+
if (existing) {
|
|
206
|
+
existing.level = level;
|
|
207
|
+
return existing;
|
|
165
208
|
}
|
|
209
|
+
const logger = new JournalLogger({ name, level });
|
|
210
|
+
JournalEnv._loggers.set(name, logger);
|
|
211
|
+
return logger;
|
|
212
|
+
}
|
|
213
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
214
|
+
function Acquire(name) {
|
|
215
|
+
var _a;
|
|
216
|
+
return (_a = JournalEnv._loggers.get(name)) !== null && _a !== void 0 ? _a : Create(name, JournalEnv._level);
|
|
217
|
+
}
|
|
218
|
+
/** 移除记录器,返回是否移除成功 */
|
|
219
|
+
function Remove(name) {
|
|
220
|
+
return JournalEnv._loggers.delete(name);
|
|
221
|
+
}
|
|
222
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
223
|
+
function Clear() {
|
|
224
|
+
JournalEnv._loggers.clear();
|
|
225
|
+
}
|
|
226
|
+
/** 获取全部记录器,不包含默认记录器 */
|
|
227
|
+
function All() {
|
|
228
|
+
return [...JournalEnv._loggers.values()];
|
|
166
229
|
}
|
|
167
230
|
|
|
168
231
|
exports.JOURNAL_DEFAULT_LEVEL_STYLES = JOURNAL_DEFAULT_LEVEL_STYLES;
|
|
169
232
|
exports.JOURNAL_DEFAULT_LOGGER_NAME = JOURNAL_DEFAULT_LOGGER_NAME;
|
|
170
233
|
exports.JOURNAL_LEVELS = JOURNAL_LEVELS;
|
|
171
234
|
exports.JOURNAL_LEVEL_WEIGHTS = JOURNAL_LEVEL_WEIGHTS;
|
|
172
|
-
exports.
|
|
235
|
+
exports.JournalInit = JournalInit;
|
|
173
236
|
exports.JournalLogger = JournalLogger;
|
package/dist/journal.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ declare const JOURNAL_DEFAULT_LOGGER_NAME = "journal";
|
|
|
57
57
|
/**
|
|
58
58
|
* 日志记录器
|
|
59
59
|
*
|
|
60
|
-
*
|
|
60
|
+
* 由 Journal 创建,持有独立分级;输出记录时受双重限制:
|
|
61
61
|
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
62
62
|
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
63
63
|
*/
|
|
@@ -66,9 +66,7 @@ declare class JournalLogger {
|
|
|
66
66
|
readonly name: string;
|
|
67
67
|
/** 记录器分级 */
|
|
68
68
|
level: JournalLevel;
|
|
69
|
-
|
|
70
|
-
private _manager;
|
|
71
|
-
constructor(manager: Journal, options: JournalLoggerOptions);
|
|
69
|
+
constructor(options: JournalLoggerOptions);
|
|
72
70
|
/** 输出 debug 级别记录 */
|
|
73
71
|
debug(message: unknown, ...args: unknown[]): void;
|
|
74
72
|
/** 输出 info 级别记录 */
|
|
@@ -86,42 +84,54 @@ declare class JournalLogger {
|
|
|
86
84
|
log(level: JournalLevel, message: unknown, ...args: unknown[]): void;
|
|
87
85
|
}
|
|
88
86
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* 通过配置初始化,负责维护分级样式与记录器集合;
|
|
92
|
-
* 自身持有独立分级,作为全部记录器的输出门槛:
|
|
93
|
-
* 记录器分级低于管理器分级时,该记录器的记录一律忽略。
|
|
94
|
-
* 内置一个默认记录器,用于便捷记录,不占用记录器表。
|
|
87
|
+
* 初始化;仅首次调用生效,重复调用将被警告并忽略
|
|
88
|
+
* @param options 管理器配置
|
|
95
89
|
*/
|
|
96
|
-
declare
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
90
|
+
declare function JournalInit(options?: JournalManagerOptions): {
|
|
91
|
+
Acquire: typeof Acquire;
|
|
92
|
+
Create: typeof Create;
|
|
93
|
+
Remove: typeof Remove;
|
|
94
|
+
Clear: typeof Clear;
|
|
95
|
+
All: typeof All;
|
|
96
|
+
GetLevel: typeof GetLevel;
|
|
97
|
+
SetLevel: typeof SetLevel;
|
|
98
|
+
GetLevelStyle: typeof GetLevelStyle;
|
|
99
|
+
SetLevelStyle: typeof SetLevelStyle;
|
|
100
|
+
Debug: typeof Debug;
|
|
101
|
+
Info: typeof Info;
|
|
102
|
+
Warn: typeof Warn;
|
|
103
|
+
Error: typeof Error;
|
|
104
|
+
};
|
|
105
|
+
/** 获取管理器分级 */
|
|
106
|
+
declare function GetLevel(): JournalLevel;
|
|
107
|
+
/** 设置管理器分级 */
|
|
108
|
+
declare function SetLevel(level: JournalLevel): void;
|
|
109
|
+
/** 输出 debug 级别记录(默认记录器) */
|
|
110
|
+
declare function Debug(message: unknown, ...args: unknown[]): void;
|
|
111
|
+
/** 输出 info 级别记录(默认记录器) */
|
|
112
|
+
declare function Info(message: unknown, ...args: unknown[]): void;
|
|
113
|
+
/** 输出 warn 级别记录(默认记录器) */
|
|
114
|
+
declare function Warn(message: unknown, ...args: unknown[]): void;
|
|
115
|
+
/** 输出 error 级别记录(默认记录器) */
|
|
116
|
+
declare function Error(message: unknown, ...args: unknown[]): void;
|
|
117
|
+
/** 获取分级样式 */
|
|
118
|
+
declare function GetLevelStyle(level: JournalLevel): Readonly<JournalLevelStyle>;
|
|
119
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
120
|
+
declare function SetLevelStyle(level: JournalLevel, style: Partial<JournalLevelStyle>): void;
|
|
121
|
+
/**
|
|
122
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
123
|
+
* @param name 记录器名称
|
|
124
|
+
* @param level 记录器分级,默认为 debug
|
|
125
|
+
*/
|
|
126
|
+
declare function Create(name: string, level?: JournalLevel): JournalLogger | null;
|
|
127
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
128
|
+
declare function Acquire(name: string): JournalLogger;
|
|
129
|
+
/** 移除记录器,返回是否移除成功 */
|
|
130
|
+
declare function Remove(name: string): boolean;
|
|
131
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
132
|
+
declare function Clear(): void;
|
|
133
|
+
/** 获取全部记录器,不包含默认记录器 */
|
|
134
|
+
declare function All(): JournalLogger[];
|
|
125
135
|
|
|
126
|
-
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS,
|
|
136
|
+
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, JournalInit, JournalLogger };
|
|
127
137
|
export type { JournalLevel, JournalLevelStyle, JournalLoggerOptions, JournalManagerOptions, JournalPrintLevel };
|
package/dist/journal.min.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=["debug","info","warn","error","disable"],l={debug:0,info:1,warn:2,error:3,disable:4},
|
|
1
|
+
"use strict";const e=["debug","info","warn","error","disable"],l={debug:0,info:1,warn:2,error:3,disable:4},o={debug:{label:"[Debug]",color:"#8C8C8C"},info:{label:"[Info]",color:"#0986FF"},warn:{label:"[Warn]",color:"#E6A23C"},error:{label:"[Error]",color:"#D93026"},disable:{label:"[Disable]",color:""}},n="journal",r={debug:"log",info:"info",warn:"warn",error:"error"};class t{constructor(e){var l;this.name=e.name,this.level=null!==(l=e.level)&&void 0!==l?l:"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,o,...n){if("disable"===e)return;if(l[this.level]<l[s()])return;if(l[e]<l[this.level])return;const t=_(e),i=`[${this.name}] ${String(o)}`,u=r[e];t.color?console[u](`%c${t.label}%c ${i}`,`color:${t.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...n):console[u](`${t.label} ${i}`,...n)}}const i={_initialized:!1,_level:"disable",_logger:null,_styles:Object.assign({},o),_loggers:new Map};function s(){return i._level}function u(e){i._level=e,i._logger.level=e}function g(e,...l){v("debug",e,...l)}function a(e,...l){v("info",e,...l)}function c(e,...l){v("warn",e,...l)}function f(e,...l){v("error",e,...l)}function v(e,l,...o){i._logger.log(e,l,...o)}function _(e){return i._styles[e]}function b(e,l){i._styles[e]=Object.assign(Object.assign({},i._styles[e]),l)}function d(e,l="debug"){const o=i._loggers.get(e);if(o)return o.level=l,o;const n=new t({name:e,level:l});return i._loggers.set(e,n),n}function L(e){var l;return null!==(l=i._loggers.get(e))&&void 0!==l?l:d(e,i._level)}function E(e){return i._loggers.delete(e)}function w(){i._loggers.clear()}function h(){return[...i._loggers.values()]}exports.JOURNAL_DEFAULT_LEVEL_STYLES=o,exports.JOURNAL_DEFAULT_LOGGER_NAME=n,exports.JOURNAL_LEVELS=e,exports.JOURNAL_LEVEL_WEIGHTS=l,exports.JournalInit=function(l){var o,r,v;if(i._initialized)return void console.warn("[Journal] 已初始化,请勿重复调用");i._initialized=!0,i._level=null!==(o=null==l?void 0:l.level)&&void 0!==o?o:"debug",i._logger=new t({name:n,level:i._level});const A=null==l?void 0:l.levels;if(A)for(const l of e){const e=A[l];e&&(i._styles[l]=Object.assign(Object.assign({},i._styles[l]),e))}for(const e of null!==(r=null==l?void 0:l.loggers)&&void 0!==r?r:[])d(e.name,null!==(v=e.level)&&void 0!==v?v:"debug");return{Acquire:L,Create:d,Remove:E,Clear:w,All:h,GetLevel:s,SetLevel:u,GetLevelStyle:_,SetLevelStyle:b,Debug:g,Info:a,Warn:c,Error:f}},exports.JournalLogger=t;
|
package/dist/journal.min.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=["debug","info","warn","error","disable"],l={debug:0,info:1,warn:2,error:3,disable:4},
|
|
1
|
+
const e=["debug","info","warn","error","disable"],l={debug:0,info:1,warn:2,error:3,disable:4},n={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",r={debug:"log",info:"info",warn:"warn",error:"error"};class t{constructor(e){var l;this.name=e.name,this.level=null!==(l=e.level)&&void 0!==l?l:"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,n,...o){if("disable"===e)return;if(l[this.level]<l[g()])return;if(l[e]<l[this.level])return;const t=d(e),i=`[${this.name}] ${String(n)}`,s=r[e];t.color?console[s](`%c${t.label}%c ${i}`,`color:${t.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...o):console[s](`${t.label} ${i}`,...o)}}const i={_initialized:!1,_level:"disable",_logger:null,_styles:Object.assign({},n),_loggers:new Map};function s(l){var n,r,s;if(i._initialized)return void console.warn("[Journal] 已初始化,请勿重复调用");i._initialized=!0,i._level=null!==(n=null==l?void 0:l.level)&&void 0!==n?n:"debug",i._logger=new t({name:o,level:i._level});const b=null==l?void 0:l.levels;if(b)for(const l of e){const e=b[l];e&&(i._styles[l]=Object.assign(Object.assign({},i._styles[l]),e))}for(const e of null!==(r=null==l?void 0:l.loggers)&&void 0!==r?r:[])w(e.name,null!==(s=e.level)&&void 0!==s?s:"debug");return{Acquire:h,Create:w,Remove:y,Clear:m,All:$,GetLevel:g,SetLevel:u,GetLevelStyle:d,SetLevelStyle:_,Debug:a,Info:c,Warn:f,Error:v}}function g(){return i._level}function u(e){i._level=e,i._logger.level=e}function a(e,...l){b("debug",e,...l)}function c(e,...l){b("info",e,...l)}function f(e,...l){b("warn",e,...l)}function v(e,...l){b("error",e,...l)}function b(e,l,...n){i._logger.log(e,l,...n)}function d(e){return i._styles[e]}function _(e,l){i._styles[e]=Object.assign(Object.assign({},i._styles[e]),l)}function w(e,l="debug"){const n=i._loggers.get(e);if(n)return n.level=l,n;const o=new t({name:e,level:l});return i._loggers.set(e,o),o}function h(e){var l;return null!==(l=i._loggers.get(e))&&void 0!==l?l:w(e,i._level)}function y(e){return i._loggers.delete(e)}function m(){i._loggers.clear()}function $(){return[...i._loggers.values()]}export{n as JOURNAL_DEFAULT_LEVEL_STYLES,o as JOURNAL_DEFAULT_LOGGER_NAME,e as JOURNAL_LEVELS,l as JOURNAL_LEVEL_WEIGHTS,s as JournalInit,t as JournalLogger};
|
package/dist/journal.mjs
CHANGED
|
@@ -39,14 +39,13 @@ const CONSOLE_METHODS = {
|
|
|
39
39
|
/**
|
|
40
40
|
* 日志记录器
|
|
41
41
|
*
|
|
42
|
-
*
|
|
42
|
+
* 由 Journal 创建,持有独立分级;输出记录时受双重限制:
|
|
43
43
|
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
44
44
|
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
45
45
|
*/
|
|
46
46
|
class JournalLogger {
|
|
47
|
-
constructor(
|
|
47
|
+
constructor(options) {
|
|
48
48
|
var _a;
|
|
49
|
-
this._manager = manager;
|
|
50
49
|
this.name = options.name;
|
|
51
50
|
this.level = (_a = options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
52
51
|
}
|
|
@@ -76,13 +75,13 @@ class JournalLogger {
|
|
|
76
75
|
if (level === 'disable') {
|
|
77
76
|
return;
|
|
78
77
|
}
|
|
79
|
-
if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[
|
|
78
|
+
if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[GetLevel()]) {
|
|
80
79
|
return;
|
|
81
80
|
}
|
|
82
81
|
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.level]) {
|
|
83
82
|
return;
|
|
84
83
|
}
|
|
85
|
-
const style =
|
|
84
|
+
const style = GetLevelStyle(level);
|
|
86
85
|
const text = `[${this.name}] ${String(message)}`;
|
|
87
86
|
const method = CONSOLE_METHODS[level];
|
|
88
87
|
if (style.color) {
|
|
@@ -94,73 +93,137 @@ class JournalLogger {
|
|
|
94
93
|
}
|
|
95
94
|
}
|
|
96
95
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* 通过配置初始化,负责维护分级样式与记录器集合;
|
|
100
|
-
* 自身持有独立分级,作为全部记录器的输出门槛:
|
|
101
|
-
* 记录器分级低于管理器分级时,该记录器的记录一律忽略。
|
|
102
|
-
* 内置一个默认记录器,用于便捷记录,不占用记录器表。
|
|
96
|
+
* 日志记录器环境
|
|
103
97
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
98
|
+
const JournalEnv = {
|
|
99
|
+
_initialized: false,
|
|
100
|
+
_level: 'disable',
|
|
101
|
+
_logger: null,
|
|
102
|
+
_styles: Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES),
|
|
103
|
+
_loggers: new Map(),
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* 初始化;仅首次调用生效,重复调用将被警告并忽略
|
|
107
|
+
* @param options 管理器配置
|
|
108
|
+
*/
|
|
109
|
+
function JournalInit(options) {
|
|
110
|
+
var _a, _b, _c;
|
|
111
|
+
if (JournalEnv._initialized) {
|
|
112
|
+
console.warn('[Journal] 已初始化,请勿重复调用');
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
JournalEnv._initialized = true;
|
|
116
|
+
JournalEnv._level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
117
|
+
JournalEnv._logger = new JournalLogger({
|
|
118
|
+
name: JOURNAL_DEFAULT_LOGGER_NAME,
|
|
119
|
+
level: JournalEnv._level,
|
|
120
|
+
});
|
|
121
|
+
const overrides = options === null || options === void 0 ? void 0 : options.levels;
|
|
122
|
+
if (overrides) {
|
|
123
|
+
for (const level of JOURNAL_LEVELS) {
|
|
124
|
+
const style = overrides[level];
|
|
125
|
+
if (style) {
|
|
126
|
+
JournalEnv._styles[level] = Object.assign(Object.assign({}, JournalEnv._styles[level]), style);
|
|
117
127
|
}
|
|
118
128
|
}
|
|
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
129
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
|
|
130
|
+
for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
|
|
131
|
+
Create(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
|
|
131
132
|
}
|
|
132
133
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
134
|
+
* 日志管理器
|
|
135
|
+
*
|
|
136
|
+
* 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
|
|
136
137
|
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
138
|
+
return {
|
|
139
|
+
Acquire,
|
|
140
|
+
Create,
|
|
141
|
+
Remove,
|
|
142
|
+
Clear,
|
|
143
|
+
All,
|
|
144
|
+
GetLevel,
|
|
145
|
+
SetLevel,
|
|
146
|
+
GetLevelStyle,
|
|
147
|
+
SetLevelStyle,
|
|
148
|
+
Debug,
|
|
149
|
+
Info,
|
|
150
|
+
Warn,
|
|
151
|
+
Error,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** 获取管理器分级 */
|
|
155
|
+
function GetLevel() {
|
|
156
|
+
return JournalEnv._level;
|
|
157
|
+
}
|
|
158
|
+
/** 设置管理器分级 */
|
|
159
|
+
function SetLevel(level) {
|
|
160
|
+
JournalEnv._level = level;
|
|
161
|
+
JournalEnv._logger.level = level;
|
|
162
|
+
}
|
|
163
|
+
/** 输出 debug 级别记录(默认记录器) */
|
|
164
|
+
function Debug(message, ...args) {
|
|
165
|
+
Log('debug', message, ...args);
|
|
166
|
+
}
|
|
167
|
+
/** 输出 info 级别记录(默认记录器) */
|
|
168
|
+
function Info(message, ...args) {
|
|
169
|
+
Log('info', message, ...args);
|
|
170
|
+
}
|
|
171
|
+
/** 输出 warn 级别记录(默认记录器) */
|
|
172
|
+
function Warn(message, ...args) {
|
|
173
|
+
Log('warn', message, ...args);
|
|
174
|
+
}
|
|
175
|
+
/** 输出 error 级别记录(默认记录器) */
|
|
176
|
+
function Error(message, ...args) {
|
|
177
|
+
Log('error', message, ...args);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 输出一条记录(默认记录器)
|
|
181
|
+
* @param level 记录分级
|
|
182
|
+
* @param message 记录内容
|
|
183
|
+
* @param args 附加内容,透传给 console
|
|
184
|
+
*/
|
|
185
|
+
function Log(level, message, ...args) {
|
|
186
|
+
JournalEnv._logger.log(level, message, ...args);
|
|
187
|
+
}
|
|
188
|
+
/** 获取分级样式 */
|
|
189
|
+
function GetLevelStyle(level) {
|
|
190
|
+
return JournalEnv._styles[level];
|
|
191
|
+
}
|
|
192
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
193
|
+
function SetLevelStyle(level, style) {
|
|
194
|
+
JournalEnv._styles[level] = Object.assign(Object.assign({}, JournalEnv._styles[level]), style);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
198
|
+
* @param name 记录器名称
|
|
199
|
+
* @param level 记录器分级,默认为 debug
|
|
200
|
+
*/
|
|
201
|
+
function Create(name, level = 'debug') {
|
|
202
|
+
const existing = JournalEnv._loggers.get(name);
|
|
203
|
+
if (existing) {
|
|
204
|
+
existing.level = level;
|
|
205
|
+
return existing;
|
|
163
206
|
}
|
|
207
|
+
const logger = new JournalLogger({ name, level });
|
|
208
|
+
JournalEnv._loggers.set(name, logger);
|
|
209
|
+
return logger;
|
|
210
|
+
}
|
|
211
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
212
|
+
function Acquire(name) {
|
|
213
|
+
var _a;
|
|
214
|
+
return (_a = JournalEnv._loggers.get(name)) !== null && _a !== void 0 ? _a : Create(name, JournalEnv._level);
|
|
215
|
+
}
|
|
216
|
+
/** 移除记录器,返回是否移除成功 */
|
|
217
|
+
function Remove(name) {
|
|
218
|
+
return JournalEnv._loggers.delete(name);
|
|
219
|
+
}
|
|
220
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
221
|
+
function Clear() {
|
|
222
|
+
JournalEnv._loggers.clear();
|
|
223
|
+
}
|
|
224
|
+
/** 获取全部记录器,不包含默认记录器 */
|
|
225
|
+
function All() {
|
|
226
|
+
return [...JournalEnv._loggers.values()];
|
|
164
227
|
}
|
|
165
228
|
|
|
166
|
-
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS,
|
|
229
|
+
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, JournalInit, JournalLogger };
|
package/dist/package.json
CHANGED