@nebulaaa/journal 1.0.1 → 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 CHANGED
@@ -77,13 +77,13 @@ class JournalLogger {
77
77
  if (level === 'disable') {
78
78
  return;
79
79
  }
80
- if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[Journal.Level]) {
80
+ if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[GetLevel()]) {
81
81
  return;
82
82
  }
83
83
  if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.level]) {
84
84
  return;
85
85
  }
86
- const style = Journal.GetLevelStyle(level);
86
+ const style = GetLevelStyle(level);
87
87
  const text = `[${this.name}] ${String(message)}`;
88
88
  const method = CONSOLE_METHODS[level];
89
89
  if (style.color) {
@@ -95,162 +95,142 @@ class JournalLogger {
95
95
  }
96
96
  }
97
97
  /**
98
- * 日志管理器(静态类)
99
- *
100
- * 通过 Journal.init(options) 初始化,必须初始化后才能使用:
101
- * 未初始化时管理器分级为 disable,任何记录都不会输出,
102
- * 且管理操作会被警告并忽略;使用 Journal.reset() 可恢复未初始化状态。
103
- * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
98
+ * 日志记录器环境
104
99
  */
105
- class Journal {
106
- /** 静态类,禁止实例化 */
107
- constructor() { }
108
- /**
109
- * 初始化;仅首次调用生效,重复调用将被警告并忽略
110
- * @param options 管理器配置
111
- */
112
- static Init(options) {
113
- var _a, _b, _c;
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 });
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
- Journal._Styles[level] = Object.assign(Object.assign({}, Journal._Styles[level]), style);
127
- }
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);
128
129
  }
129
130
  }
130
- for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
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;
179
- }
180
- (_a = Journal._Logger) === null || _a === void 0 ? void 0 : _a.log(level, message, ...args);
181
131
  }
182
- /** 获取分级样式 */
183
- static GetLevelStyle(level) {
184
- return Journal._Styles[level];
185
- }
186
- /** 更新分级样式,仅覆盖传入字段 */
187
- static SetLevelStyle(level, style) {
188
- if (!Journal.EnsureReady()) {
189
- return;
190
- }
191
- Journal._Styles[level] = Object.assign(Object.assign({}, Journal._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');
192
134
  }
193
135
  /**
194
- * 创建记录器;同名记录器已存在时更新其分级并返回原实例
195
- * @param name 记录器名称
196
- * @param level 记录器分级,默认为 debug
136
+ * 日志管理器
137
+ *
138
+ * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
197
139
  */
198
- static CreateLogger(name, level = 'debug') {
199
- if (!Journal.EnsureReady()) {
200
- return null;
201
- }
202
- const existing = Journal._Loggers.get(name);
203
- if (existing) {
204
- existing.level = level;
205
- return existing;
206
- }
207
- const logger = new JournalLogger({ name, level });
208
- Journal._Loggers.set(name, logger);
209
- return logger;
210
- }
211
- /** 获取记录器,不存在时以管理器当前分级创建并返回 */
212
- static GetLogger(name) {
213
- var _a;
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);
218
- }
219
- /** 移除记录器,返回是否移除成功 */
220
- static RemoveLogger(name) {
221
- return Journal._Loggers.delete(name);
222
- }
223
- /** 清空全部记录器,不影响默认记录器 */
224
- static ClearLoggers() {
225
- Journal._Loggers.clear();
226
- }
227
- /** 获取全部记录器,不含默认记录器 */
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;
238
- }
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;
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()];
239
229
  }
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();
250
230
 
251
231
  exports.JOURNAL_DEFAULT_LEVEL_STYLES = JOURNAL_DEFAULT_LEVEL_STYLES;
252
232
  exports.JOURNAL_DEFAULT_LOGGER_NAME = JOURNAL_DEFAULT_LOGGER_NAME;
253
233
  exports.JOURNAL_LEVELS = JOURNAL_LEVELS;
254
234
  exports.JOURNAL_LEVEL_WEIGHTS = JOURNAL_LEVEL_WEIGHTS;
255
- exports.Journal = Journal;
235
+ exports.JournalInit = JournalInit;
256
236
  exports.JournalLogger = JournalLogger;
package/dist/journal.d.ts CHANGED
@@ -84,72 +84,54 @@ declare class JournalLogger {
84
84
  log(level: JournalLevel, message: unknown, ...args: unknown[]): void;
85
85
  }
86
86
  /**
87
- * 日志管理器(静态类)
88
- *
89
- * 通过 Journal.init(options) 初始化,必须初始化后才能使用:
90
- * 未初始化时管理器分级为 disable,任何记录都不会输出,
91
- * 且管理操作会被警告并忽略;使用 Journal.reset() 可恢复未初始化状态。
92
- * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
87
+ * 初始化;仅首次调用生效,重复调用将被警告并忽略
88
+ * @param options 管理器配置
93
89
  */
94
- declare class Journal {
95
- /** 是否已初始化 */
96
- private static _Initialized;
97
- /** 管理器分级;未初始化时为 disable,确保无任何输出 */
98
- private static _Level;
99
- /** 默认记录器;分级随管理器动态调整 */
100
- private static _Logger;
101
- /** 分级样式表 */
102
- private static _Styles;
103
- /** 记录器表 */
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;
132
- /** 获取分级样式 */
133
- static GetLevelStyle(level: JournalLevel): Readonly<JournalLevelStyle>;
134
- /** 更新分级样式,仅覆盖传入字段 */
135
- static SetLevelStyle(level: JournalLevel, style: Partial<JournalLevelStyle>): void;
136
- /**
137
- * 创建记录器;同名记录器已存在时更新其分级并返回原实例
138
- * @param name 记录器名称
139
- * @param level 记录器分级,默认为 debug
140
- */
141
- static CreateLogger(name: string, level?: JournalLevel): JournalLogger | null;
142
- /** 获取记录器,不存在时以管理器当前分级创建并返回 */
143
- static GetLogger(name: string): JournalLogger | null;
144
- /** 移除记录器,返回是否移除成功 */
145
- static RemoveLogger(name: string): boolean;
146
- /** 清空全部记录器,不影响默认记录器 */
147
- static ClearLoggers(): void;
148
- /** 获取全部记录器,不含默认记录器 */
149
- static GetLoggers(): JournalLogger[];
150
- /** 未初始化时警告并返回 false */
151
- private static EnsureReady;
152
- }
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[];
153
135
 
154
- export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, Journal, JournalLogger };
136
+ export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, JournalInit, JournalLogger };
155
137
  export type { JournalLevel, JournalLevelStyle, JournalLoggerOptions, JournalManagerOptions, JournalPrintLevel };
@@ -1 +1 @@
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
+ "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;
@@ -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:""}},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};
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
@@ -75,13 +75,13 @@ class JournalLogger {
75
75
  if (level === 'disable') {
76
76
  return;
77
77
  }
78
- if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[Journal.Level]) {
78
+ if (JOURNAL_LEVEL_WEIGHTS[this.level] < JOURNAL_LEVEL_WEIGHTS[GetLevel()]) {
79
79
  return;
80
80
  }
81
81
  if (JOURNAL_LEVEL_WEIGHTS[level] < JOURNAL_LEVEL_WEIGHTS[this.level]) {
82
82
  return;
83
83
  }
84
- const style = Journal.GetLevelStyle(level);
84
+ const style = GetLevelStyle(level);
85
85
  const text = `[${this.name}] ${String(message)}`;
86
86
  const method = CONSOLE_METHODS[level];
87
87
  if (style.color) {
@@ -93,157 +93,137 @@ class JournalLogger {
93
93
  }
94
94
  }
95
95
  /**
96
- * 日志管理器(静态类)
97
- *
98
- * 通过 Journal.init(options) 初始化,必须初始化后才能使用:
99
- * 未初始化时管理器分级为 disable,任何记录都不会输出,
100
- * 且管理操作会被警告并忽略;使用 Journal.reset() 可恢复未初始化状态。
101
- * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
96
+ * 日志记录器环境
102
97
  */
103
- class Journal {
104
- /** 静态类,禁止实例化 */
105
- constructor() { }
106
- /**
107
- * 初始化;仅首次调用生效,重复调用将被警告并忽略
108
- * @param options 管理器配置
109
- */
110
- static Init(options) {
111
- var _a, _b, _c;
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 });
119
- const overrides = options === null || options === void 0 ? void 0 : options.levels;
120
- if (overrides) {
121
- for (const level of JOURNAL_LEVELS) {
122
- const style = overrides[level];
123
- if (style) {
124
- Journal._Styles[level] = Object.assign(Object.assign({}, Journal._Styles[level]), style);
125
- }
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);
126
127
  }
127
128
  }
128
- for (const config of (_b = options === null || options === void 0 ? void 0 : options.loggers) !== null && _b !== void 0 ? _b : []) {
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;
177
- }
178
- (_a = Journal._Logger) === null || _a === void 0 ? void 0 : _a.log(level, message, ...args);
179
129
  }
180
- /** 获取分级样式 */
181
- static GetLevelStyle(level) {
182
- return Journal._Styles[level];
183
- }
184
- /** 更新分级样式,仅覆盖传入字段 */
185
- static SetLevelStyle(level, style) {
186
- if (!Journal.EnsureReady()) {
187
- return;
188
- }
189
- Journal._Styles[level] = Object.assign(Object.assign({}, Journal._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');
190
132
  }
191
133
  /**
192
- * 创建记录器;同名记录器已存在时更新其分级并返回原实例
193
- * @param name 记录器名称
194
- * @param level 记录器分级,默认为 debug
134
+ * 日志管理器
135
+ *
136
+ * 内置一个默认记录器,用于便捷记录,不占用记录器表,分级随管理器动态调整。
195
137
  */
196
- static CreateLogger(name, level = 'debug') {
197
- if (!Journal.EnsureReady()) {
198
- return null;
199
- }
200
- const existing = Journal._Loggers.get(name);
201
- if (existing) {
202
- existing.level = level;
203
- return existing;
204
- }
205
- const logger = new JournalLogger({ name, level });
206
- Journal._Loggers.set(name, logger);
207
- return logger;
208
- }
209
- /** 获取记录器,不存在时以管理器当前分级创建并返回 */
210
- static GetLogger(name) {
211
- var _a;
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);
216
- }
217
- /** 移除记录器,返回是否移除成功 */
218
- static RemoveLogger(name) {
219
- return Journal._Loggers.delete(name);
220
- }
221
- /** 清空全部记录器,不影响默认记录器 */
222
- static ClearLoggers() {
223
- Journal._Loggers.clear();
224
- }
225
- /** 获取全部记录器,不含默认记录器 */
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;
236
- }
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;
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()];
237
227
  }
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();
248
228
 
249
- export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, Journal, JournalLogger };
229
+ export { JOURNAL_DEFAULT_LEVEL_STYLES, JOURNAL_DEFAULT_LOGGER_NAME, JOURNAL_LEVELS, JOURNAL_LEVEL_WEIGHTS, JournalInit, JournalLogger };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulaaa/journal",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
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.1",
3
+ "version": "1.1.0",
4
4
  "description": "日志记录扩展包",
5
5
  "main": "./dist/journal.cjs",
6
6
  "module": "./dist/journal.mjs",