Skip to content

Commit

Permalink
perf(Logger): 日志打印时间格式支持 timeFormat 参数配置
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Mar 8, 2024
1 parent a6bcc72 commit 0180976
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/common/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* @Author: lzw
* @Date: 2022-04-08 10:30:02
* @LastEditors: renxia
* @LastEditTime: 2024-03-07 09:35:51
* @LastEditTime: 2024-03-07 09:43:33
* @Description:
*/
/* eslint no-console: 0 */

import { type GeneralFn } from '../../types';
import { safeStringify } from '../objects';
import { dateFormat } from '../date';

/** 日志级别 */
export enum LogLevel {
Expand All @@ -35,6 +36,8 @@ export interface LoggerOptions {
/** 通过外部注入 color 能力 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
color?: Record<string, any>;
/** 日志时间格式。默认为:yyyy-MM-dd hh:mm:ss.S */
timeFormat?: string;
}

export type LogLevelType = keyof typeof LogLevel;
Expand All @@ -45,6 +48,7 @@ const defaultOptions: LoggerOptions = {
silent: false,
logDir: '',
color: void 0,
timeFormat: 'yyyy-MM-dd hh:mm:ss.S',
};

let headTipColored = false;
Expand Down Expand Up @@ -123,8 +127,7 @@ export class Logger {
if (lvl <= this.level) {
this.times++;
const now = this.getSeverTime(true);
now.setUTCHours(now.getHours());
const curTime = now.toISOString();
const curTime = dateFormat(this.options.timeFormat!, now);
const msg = args.map(s => (typeof s === 'string' ? s : safeStringify(s))).join(' ');

if (this.writeToFile) this.writeToFile(`[${curTime}]${this.tag}[${type}] ${msg}\n`);
Expand Down
4 changes: 2 additions & 2 deletions src/node/LiteStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ export class LiteStorage<T extends object = Record<string, unknown>> {

return this.set(data, 'cover');
}
public getItem(key: keyof T) {
public getItem<K extends keyof T>(key: K): T[K] {
const value = this.get(true)[key];
if (value && !Array.isArray(value) && typeof value === 'object') return assign({}, value) as T[keyof T];
if (value && !Array.isArray(value) && typeof value === 'object') return assign({}, value) as T[K];
return value;
}
/** 移除一项数据。同 del 方法 */
Expand Down

0 comments on commit 0180976

Please sign in to comment.