@nebulaaa/journal 1.1.0 → 1.1.2
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 +38 -43
- package/dist/journal.d.ts +60 -54
- package/dist/journal.min.cjs +1 -1
- package/dist/journal.min.mjs +1 -1
- package/dist/journal.mjs +38 -43
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/journal.cjs
CHANGED
|
@@ -48,24 +48,24 @@ const CONSOLE_METHODS = {
|
|
|
48
48
|
class JournalLogger {
|
|
49
49
|
constructor(options) {
|
|
50
50
|
var _a;
|
|
51
|
-
this.
|
|
52
|
-
this.
|
|
51
|
+
this.Name = options.name;
|
|
52
|
+
this.Level = (_a = options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
53
53
|
}
|
|
54
54
|
/** 输出 debug 级别记录 */
|
|
55
|
-
|
|
56
|
-
this.
|
|
55
|
+
Debug(message, ...args) {
|
|
56
|
+
this.Log('debug', message, ...args);
|
|
57
57
|
}
|
|
58
58
|
/** 输出 info 级别记录 */
|
|
59
|
-
|
|
60
|
-
this.
|
|
59
|
+
Info(message, ...args) {
|
|
60
|
+
this.Log('info', message, ...args);
|
|
61
61
|
}
|
|
62
62
|
/** 输出 warn 级别记录 */
|
|
63
|
-
|
|
64
|
-
this.
|
|
63
|
+
Warn(message, ...args) {
|
|
64
|
+
this.Log('warn', message, ...args);
|
|
65
65
|
}
|
|
66
66
|
/** 输出 error 级别记录 */
|
|
67
|
-
|
|
68
|
-
this.
|
|
67
|
+
Error(message, ...args) {
|
|
68
|
+
this.Log('error', message, ...args);
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
71
71
|
* 输出一条记录,分级规则见类说明
|
|
@@ -73,18 +73,18 @@ class JournalLogger {
|
|
|
73
73
|
* @param message 记录内容
|
|
74
74
|
* @param args 附加内容,透传给 console
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
Log(level, message, ...args) {
|
|
77
77
|
if (level === 'disable') {
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
|
-
if (JOURNAL_LEVEL_WEIGHTS[this.
|
|
80
|
+
if (JOURNAL_LEVEL_WEIGHTS[this.Level] < JOURNAL_LEVEL_WEIGHTS[GetLevel()]) {
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
|
-
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.
|
|
83
|
+
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.Level]) {
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
const style = GetLevelStyle(level);
|
|
87
|
-
const text = `[${this.
|
|
87
|
+
const text = `[${this.Name}] ${String(message)}`;
|
|
88
88
|
const method = CONSOLE_METHODS[level];
|
|
89
89
|
if (style.color) {
|
|
90
90
|
console[method](`%c${style.label}%c ${text}`, `color:${style.color};font-weight:bold;`, 'color:inherit;font-weight:inherit;', ...args);
|
|
@@ -95,10 +95,10 @@ class JournalLogger {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
|
-
*
|
|
98
|
+
* 日志记录器上下文
|
|
99
99
|
*/
|
|
100
|
-
const
|
|
101
|
-
|
|
100
|
+
const JournalContext = {
|
|
101
|
+
_instance: null,
|
|
102
102
|
_level: 'disable',
|
|
103
103
|
_logger: null,
|
|
104
104
|
_styles: Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES),
|
|
@@ -110,34 +110,28 @@ const JournalEnv = {
|
|
|
110
110
|
*/
|
|
111
111
|
function JournalInit(options) {
|
|
112
112
|
var _a, _b, _c;
|
|
113
|
-
if (
|
|
113
|
+
if (JournalContext._instance) {
|
|
114
114
|
console.warn('[Journal] 已初始化,请勿重复调用');
|
|
115
|
-
return;
|
|
115
|
+
return JournalContext._instance;
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
JournalEnv._logger = new JournalLogger({
|
|
117
|
+
JournalContext._level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
118
|
+
JournalContext._logger = new JournalLogger({
|
|
120
119
|
name: JOURNAL_DEFAULT_LOGGER_NAME,
|
|
121
|
-
level:
|
|
120
|
+
level: JournalContext._level,
|
|
122
121
|
});
|
|
123
122
|
const overrides = options === null || options === void 0 ? void 0 : options.levels;
|
|
124
123
|
if (overrides) {
|
|
125
124
|
for (const level of JOURNAL_LEVELS) {
|
|
126
125
|
const style = overrides[level];
|
|
127
126
|
if (style) {
|
|
128
|
-
|
|
127
|
+
JournalContext._styles[level] = Object.assign(Object.assign({}, JournalContext._styles[level]), style);
|
|
129
128
|
}
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
|
|
133
132
|
Create(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
|
|
134
133
|
}
|
|
135
|
-
|
|
136
|
-
* 日志管理器
|
|
137
|
-
*
|
|
138
|
-
* 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
|
|
139
|
-
*/
|
|
140
|
-
return {
|
|
134
|
+
JournalContext._instance = {
|
|
141
135
|
Acquire,
|
|
142
136
|
Create,
|
|
143
137
|
Remove,
|
|
@@ -152,15 +146,16 @@ function JournalInit(options) {
|
|
|
152
146
|
Warn,
|
|
153
147
|
Error,
|
|
154
148
|
};
|
|
149
|
+
return JournalContext._instance;
|
|
155
150
|
}
|
|
156
151
|
/** 获取管理器分级 */
|
|
157
152
|
function GetLevel() {
|
|
158
|
-
return
|
|
153
|
+
return JournalContext._level;
|
|
159
154
|
}
|
|
160
155
|
/** 设置管理器分级 */
|
|
161
156
|
function SetLevel(level) {
|
|
162
|
-
|
|
163
|
-
|
|
157
|
+
JournalContext._level = level;
|
|
158
|
+
JournalContext._logger.Level = level;
|
|
164
159
|
}
|
|
165
160
|
/** 输出 debug 级别记录(默认记录器) */
|
|
166
161
|
function Debug(message, ...args) {
|
|
@@ -185,15 +180,15 @@ function Error(message, ...args) {
|
|
|
185
180
|
* @param args 附加内容,透传给 console
|
|
186
181
|
*/
|
|
187
182
|
function Log(level, message, ...args) {
|
|
188
|
-
|
|
183
|
+
JournalContext._logger.Log(level, message, ...args);
|
|
189
184
|
}
|
|
190
185
|
/** 获取分级样式 */
|
|
191
186
|
function GetLevelStyle(level) {
|
|
192
|
-
return
|
|
187
|
+
return JournalContext._styles[level];
|
|
193
188
|
}
|
|
194
189
|
/** 更新分级样式,仅覆盖传入字段 */
|
|
195
190
|
function SetLevelStyle(level, style) {
|
|
196
|
-
|
|
191
|
+
JournalContext._styles[level] = Object.assign(Object.assign({}, JournalContext._styles[level]), style);
|
|
197
192
|
}
|
|
198
193
|
/**
|
|
199
194
|
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
@@ -201,31 +196,31 @@ function SetLevelStyle(level, style) {
|
|
|
201
196
|
* @param level 记录器分级,默认为 debug
|
|
202
197
|
*/
|
|
203
198
|
function Create(name, level = 'debug') {
|
|
204
|
-
const existing =
|
|
199
|
+
const existing = JournalContext._loggers.get(name);
|
|
205
200
|
if (existing) {
|
|
206
|
-
existing.
|
|
201
|
+
existing.Level = level;
|
|
207
202
|
return existing;
|
|
208
203
|
}
|
|
209
204
|
const logger = new JournalLogger({ name, level });
|
|
210
|
-
|
|
205
|
+
JournalContext._loggers.set(name, logger);
|
|
211
206
|
return logger;
|
|
212
207
|
}
|
|
213
208
|
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
214
209
|
function Acquire(name) {
|
|
215
210
|
var _a;
|
|
216
|
-
return (_a =
|
|
211
|
+
return (_a = JournalContext._loggers.get(name)) !== null && _a !== void 0 ? _a : Create(name, JournalContext._level);
|
|
217
212
|
}
|
|
218
213
|
/** 移除记录器,返回是否移除成功 */
|
|
219
214
|
function Remove(name) {
|
|
220
|
-
return
|
|
215
|
+
return JournalContext._loggers.delete(name);
|
|
221
216
|
}
|
|
222
217
|
/** 清空全部记录器,不影响默认记录器 */
|
|
223
218
|
function Clear() {
|
|
224
|
-
|
|
219
|
+
JournalContext._loggers.clear();
|
|
225
220
|
}
|
|
226
221
|
/** 获取全部记录器,不包含默认记录器 */
|
|
227
222
|
function All() {
|
|
228
|
-
return [...
|
|
223
|
+
return [...JournalContext._loggers.values()];
|
|
229
224
|
}
|
|
230
225
|
|
|
231
226
|
exports.JOURNAL_DEFAULT_LEVEL_STYLES = JOURNAL_DEFAULT_LEVEL_STYLES;
|
package/dist/journal.d.ts
CHANGED
|
@@ -32,6 +32,56 @@ interface JournalManagerOptions {
|
|
|
32
32
|
/** 初始记录器配置列表 */
|
|
33
33
|
loggers?: JournalLoggerOptions[];
|
|
34
34
|
}
|
|
35
|
+
/** 日志记录器 */
|
|
36
|
+
interface IJournalLogger {
|
|
37
|
+
/** 记录器名称 */
|
|
38
|
+
readonly Name: string;
|
|
39
|
+
/** 记录器分级 */
|
|
40
|
+
Level: JournalLevel;
|
|
41
|
+
/** 输出 debug 级别记录 */
|
|
42
|
+
Debug(message: string, ...args: unknown[]): void;
|
|
43
|
+
/** 输出 info 级别记录 */
|
|
44
|
+
Info(message: string, ...args: unknown[]): void;
|
|
45
|
+
/** 输出 warn 级别记录 */
|
|
46
|
+
Warn(message: string, ...args: unknown[]): void;
|
|
47
|
+
/** 输出 error 级别记录 */
|
|
48
|
+
Error(message: string, ...args: unknown[]): void;
|
|
49
|
+
/** 输出分级记录 */
|
|
50
|
+
Log(level: JournalLevel, message: string, ...args: unknown[]): void;
|
|
51
|
+
}
|
|
52
|
+
/** 日志管理器 */
|
|
53
|
+
interface IJournal {
|
|
54
|
+
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
55
|
+
Acquire(name: string): IJournalLogger;
|
|
56
|
+
/**
|
|
57
|
+
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
58
|
+
* @param name 记录器名称
|
|
59
|
+
* @param level 记录器分级,默认为 debug
|
|
60
|
+
*/
|
|
61
|
+
Create(name: string, level?: JournalLevel): IJournalLogger | null;
|
|
62
|
+
/** 移除记录器,返回是否移除成功 */
|
|
63
|
+
Remove(name: string): boolean;
|
|
64
|
+
/** 清空全部记录器,不影响默认记录器 */
|
|
65
|
+
Clear(): void;
|
|
66
|
+
/** 获取全部记录器,不包含默认记录器 */
|
|
67
|
+
All(): IJournalLogger[];
|
|
68
|
+
/** 获取管理器分级 */
|
|
69
|
+
GetLevel(): JournalLevel;
|
|
70
|
+
/** 设置管理器分级 */
|
|
71
|
+
SetLevel(level: JournalLevel): void;
|
|
72
|
+
/** 获取分级样式 */
|
|
73
|
+
GetLevelStyle(level: JournalLevel): Readonly<JournalLevelStyle>;
|
|
74
|
+
/** 更新分级样式,仅覆盖传入字段 */
|
|
75
|
+
SetLevelStyle(level: JournalLevel, style: Partial<JournalLevelStyle>): void;
|
|
76
|
+
/** 输出 debug 级别记录(默认记录器) */
|
|
77
|
+
Debug: (message: string, ...args: unknown[]) => void;
|
|
78
|
+
/** 输出 info 级别记录(默认记录器) */
|
|
79
|
+
Info: (message: string, ...args: unknown[]) => void;
|
|
80
|
+
/** 输出 warn 级别记录(默认记录器) */
|
|
81
|
+
Warn: (message: string, ...args: unknown[]) => void;
|
|
82
|
+
/** 输出 error 级别记录(默认记录器) */
|
|
83
|
+
Error: (message: string, ...args: unknown[]) => void;
|
|
84
|
+
}
|
|
35
85
|
|
|
36
86
|
/*****************************************************************************
|
|
37
87
|
Author: DoooReyn
|
|
@@ -61,77 +111,33 @@ declare const JOURNAL_DEFAULT_LOGGER_NAME = "journal";
|
|
|
61
111
|
* 1. 记录器分级低于所属管理器分级时,该记录器整体视为忽略;
|
|
62
112
|
* 2. 记录分级低于记录器分级时,该条记录视为忽略。
|
|
63
113
|
*/
|
|
64
|
-
declare class JournalLogger {
|
|
114
|
+
declare class JournalLogger implements IJournalLogger {
|
|
65
115
|
/** 记录器名称 */
|
|
66
|
-
readonly
|
|
116
|
+
readonly Name: string;
|
|
67
117
|
/** 记录器分级 */
|
|
68
|
-
|
|
118
|
+
Level: JournalLevel;
|
|
69
119
|
constructor(options: JournalLoggerOptions);
|
|
70
120
|
/** 输出 debug 级别记录 */
|
|
71
|
-
|
|
121
|
+
Debug(message: string, ...args: unknown[]): void;
|
|
72
122
|
/** 输出 info 级别记录 */
|
|
73
|
-
|
|
123
|
+
Info(message: string, ...args: unknown[]): void;
|
|
74
124
|
/** 输出 warn 级别记录 */
|
|
75
|
-
|
|
125
|
+
Warn(message: string, ...args: unknown[]): void;
|
|
76
126
|
/** 输出 error 级别记录 */
|
|
77
|
-
|
|
127
|
+
Error(message: string, ...args: unknown[]): void;
|
|
78
128
|
/**
|
|
79
129
|
* 输出一条记录,分级规则见类说明
|
|
80
130
|
* @param level 记录分级
|
|
81
131
|
* @param message 记录内容
|
|
82
132
|
* @param args 附加内容,透传给 console
|
|
83
133
|
*/
|
|
84
|
-
|
|
134
|
+
Log(level: JournalLevel, message: string, ...args: unknown[]): void;
|
|
85
135
|
}
|
|
86
136
|
/**
|
|
87
137
|
* 初始化;仅首次调用生效,重复调用将被警告并忽略
|
|
88
138
|
* @param options 管理器配置
|
|
89
139
|
*/
|
|
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[];
|
|
140
|
+
declare function JournalInit(options?: JournalManagerOptions): IJournal;
|
|
135
141
|
|
|
136
142
|
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, JournalInit, JournalLogger };
|
|
137
|
-
export type { JournalLevel, JournalLevelStyle, JournalLoggerOptions, JournalManagerOptions, JournalPrintLevel };
|
|
143
|
+
export type { IJournal, IJournalLogger, 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},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[i()])return;if(l[e]<l[this.Level])return;const t=v(e),s=`[${this.Name}] ${String(n)}`,u=r[e];t.color?console[u](`%c${t.label}%c ${s}`,`color:${t.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...o):console[u](`${t.label} ${s}`,...o)}}const s={_instance:null,_level:"disable",_logger:null,_styles:Object.assign({},n),_loggers:new Map};function i(){return s._level}function u(e){s._level=e,s._logger.Level=e}function g(e,...l){_("debug",e,...l)}function c(e,...l){_("info",e,...l)}function a(e,...l){_("warn",e,...l)}function f(e,...l){_("error",e,...l)}function _(e,l,...n){s._logger.Log(e,l,...n)}function v(e){return s._styles[e]}function b(e,l){s._styles[e]=Object.assign(Object.assign({},s._styles[e]),l)}function L(e,l="debug"){const n=s._loggers.get(e);if(n)return n.Level=l,n;const o=new t({name:e,level:l});return s._loggers.set(e,o),o}function d(e){var l;return null!==(l=s._loggers.get(e))&&void 0!==l?l:L(e,s._level)}function E(e){return s._loggers.delete(e)}function h(){s._loggers.clear()}function w(){return[...s._loggers.values()]}exports.JOURNAL_DEFAULT_LEVEL_STYLES=n,exports.JOURNAL_DEFAULT_LOGGER_NAME=o,exports.JOURNAL_LEVELS=e,exports.JOURNAL_LEVEL_WEIGHTS=l,exports.JournalInit=function(l){var n,r,_;if(s._instance)return console.warn("[Journal] 已初始化,请勿重复调用"),s._instance;s._level=null!==(n=null==l?void 0:l.level)&&void 0!==n?n:"debug",s._logger=new t({name:o,level:s._level});const A=null==l?void 0:l.levels;if(A)for(const l of e){const e=A[l];e&&(s._styles[l]=Object.assign(Object.assign({},s._styles[l]),e))}for(const e of null!==(r=null==l?void 0:l.loggers)&&void 0!==r?r:[])L(e.name,null!==(_=e.level)&&void 0!==_?_:"debug");return s._instance={Acquire:d,Create:L,Remove:E,Clear:h,All:w,GetLevel:i,SetLevel:u,GetLevelStyle:v,SetLevelStyle:b,Debug:g,Info:c,Warn:a,Error:f},s._instance},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},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.
|
|
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[u()])return;if(l[e]<l[this.Level])return;const t=_(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={_instance:null,_level:"disable",_logger:null,_styles:Object.assign({},n),_loggers:new Map};function s(l){var n,r,s;if(i._instance)return console.warn("[Journal] 已初始化,请勿重复调用"),i._instance;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:[])L(e.name,null!==(s=e.level)&&void 0!==s?s:"debug");return i._instance={Acquire:h,Create:L,Remove:w,Clear:y,All:m,GetLevel:u,SetLevel:g,GetLevelStyle:_,SetLevelStyle:d,Debug:c,Info:a,Warn:f,Error:v},i._instance}function u(){return i._level}function g(e){i._level=e,i._logger.Level=e}function c(e,...l){b("debug",e,...l)}function a(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 _(e){return i._styles[e]}function d(e,l){i._styles[e]=Object.assign(Object.assign({},i._styles[e]),l)}function L(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:L(e,i._level)}function w(e){return i._loggers.delete(e)}function y(){i._loggers.clear()}function m(){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
|
@@ -46,24 +46,24 @@ const CONSOLE_METHODS = {
|
|
|
46
46
|
class JournalLogger {
|
|
47
47
|
constructor(options) {
|
|
48
48
|
var _a;
|
|
49
|
-
this.
|
|
50
|
-
this.
|
|
49
|
+
this.Name = options.name;
|
|
50
|
+
this.Level = (_a = options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
51
51
|
}
|
|
52
52
|
/** 输出 debug 级别记录 */
|
|
53
|
-
|
|
54
|
-
this.
|
|
53
|
+
Debug(message, ...args) {
|
|
54
|
+
this.Log('debug', message, ...args);
|
|
55
55
|
}
|
|
56
56
|
/** 输出 info 级别记录 */
|
|
57
|
-
|
|
58
|
-
this.
|
|
57
|
+
Info(message, ...args) {
|
|
58
|
+
this.Log('info', message, ...args);
|
|
59
59
|
}
|
|
60
60
|
/** 输出 warn 级别记录 */
|
|
61
|
-
|
|
62
|
-
this.
|
|
61
|
+
Warn(message, ...args) {
|
|
62
|
+
this.Log('warn', message, ...args);
|
|
63
63
|
}
|
|
64
64
|
/** 输出 error 级别记录 */
|
|
65
|
-
|
|
66
|
-
this.
|
|
65
|
+
Error(message, ...args) {
|
|
66
|
+
this.Log('error', message, ...args);
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* 输出一条记录,分级规则见类说明
|
|
@@ -71,18 +71,18 @@ class JournalLogger {
|
|
|
71
71
|
* @param message 记录内容
|
|
72
72
|
* @param args 附加内容,透传给 console
|
|
73
73
|
*/
|
|
74
|
-
|
|
74
|
+
Log(level, message, ...args) {
|
|
75
75
|
if (level === 'disable') {
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
|
-
if (JOURNAL_LEVEL_WEIGHTS[this.
|
|
78
|
+
if (JOURNAL_LEVEL_WEIGHTS[this.Level] < JOURNAL_LEVEL_WEIGHTS[GetLevel()]) {
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
|
-
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.
|
|
81
|
+
if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.Level]) {
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
84
84
|
const style = GetLevelStyle(level);
|
|
85
|
-
const text = `[${this.
|
|
85
|
+
const text = `[${this.Name}] ${String(message)}`;
|
|
86
86
|
const method = CONSOLE_METHODS[level];
|
|
87
87
|
if (style.color) {
|
|
88
88
|
console[method](`%c${style.label}%c ${text}`, `color:${style.color};font-weight:bold;`, 'color:inherit;font-weight:inherit;', ...args);
|
|
@@ -93,10 +93,10 @@ class JournalLogger {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
96
|
+
* 日志记录器上下文
|
|
97
97
|
*/
|
|
98
|
-
const
|
|
99
|
-
|
|
98
|
+
const JournalContext = {
|
|
99
|
+
_instance: null,
|
|
100
100
|
_level: 'disable',
|
|
101
101
|
_logger: null,
|
|
102
102
|
_styles: Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES),
|
|
@@ -108,34 +108,28 @@ const JournalEnv = {
|
|
|
108
108
|
*/
|
|
109
109
|
function JournalInit(options) {
|
|
110
110
|
var _a, _b, _c;
|
|
111
|
-
if (
|
|
111
|
+
if (JournalContext._instance) {
|
|
112
112
|
console.warn('[Journal] 已初始化,请勿重复调用');
|
|
113
|
-
return;
|
|
113
|
+
return JournalContext._instance;
|
|
114
114
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
JournalEnv._logger = new JournalLogger({
|
|
115
|
+
JournalContext._level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
|
|
116
|
+
JournalContext._logger = new JournalLogger({
|
|
118
117
|
name: JOURNAL_DEFAULT_LOGGER_NAME,
|
|
119
|
-
level:
|
|
118
|
+
level: JournalContext._level,
|
|
120
119
|
});
|
|
121
120
|
const overrides = options === null || options === void 0 ? void 0 : options.levels;
|
|
122
121
|
if (overrides) {
|
|
123
122
|
for (const level of JOURNAL_LEVELS) {
|
|
124
123
|
const style = overrides[level];
|
|
125
124
|
if (style) {
|
|
126
|
-
|
|
125
|
+
JournalContext._styles[level] = Object.assign(Object.assign({}, JournalContext._styles[level]), style);
|
|
127
126
|
}
|
|
128
127
|
}
|
|
129
128
|
}
|
|
130
129
|
for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
|
|
131
130
|
Create(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
|
|
132
131
|
}
|
|
133
|
-
|
|
134
|
-
* 日志管理器
|
|
135
|
-
*
|
|
136
|
-
* 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
|
|
137
|
-
*/
|
|
138
|
-
return {
|
|
132
|
+
JournalContext._instance = {
|
|
139
133
|
Acquire,
|
|
140
134
|
Create,
|
|
141
135
|
Remove,
|
|
@@ -150,15 +144,16 @@ function JournalInit(options) {
|
|
|
150
144
|
Warn,
|
|
151
145
|
Error,
|
|
152
146
|
};
|
|
147
|
+
return JournalContext._instance;
|
|
153
148
|
}
|
|
154
149
|
/** 获取管理器分级 */
|
|
155
150
|
function GetLevel() {
|
|
156
|
-
return
|
|
151
|
+
return JournalContext._level;
|
|
157
152
|
}
|
|
158
153
|
/** 设置管理器分级 */
|
|
159
154
|
function SetLevel(level) {
|
|
160
|
-
|
|
161
|
-
|
|
155
|
+
JournalContext._level = level;
|
|
156
|
+
JournalContext._logger.Level = level;
|
|
162
157
|
}
|
|
163
158
|
/** 输出 debug 级别记录(默认记录器) */
|
|
164
159
|
function Debug(message, ...args) {
|
|
@@ -183,15 +178,15 @@ function Error(message, ...args) {
|
|
|
183
178
|
* @param args 附加内容,透传给 console
|
|
184
179
|
*/
|
|
185
180
|
function Log(level, message, ...args) {
|
|
186
|
-
|
|
181
|
+
JournalContext._logger.Log(level, message, ...args);
|
|
187
182
|
}
|
|
188
183
|
/** 获取分级样式 */
|
|
189
184
|
function GetLevelStyle(level) {
|
|
190
|
-
return
|
|
185
|
+
return JournalContext._styles[level];
|
|
191
186
|
}
|
|
192
187
|
/** 更新分级样式,仅覆盖传入字段 */
|
|
193
188
|
function SetLevelStyle(level, style) {
|
|
194
|
-
|
|
189
|
+
JournalContext._styles[level] = Object.assign(Object.assign({}, JournalContext._styles[level]), style);
|
|
195
190
|
}
|
|
196
191
|
/**
|
|
197
192
|
* 创建记录器;同名记录器已存在时更新其分级并返回原实例
|
|
@@ -199,31 +194,31 @@ function SetLevelStyle(level, style) {
|
|
|
199
194
|
* @param level 记录器分级,默认为 debug
|
|
200
195
|
*/
|
|
201
196
|
function Create(name, level = 'debug') {
|
|
202
|
-
const existing =
|
|
197
|
+
const existing = JournalContext._loggers.get(name);
|
|
203
198
|
if (existing) {
|
|
204
|
-
existing.
|
|
199
|
+
existing.Level = level;
|
|
205
200
|
return existing;
|
|
206
201
|
}
|
|
207
202
|
const logger = new JournalLogger({ name, level });
|
|
208
|
-
|
|
203
|
+
JournalContext._loggers.set(name, logger);
|
|
209
204
|
return logger;
|
|
210
205
|
}
|
|
211
206
|
/** 获取记录器,不存在时以管理器当前分级创建并返回 */
|
|
212
207
|
function Acquire(name) {
|
|
213
208
|
var _a;
|
|
214
|
-
return (_a =
|
|
209
|
+
return (_a = JournalContext._loggers.get(name)) !== null && _a !== void 0 ? _a : Create(name, JournalContext._level);
|
|
215
210
|
}
|
|
216
211
|
/** 移除记录器,返回是否移除成功 */
|
|
217
212
|
function Remove(name) {
|
|
218
|
-
return
|
|
213
|
+
return JournalContext._loggers.delete(name);
|
|
219
214
|
}
|
|
220
215
|
/** 清空全部记录器,不影响默认记录器 */
|
|
221
216
|
function Clear() {
|
|
222
|
-
|
|
217
|
+
JournalContext._loggers.clear();
|
|
223
218
|
}
|
|
224
219
|
/** 获取全部记录器,不包含默认记录器 */
|
|
225
220
|
function All() {
|
|
226
|
-
return [...
|
|
221
|
+
return [...JournalContext._loggers.values()];
|
|
227
222
|
}
|
|
228
223
|
|
|
229
224
|
export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, JournalInit, JournalLogger };
|
package/dist/package.json
CHANGED