Skip to content

Commit

Permalink
feat(logging): add initial implementation of Formatter (#3272)
Browse files Browse the repository at this point in the history
Refs #3197
  • Loading branch information
char0n committed Oct 16, 2023
1 parent 2c30b13 commit eae432e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/apidom-logging/src/Filterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getLogRecordClass } from './LogRecord';

type Filter = FilterFunction | FilterInstance;

class Filterer {
abstract class Filterer {
protected readonly filters: Filter[] = [];

public addFilter(filter: Filter) {
Expand Down
42 changes: 42 additions & 0 deletions packages/apidom-logging/src/Formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ApiDOMStructuredError } from '@swagger-api/apidom-error';

import STYLES, { Style } from './styles';

export interface FormatterOptions {
readonly fmt?: Style['fmt'];
readonly datefmt?: string;
readonly style?: keyof typeof STYLES;
readonly validate?: boolean;
readonly defaults?: Record<string, unknown>;
}

class Formatter {
protected readonly style: Style;

protected readonly datefmt?: string;

constructor(options: FormatterOptions = {}) {
const style = options.style ?? '$';

if (style === '$' && typeof options.fmt === 'string') {
this.style = new STYLES.$[0](options.fmt, options.defaults);
} else if (style === '`' && typeof options.fmt === 'function') {
this.style = new STYLES['`'][0](options.fmt, options.defaults);
} else {
throw new ApiDOMStructuredError(
`Invalid style: "${style}". Valid styles are: ${Object.keys(STYLES).join(', ')}`,
{
style,
},
);
}

if (options.validate) {
this.style.validate();
}

this.datefmt = options.datefmt;
}
}

export default Formatter;
4 changes: 2 additions & 2 deletions packages/apidom-logging/src/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export interface LogRecordConstructor<T extends Error = Error> {
class LogRecord<T extends Error = Error> implements LogRecordInstance<T> {
public readonly name: string;

public readonly message: string;

public readonly levelname: string;

public readonly levelno: number;

public readonly message: string;

public readonly process?: number;

public readonly processName?: string;
Expand Down
9 changes: 5 additions & 4 deletions packages/apidom-logging/src/styles/StringTemplateStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class StringTemplateStyle implements Style {

public static readonly validationPattern = /\${(.*?)}/g;

protected fmt: Format;
public readonly fmt: Format;

protected defaults?: Defaults;
protected readonly defaults?: Defaults;

constructor(fmt: Format, defaults?: Defaults) {
this.fmt = fmt;
constructor(fmt?: Format, defaults?: Defaults) {
const self = this.constructor as unknown as typeof StringTemplateStyle;
this.fmt = fmt ?? self.defaultFormat;
this.defaults = defaults;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/apidom-logging/src/styles/Style.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { LogRecordInstance } from '../LogRecord';

export default interface Style {
interface Style {
readonly fmt: string | ((value: Record<string, unknown>) => string);
usesTime(): boolean;
validate(): boolean;
format(record: LogRecordInstance): string;
}

export default Style;
11 changes: 7 additions & 4 deletions packages/apidom-logging/src/styles/TemplateLiteralStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ export type Format = (value: Record<string, unknown>) => string;
type Defaults = Record<string, unknown>;

class TemplateLiteralStyle implements Style {
protected fmt: Format;
public static readonly defaultFormat: Format = ({ message }) => `${message}`;

protected defaults?: Defaults;
public readonly fmt: Format;

constructor(fmt: Format, defaults?: Defaults) {
this.fmt = fmt;
protected readonly defaults?: Defaults;

constructor(fmt?: Format, defaults?: Defaults) {
const self = this.constructor as unknown as typeof TemplateLiteralStyle;
this.fmt = fmt ?? self.defaultFormat;
this.defaults = defaults;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/apidom-logging/src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import StringTemplateStyle from './StringTemplateStyle';
import TemplateLiteralStyle from './TemplateLiteralStyle';
import type { Format as TemplateLiteralStyleFormat } from './TemplateLiteralStyle';

export type { default as Style } from './Style';

const stringTemplateStyle: [typeof StringTemplateStyle, string] = [
StringTemplateStyle,
'${levelname}:${name}:${message}', // eslint-disable-line no-template-curly-in-string
Expand All @@ -15,6 +17,6 @@ const templateLiteralStyle: [typeof TemplateLiteralStyle, TemplateLiteralStyleFo
const STYLES = {
$: stringTemplateStyle,
'`': templateLiteralStyle,
};
} as const;

export default STYLES;

0 comments on commit eae432e

Please sign in to comment.