Skip to content

Commit

Permalink
feat: add more logger type
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Sep 26, 2023
1 parent 3ff1337 commit 8e801df
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 23 deletions.
45 changes: 40 additions & 5 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isDebug, isTest } from 'std-env';

import type { LoggerOptions } from './types';
import type { InputLogItem, InputLogObject, LoggerOptions } from './types';

import { LogLevels } from './level';
import { FormatReporter } from './reporters';
import { BreadcLogger, BreadcLoggerInstance } from './logger';
import { LogLevels, LogType } from './level';
import { BreadcLogger, LogFn } from './logger';

export * from './level';
export * from './types';
Expand All @@ -13,10 +13,10 @@ export * from './reporters';

export const Logger = (
options: Partial<LoggerOptions> & { fancy?: boolean } = {}
): BreadcLoggerInstance => {
): BreadcLogger<Record<LogType, LogFn>> & Record<LogType, LogFn> => {
const level = getDefaultLogLevel();

return new BreadcLogger({
const logger = new BreadcLogger({
reporter: options.reporter || [FormatReporter({ fancy: options.fancy })],
level,
defaults: {},
Expand All @@ -26,6 +26,41 @@ export const Logger = (
plugins: [],
...options
});

const types: LogType[] = [
'fatal',
'error',
'warn',
'info',
'fail',
'ready',
'box',
'start',
'success',
'debug',
'trace',
'verbose'
];

const fns = Object.fromEntries(
types.map(
(type) =>
[
type,
function (
this: BreadcLogger<{}>,
input: InputLogItem,
...args: any[]
) {
const level = LogLevels[type];
const defaults: InputLogObject = { type, level };
this.print(defaults, this.resolveInput(input, args));
}
] as const
)
) as Record<LogType, LogFn>;

return logger.extend(fns);
};

function getDefaultLogLevel() {
Expand Down
37 changes: 19 additions & 18 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,42 @@ import type {

import { LogLevels } from './level';

export class BreadcLogger {
export class BreadcLogger<T extends {}> {
private _overrides: T = {} as T;

readonly options: LoggerOptions;

constructor(options: LoggerOptions) {
this.options = options;
}

get level() {
public get level() {
return this.options.level;
}

set level(level) {
public set level(level) {
this.options.level = level;
}

public withDefaults(defaults: InputLogObject): BreadcLoggerInstance {
return new BreadcLogger({
public extend<U extends {}>(overrides: U): BreadcLogger<T & U> & T & U {
const that = this as unknown as BreadcLogger<T & U> & T;
Object.assign(that._overrides, overrides);
return Object.assign(that, overrides);
}

public withDefaults(defaults: InputLogObject): BreadcLogger<T> & T {
const ins = new BreadcLogger<T>({
...this.options,
defaults: {
...this.options.defaults,
...defaults
}
});
ins._overrides = this._overrides;
return Object.assign(ins, this._overrides);
}

public withTag(tag: string): BreadcLoggerInstance {
public withTag(tag: string): BreadcLogger<T> & T {
return this.withDefaults({
tag: this.options.defaults.tag
? this.options.defaults.tag + ':' + tag
Expand All @@ -45,7 +55,7 @@ export class BreadcLogger {
return obj.level <= this.level;
}

private print(defaults: InputLogObject, input: InputLogObject) {
public print(defaults: InputLogObject, input: InputLogObject) {
const date = new Date();
const obj: LogObject = {
level: LogLevels['log'],
Expand All @@ -62,7 +72,7 @@ export class BreadcLogger {
}
}

private resolveInput(input: InputLogItem, args: any[]) {
public resolveInput(input: InputLogItem, args: any[]) {
if (typeof input === 'string') {
return { message: input, args };
} else if (typeof input === 'number') {
Expand All @@ -87,15 +97,6 @@ export class BreadcLogger {
const defaults: InputLogObject = { type, level };
this.print(defaults, this.resolveInput(input, args));
}

public info(input: InputLogItem, ...args: any[]) {
const type = 'info';
const level = LogLevels[type];
const defaults: InputLogObject = { type, level };
this.print(defaults, this.resolveInput(input, args));
}
}

export type LogFn = (message: string, ...args: any[]) => void;

export type BreadcLoggerInstance = BreadcLogger & {};
export type LogFn = (message: InputLogItem, ...args: any[]) => void;
15 changes: 15 additions & 0 deletions packages/logger/test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,19 @@ describe('Basic Logger', () => {
]
`);
});

it('should print info', () => {
const reporter = MockReporter(BasicReporter({ prefix: ' ' }));
const logger = Logger({
level: LogLevels.verbose,
reporter: [reporter]
});

logger.info('Hello %s', 'world');
expect(reporter.history.map((obj) => obj.output)).toMatchInlineSnapshot(`
[
" [info] Hello world",
]
`);
});
});

0 comments on commit 8e801df

Please sign in to comment.