@nebulaaa/journal 1.0.0 → 1.0.1

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 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(manager, options) {
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[this._manager.level]) {
80
+ if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[Journal.Level]) {
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 = this._manager.getLevelStyle(level);
86
+ const style = Journal.GetLevelStyle(level);
88
87
  const text = `[${this.name}] ${String(message)}`;
89
88
  const method = CONSOLE_METHODS[level];
90
89
  if (style.color) {
@@ -96,74 +95,158 @@ class JournalLogger {
96
95
  }
97
96
  }
98
97
  /**
99
- * 日志管理器
98
+ * 日志管理器(静态类)
100
99
  *
101
- * 通过配置初始化,负责维护分级样式与记录器集合;
102
- * 自身持有独立分级,作为全部记录器的输出门槛:
103
- * 记录器分级低于管理器分级时,该记录器的记录一律忽略。
104
- * 内置一个默认记录器,用于便捷记录,不占用记录器表。
100
+ * 通过 Journal.init(options) 初始化,必须初始化后才能使用:
101
+ * 未初始化时管理器分级为 disable,任何记录都不会输出,
102
+ * 且管理操作会被警告并忽略;使用 Journal.reset() 可恢复未初始化状态。
103
+ * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
105
104
  */
106
105
  class Journal {
107
- constructor(options) {
106
+ /** 静态类,禁止实例化 */
107
+ constructor() { }
108
+ /**
109
+ * 初始化;仅首次调用生效,重复调用将被警告并忽略
110
+ * @param options 管理器配置
111
+ */
112
+ static Init(options) {
108
113
  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);
114
+ if (Journal._Initialized) {
115
+ console.warn('[Journal] 已初始化,请勿重复调用 init()');
116
+ return;
117
+ }
118
+ Journal._Initialized = true;
119
+ Journal._Level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
120
+ Journal._Logger = new JournalLogger({ name: JOURNAL_DEFAULT_LOGGER_NAME, level: Journal._Level });
112
121
  const overrides = options === null || options === void 0 ? void 0 : options.levels;
113
122
  if (overrides) {
114
123
  for (const level of JOURNAL_LEVELS) {
115
124
  const style = overrides[level];
116
125
  if (style) {
117
- this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
126
+ Journal._Styles[level] = Object.assign(Object.assign({}, Journal._Styles[level]), style);
118
127
  }
119
128
  }
120
129
  }
121
- this._loggers = new Map();
122
130
  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');
131
+ Journal.CreateLogger(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
132
+ }
133
+ }
134
+ /** 重置为未初始化状态:清空记录器、恢复默认样式与分级 */
135
+ static Reset() {
136
+ Journal._Initialized = false;
137
+ Journal._Level = 'disable';
138
+ Journal._Logger = null;
139
+ Journal._Styles = Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES);
140
+ Journal._Loggers.clear();
141
+ }
142
+ /** 管理器分级 */
143
+ static get Level() {
144
+ return Journal._Level;
145
+ }
146
+ static set Level(level) {
147
+ if (!Journal.EnsureReady()) {
148
+ return;
149
+ }
150
+ Journal._Level = level;
151
+ Journal._Logger.level = level;
152
+ }
153
+ /** 输出 debug 级别记录(默认记录器) */
154
+ static Debug(message, ...args) {
155
+ Journal.Log('debug', message, ...args);
156
+ }
157
+ /** 输出 info 级别记录(默认记录器) */
158
+ static Info(message, ...args) {
159
+ Journal.Log('info', message, ...args);
160
+ }
161
+ /** 输出 warn 级别记录(默认记录器) */
162
+ static Warn(message, ...args) {
163
+ Journal.Log('warn', message, ...args);
164
+ }
165
+ /** 输出 error 级别记录(默认记录器) */
166
+ static Error(message, ...args) {
167
+ Journal.Log('error', message, ...args);
168
+ }
169
+ /**
170
+ * 输出一条记录(默认记录器)
171
+ * @param level 记录分级
172
+ * @param message 记录内容
173
+ * @param args 附加内容,透传给 console
174
+ */
175
+ static Log(level, message, ...args) {
176
+ var _a;
177
+ if (!Journal.EnsureReady()) {
178
+ return;
124
179
  }
180
+ (_a = Journal._Logger) === null || _a === void 0 ? void 0 : _a.log(level, message, ...args);
125
181
  }
126
182
  /** 获取分级样式 */
127
- getLevelStyle(level) {
128
- return this._styles[level];
183
+ static GetLevelStyle(level) {
184
+ return Journal._Styles[level];
129
185
  }
130
186
  /** 更新分级样式,仅覆盖传入字段 */
131
- setLevelStyle(level, style) {
132
- this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
187
+ static SetLevelStyle(level, style) {
188
+ if (!Journal.EnsureReady()) {
189
+ return;
190
+ }
191
+ Journal._Styles[level] = Object.assign(Object.assign({}, Journal._Styles[level]), style);
133
192
  }
134
193
  /**
135
194
  * 创建记录器;同名记录器已存在时更新其分级并返回原实例
136
195
  * @param name 记录器名称
137
196
  * @param level 记录器分级,默认为 debug
138
197
  */
139
- createLogger(name, level = 'debug') {
140
- const existing = this._loggers.get(name);
198
+ static CreateLogger(name, level = 'debug') {
199
+ if (!Journal.EnsureReady()) {
200
+ return null;
201
+ }
202
+ const existing = Journal._Loggers.get(name);
141
203
  if (existing) {
142
204
  existing.level = level;
143
205
  return existing;
144
206
  }
145
- const logger = new JournalLogger(this, { name, level });
146
- this._loggers.set(name, logger);
207
+ const logger = new JournalLogger({ name, level });
208
+ Journal._Loggers.set(name, logger);
147
209
  return logger;
148
210
  }
149
211
  /** 获取记录器,不存在时以管理器当前分级创建并返回 */
150
- getLogger(name) {
212
+ static GetLogger(name) {
151
213
  var _a;
152
- return (_a = this._loggers.get(name)) !== null && _a !== void 0 ? _a : this.createLogger(name, this.level);
214
+ if (!Journal.EnsureReady()) {
215
+ return null;
216
+ }
217
+ return (_a = Journal._Loggers.get(name)) !== null && _a !== void 0 ? _a : Journal.CreateLogger(name, Journal._Level);
153
218
  }
154
219
  /** 移除记录器,返回是否移除成功 */
155
- removeLogger(name) {
156
- return this._loggers.delete(name);
220
+ static RemoveLogger(name) {
221
+ return Journal._Loggers.delete(name);
157
222
  }
158
223
  /** 清空全部记录器,不影响默认记录器 */
159
- clearLoggers() {
160
- this._loggers.clear();
224
+ static ClearLoggers() {
225
+ Journal._Loggers.clear();
161
226
  }
162
227
  /** 获取全部记录器,不含默认记录器 */
163
- getLoggers() {
164
- return [...this._loggers.values()];
228
+ static GetLoggers() {
229
+ return [...Journal._Loggers.values()];
230
+ }
231
+ /** 未初始化时警告并返回 false */
232
+ static EnsureReady() {
233
+ if (!Journal._Initialized) {
234
+ console.warn('[Journal] 尚未初始化,请先调用 Journal.Init()');
235
+ return false;
236
+ }
237
+ return true;
165
238
  }
166
239
  }
240
+ /** 是否已初始化 */
241
+ Journal._Initialized = false;
242
+ /** 管理器分级;未初始化时为 disable,确保无任何输出 */
243
+ Journal._Level = 'disable';
244
+ /** 默认记录器;分级随管理器动态调整 */
245
+ Journal._Logger = null;
246
+ /** 分级样式表 */
247
+ Journal._Styles = Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES);
248
+ /** 记录器表 */
249
+ Journal._Loggers = new Map();
167
250
 
168
251
  exports.JOURNAL_DEFAULT_LEVEL_STYLES = JOURNAL_DEFAULT_LEVEL_STYLES;
169
252
  exports.JOURNAL_DEFAULT_LOGGER_NAME = JOURNAL_DEFAULT_LOGGER_NAME;
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,41 +84,71 @@ declare class JournalLogger {
86
84
  log(level: JournalLevel, message: unknown, ...args: unknown[]): void;
87
85
  }
88
86
  /**
89
- * 日志管理器
87
+ * 日志管理器(静态类)
90
88
  *
91
- * 通过配置初始化,负责维护分级样式与记录器集合;
92
- * 自身持有独立分级,作为全部记录器的输出门槛:
93
- * 记录器分级低于管理器分级时,该记录器的记录一律忽略。
94
- * 内置一个默认记录器,用于便捷记录,不占用记录器表。
89
+ * 通过 Journal.init(options) 初始化,必须初始化后才能使用:
90
+ * 未初始化时管理器分级为 disable,任何记录都不会输出,
91
+ * 且管理操作会被警告并忽略;使用 Journal.reset() 可恢复未初始化状态。
92
+ * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
95
93
  */
96
94
  declare class Journal {
97
- /** 管理器分级 */
98
- level: JournalLevel;
99
- /** 默认记录器:初始分级与管理器一致,同样受管理器分级约束 */
100
- readonly logger: JournalLogger;
95
+ /** 是否已初始化 */
96
+ private static _Initialized;
97
+ /** 管理器分级;未初始化时为 disable,确保无任何输出 */
98
+ private static _Level;
99
+ /** 默认记录器;分级随管理器动态调整 */
100
+ private static _Logger;
101
101
  /** 分级样式表 */
102
- private _styles;
102
+ private static _Styles;
103
103
  /** 记录器表 */
104
- private _loggers;
105
- constructor(options?: JournalManagerOptions);
104
+ private static _Loggers;
105
+ /** 静态类,禁止实例化 */
106
+ private constructor();
107
+ /**
108
+ * 初始化;仅首次调用生效,重复调用将被警告并忽略
109
+ * @param options 管理器配置
110
+ */
111
+ static Init(options?: JournalManagerOptions): void;
112
+ /** 重置为未初始化状态:清空记录器、恢复默认样式与分级 */
113
+ static Reset(): void;
114
+ /** 管理器分级 */
115
+ static get Level(): JournalLevel;
116
+ static set Level(level: JournalLevel);
117
+ /** 输出 debug 级别记录(默认记录器) */
118
+ static Debug(message: unknown, ...args: unknown[]): void;
119
+ /** 输出 info 级别记录(默认记录器) */
120
+ static Info(message: unknown, ...args: unknown[]): void;
121
+ /** 输出 warn 级别记录(默认记录器) */
122
+ static Warn(message: unknown, ...args: unknown[]): void;
123
+ /** 输出 error 级别记录(默认记录器) */
124
+ static Error(message: unknown, ...args: unknown[]): void;
125
+ /**
126
+ * 输出一条记录(默认记录器)
127
+ * @param level 记录分级
128
+ * @param message 记录内容
129
+ * @param args 附加内容,透传给 console
130
+ */
131
+ private static Log;
106
132
  /** 获取分级样式 */
107
- getLevelStyle(level: JournalLevel): Readonly<JournalLevelStyle>;
133
+ static GetLevelStyle(level: JournalLevel): Readonly<JournalLevelStyle>;
108
134
  /** 更新分级样式,仅覆盖传入字段 */
109
- setLevelStyle(level: JournalLevel, style: Partial<JournalLevelStyle>): void;
135
+ static SetLevelStyle(level: JournalLevel, style: Partial<JournalLevelStyle>): void;
110
136
  /**
111
137
  * 创建记录器;同名记录器已存在时更新其分级并返回原实例
112
138
  * @param name 记录器名称
113
139
  * @param level 记录器分级,默认为 debug
114
140
  */
115
- createLogger(name: string, level?: JournalLevel): JournalLogger;
141
+ static CreateLogger(name: string, level?: JournalLevel): JournalLogger | null;
116
142
  /** 获取记录器,不存在时以管理器当前分级创建并返回 */
117
- getLogger(name: string): JournalLogger;
143
+ static GetLogger(name: string): JournalLogger | null;
118
144
  /** 移除记录器,返回是否移除成功 */
119
- removeLogger(name: string): boolean;
145
+ static RemoveLogger(name: string): boolean;
120
146
  /** 清空全部记录器,不影响默认记录器 */
121
- clearLoggers(): void;
147
+ static ClearLoggers(): void;
122
148
  /** 获取全部记录器,不含默认记录器 */
123
- getLoggers(): JournalLogger[];
149
+ static GetLoggers(): JournalLogger[];
150
+ /** 未初始化时警告并返回 false */
151
+ private static EnsureReady;
124
152
  }
125
153
 
126
154
  export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, Journal, JournalLogger };
@@ -1 +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;
1
+ "use strict";const e=["debug","info","warn","error","disable"],r={debug:0,info:1,warn:2,error:3,disable:4},l={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",t={debug:"log",info:"info",warn:"warn",error:"error"};class n{constructor(e){var r;this.name=e.name,this.level=null!==(r=e.level)&&void 0!==r?r:"debug"}debug(e,...r){this.log("debug",e,...r)}info(e,...r){this.log("info",e,...r)}warn(e,...r){this.log("warn",e,...r)}error(e,...r){this.log("error",e,...r)}log(e,l,...o){if("disable"===e)return;if(r[this.level]<r[s.Level])return;if(r[e]<r[this.level])return;const n=s.GetLevelStyle(e),i=`[${this.name}] ${String(l)}`,a=t[e];n.color?console[a](`%c${n.label}%c ${i}`,`color:${n.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...o):console[a](`${n.label} ${i}`,...o)}}class s{constructor(){}static Init(r){var l,t,i;if(s._Initialized)return void console.warn("[Journal] 已初始化,请勿重复调用 init()");s._Initialized=!0,s._Level=null!==(l=null==r?void 0:r.level)&&void 0!==l?l:"debug",s._Logger=new n({name:o,level:s._Level});const a=null==r?void 0:r.levels;if(a)for(const r of e){const e=a[r];e&&(s._Styles[r]=Object.assign(Object.assign({},s._Styles[r]),e))}for(const e of null!==(t=null==r?void 0:r.loggers)&&void 0!==t?t:[])s.CreateLogger(e.name,null!==(i=e.level)&&void 0!==i?i:"debug")}static Reset(){s._Initialized=!1,s._Level="disable",s._Logger=null,s._Styles=Object.assign({},l),s._Loggers.clear()}static get Level(){return s._Level}static set Level(e){s.EnsureReady()&&(s._Level=e,s._Logger.level=e)}static Debug(e,...r){s.Log("debug",e,...r)}static Info(e,...r){s.Log("info",e,...r)}static Warn(e,...r){s.Log("warn",e,...r)}static Error(e,...r){s.Log("error",e,...r)}static Log(e,r,...l){var o;s.EnsureReady()&&(null===(o=s._Logger)||void 0===o||o.log(e,r,...l))}static GetLevelStyle(e){return s._Styles[e]}static SetLevelStyle(e,r){s.EnsureReady()&&(s._Styles[e]=Object.assign(Object.assign({},s._Styles[e]),r))}static CreateLogger(e,r="debug"){if(!s.EnsureReady())return null;const l=s._Loggers.get(e);if(l)return l.level=r,l;const o=new n({name:e,level:r});return s._Loggers.set(e,o),o}static GetLogger(e){var r;return s.EnsureReady()?null!==(r=s._Loggers.get(e))&&void 0!==r?r:s.CreateLogger(e,s._Level):null}static RemoveLogger(e){return s._Loggers.delete(e)}static ClearLoggers(){s._Loggers.clear()}static GetLoggers(){return[...s._Loggers.values()]}static EnsureReady(){return!!s._Initialized||(console.warn("[Journal] 尚未初始化,请先调用 Journal.Init()"),!1)}}s._Initialized=!1,s._Level="disable",s._Logger=null,s._Styles=Object.assign({},l),s._Loggers=new Map,exports.JOURNAL_DEFAULT_LEVEL_STYLES=l,exports.JOURNAL_DEFAULT_LOGGER_NAME=o,exports.JOURNAL_LEVELS=e,exports.JOURNAL_LEVEL_WEIGHTS=r,exports.Journal=s,exports.JournalLogger=n;
@@ -1 +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};
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:""}},t="journal",o={debug:"log",info:"info",warn:"warn",error:"error"};class n{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,r,...t){if("disable"===e)return;if(l[this.level]<l[s.Level])return;if(l[e]<l[this.level])return;const n=s.GetLevelStyle(e),i=`[${this.name}] ${String(r)}`,a=o[e];n.color?console[a](`%c${n.label}%c ${i}`,`color:${n.color};font-weight:bold;`,"color:inherit;font-weight:inherit;",...t):console[a](`${n.label} ${i}`,...t)}}class s{constructor(){}static Init(l){var r,o,i;if(s._Initialized)return void console.warn("[Journal] 已初始化,请勿重复调用 init()");s._Initialized=!0,s._Level=null!==(r=null==l?void 0:l.level)&&void 0!==r?r:"debug",s._Logger=new n({name:t,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!==(o=null==l?void 0:l.loggers)&&void 0!==o?o:[])s.CreateLogger(e.name,null!==(i=e.level)&&void 0!==i?i:"debug")}static Reset(){s._Initialized=!1,s._Level="disable",s._Logger=null,s._Styles=Object.assign({},r),s._Loggers.clear()}static get Level(){return s._Level}static set Level(e){s.EnsureReady()&&(s._Level=e,s._Logger.level=e)}static Debug(e,...l){s.Log("debug",e,...l)}static Info(e,...l){s.Log("info",e,...l)}static Warn(e,...l){s.Log("warn",e,...l)}static Error(e,...l){s.Log("error",e,...l)}static Log(e,l,...r){var t;s.EnsureReady()&&(null===(t=s._Logger)||void 0===t||t.log(e,l,...r))}static GetLevelStyle(e){return s._Styles[e]}static SetLevelStyle(e,l){s.EnsureReady()&&(s._Styles[e]=Object.assign(Object.assign({},s._Styles[e]),l))}static CreateLogger(e,l="debug"){if(!s.EnsureReady())return null;const r=s._Loggers.get(e);if(r)return r.level=l,r;const t=new n({name:e,level:l});return s._Loggers.set(e,t),t}static GetLogger(e){var l;return s.EnsureReady()?null!==(l=s._Loggers.get(e))&&void 0!==l?l:s.CreateLogger(e,s._Level):null}static RemoveLogger(e){return s._Loggers.delete(e)}static ClearLoggers(){s._Loggers.clear()}static GetLoggers(){return[...s._Loggers.values()]}static EnsureReady(){return!!s._Initialized||(console.warn("[Journal] 尚未初始化,请先调用 Journal.Init()"),!1)}}s._Initialized=!1,s._Level="disable",s._Logger=null,s._Styles=Object.assign({},r),s._Loggers=new Map;export{r as JOURNAL_DEFAULT_LEVEL_STYLES,t as JOURNAL_DEFAULT_LOGGER_NAME,e as JOURNAL_LEVELS,l as JOURNAL_LEVEL_WEIGHTS,s as Journal,n 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(manager, options) {
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[this._manager.level]) {
78
+ if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[Journal.Level]) {
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 = this._manager.getLevelStyle(level);
84
+ const style = Journal.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,157 @@ class JournalLogger {
94
93
  }
95
94
  }
96
95
  /**
97
- * 日志管理器
96
+ * 日志管理器(静态类)
98
97
  *
99
- * 通过配置初始化,负责维护分级样式与记录器集合;
100
- * 自身持有独立分级,作为全部记录器的输出门槛:
101
- * 记录器分级低于管理器分级时,该记录器的记录一律忽略。
102
- * 内置一个默认记录器,用于便捷记录,不占用记录器表。
98
+ * 通过 Journal.init(options) 初始化,必须初始化后才能使用:
99
+ * 未初始化时管理器分级为 disable,任何记录都不会输出,
100
+ * 且管理操作会被警告并忽略;使用 Journal.reset() 可恢复未初始化状态。
101
+ * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
103
102
  */
104
103
  class Journal {
105
- constructor(options) {
104
+ /** 静态类,禁止实例化 */
105
+ constructor() { }
106
+ /**
107
+ * 初始化;仅首次调用生效,重复调用将被警告并忽略
108
+ * @param options 管理器配置
109
+ */
110
+ static Init(options) {
106
111
  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);
112
+ if (Journal._Initialized) {
113
+ console.warn('[Journal] 已初始化,请勿重复调用 init()');
114
+ return;
115
+ }
116
+ Journal._Initialized = true;
117
+ Journal._Level = (_a = options === null || options === void 0 ? void 0 : options.level) !== null && _a !== void 0 ? _a : 'debug';
118
+ Journal._Logger = new JournalLogger({ name: JOURNAL_DEFAULT_LOGGER_NAME, level: Journal._Level });
110
119
  const overrides = options === null || options === void 0 ? void 0 : options.levels;
111
120
  if (overrides) {
112
121
  for (const level of JOURNAL_LEVELS) {
113
122
  const style = overrides[level];
114
123
  if (style) {
115
- this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
124
+ Journal._Styles[level] = Object.assign(Object.assign({}, Journal._Styles[level]), style);
116
125
  }
117
126
  }
118
127
  }
119
- this._loggers = new Map();
120
128
  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');
129
+ Journal.CreateLogger(config.name, (_c = config.level) !== null && _c !== void 0 ? _c : 'debug');
130
+ }
131
+ }
132
+ /** 重置为未初始化状态:清空记录器、恢复默认样式与分级 */
133
+ static Reset() {
134
+ Journal._Initialized = false;
135
+ Journal._Level = 'disable';
136
+ Journal._Logger = null;
137
+ Journal._Styles = Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES);
138
+ Journal._Loggers.clear();
139
+ }
140
+ /** 管理器分级 */
141
+ static get Level() {
142
+ return Journal._Level;
143
+ }
144
+ static set Level(level) {
145
+ if (!Journal.EnsureReady()) {
146
+ return;
147
+ }
148
+ Journal._Level = level;
149
+ Journal._Logger.level = level;
150
+ }
151
+ /** 输出 debug 级别记录(默认记录器) */
152
+ static Debug(message, ...args) {
153
+ Journal.Log('debug', message, ...args);
154
+ }
155
+ /** 输出 info 级别记录(默认记录器) */
156
+ static Info(message, ...args) {
157
+ Journal.Log('info', message, ...args);
158
+ }
159
+ /** 输出 warn 级别记录(默认记录器) */
160
+ static Warn(message, ...args) {
161
+ Journal.Log('warn', message, ...args);
162
+ }
163
+ /** 输出 error 级别记录(默认记录器) */
164
+ static Error(message, ...args) {
165
+ Journal.Log('error', message, ...args);
166
+ }
167
+ /**
168
+ * 输出一条记录(默认记录器)
169
+ * @param level 记录分级
170
+ * @param message 记录内容
171
+ * @param args 附加内容,透传给 console
172
+ */
173
+ static Log(level, message, ...args) {
174
+ var _a;
175
+ if (!Journal.EnsureReady()) {
176
+ return;
122
177
  }
178
+ (_a = Journal._Logger) === null || _a === void 0 ? void 0 : _a.log(level, message, ...args);
123
179
  }
124
180
  /** 获取分级样式 */
125
- getLevelStyle(level) {
126
- return this._styles[level];
181
+ static GetLevelStyle(level) {
182
+ return Journal._Styles[level];
127
183
  }
128
184
  /** 更新分级样式,仅覆盖传入字段 */
129
- setLevelStyle(level, style) {
130
- this._styles[level] = Object.assign(Object.assign({}, this._styles[level]), style);
185
+ static SetLevelStyle(level, style) {
186
+ if (!Journal.EnsureReady()) {
187
+ return;
188
+ }
189
+ Journal._Styles[level] = Object.assign(Object.assign({}, Journal._Styles[level]), style);
131
190
  }
132
191
  /**
133
192
  * 创建记录器;同名记录器已存在时更新其分级并返回原实例
134
193
  * @param name 记录器名称
135
194
  * @param level 记录器分级,默认为 debug
136
195
  */
137
- createLogger(name, level = 'debug') {
138
- const existing = this._loggers.get(name);
196
+ static CreateLogger(name, level = 'debug') {
197
+ if (!Journal.EnsureReady()) {
198
+ return null;
199
+ }
200
+ const existing = Journal._Loggers.get(name);
139
201
  if (existing) {
140
202
  existing.level = level;
141
203
  return existing;
142
204
  }
143
- const logger = new JournalLogger(this, { name, level });
144
- this._loggers.set(name, logger);
205
+ const logger = new JournalLogger({ name, level });
206
+ Journal._Loggers.set(name, logger);
145
207
  return logger;
146
208
  }
147
209
  /** 获取记录器,不存在时以管理器当前分级创建并返回 */
148
- getLogger(name) {
210
+ static GetLogger(name) {
149
211
  var _a;
150
- return (_a = this._loggers.get(name)) !== null && _a !== void 0 ? _a : this.createLogger(name, this.level);
212
+ if (!Journal.EnsureReady()) {
213
+ return null;
214
+ }
215
+ return (_a = Journal._Loggers.get(name)) !== null && _a !== void 0 ? _a : Journal.CreateLogger(name, Journal._Level);
151
216
  }
152
217
  /** 移除记录器,返回是否移除成功 */
153
- removeLogger(name) {
154
- return this._loggers.delete(name);
218
+ static RemoveLogger(name) {
219
+ return Journal._Loggers.delete(name);
155
220
  }
156
221
  /** 清空全部记录器,不影响默认记录器 */
157
- clearLoggers() {
158
- this._loggers.clear();
222
+ static ClearLoggers() {
223
+ Journal._Loggers.clear();
159
224
  }
160
225
  /** 获取全部记录器,不含默认记录器 */
161
- getLoggers() {
162
- return [...this._loggers.values()];
226
+ static GetLoggers() {
227
+ return [...Journal._Loggers.values()];
228
+ }
229
+ /** 未初始化时警告并返回 false */
230
+ static EnsureReady() {
231
+ if (!Journal._Initialized) {
232
+ console.warn('[Journal] 尚未初始化,请先调用 Journal.Init()');
233
+ return false;
234
+ }
235
+ return true;
163
236
  }
164
237
  }
238
+ /** 是否已初始化 */
239
+ Journal._Initialized = false;
240
+ /** 管理器分级;未初始化时为 disable,确保无任何输出 */
241
+ Journal._Level = 'disable';
242
+ /** 默认记录器;分级随管理器动态调整 */
243
+ Journal._Logger = null;
244
+ /** 分级样式表 */
245
+ Journal._Styles = Object.assign({}, JOURNAL_DEFAULT_LEVEL_STYLES);
246
+ /** 记录器表 */
247
+ Journal._Loggers = new Map();
165
248
 
166
249
  export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, Journal, JournalLogger };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulaaa/journal",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "日志记录扩展包",
5
5
  "author": "doooreyn",
6
6
  "license": "MIT",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulaaa/journal",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "日志记录扩展包",
5
5
  "main": "./dist/journal.cjs",
6
6
  "module": "./dist/journal.mjs",