-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logging): add initial implementation of Formatter (#3272)
Refs #3197
- Loading branch information
Showing
7 changed files
with
64 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters