From 07b14695e62abd5419ba1f82c5d1dc96f5c3a984 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 8 Mar 2024 14:55:51 -0500 Subject: [PATCH] refactor: Reorganize some types/constants locations and normalize some constant names --- README.md | 6 ++--- src/destinations.ts | 8 +++--- src/factory.ts | 13 ++++----- src/funcs.ts | 32 +++++++++++++++++++++- src/index.ts | 2 +- src/pretty.ts | 40 +++------------------------ src/types.ts | 66 ++++++++++++++++++++++----------------------- 7 files changed, 80 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 36a67fb..b879cb2 100644 --- a/README.md +++ b/README.md @@ -353,9 +353,9 @@ const myCustomizedConfig = prettyOptsFactory({ colorize: false }); Pre-configured `PrettyOptions` are also provided for different destinations: ```ts -import { - prettyConsole, // default config - prettyFile // disables colorize +import { + PRETTY_OPTS_CONSOLE, // default config + PRETTY_OPTS_FILE // disables colorize } from "@foxxmd/logging/factory"; ``` diff --git a/src/destinations.ts b/src/destinations.ts index b162a0f..70bd402 100644 --- a/src/destinations.ts +++ b/src/destinations.ts @@ -7,7 +7,7 @@ import { } from "./types.js"; import {DestinationStream, pino, destination} from "pino"; import {build} from "pino-pretty" -import {prettyConsole, prettyFile} from "./pretty.js"; +import {PRETTY_OPTS_CONSOLE, PRETTY_OPTS_FILE} from "./pretty.js"; import {fileOrDirectoryIsWriteable} from "./util.js"; import path from "path"; import {ErrorWithCause} from "pony-cause"; @@ -73,7 +73,7 @@ export const buildDestinationRollingFile = async (level: LogLevel | false, optio return { level: level, - stream: build({...prettyFile, ...rest, destination: rollingDest}) + stream: build({...PRETTY_OPTS_FILE, ...rest, destination: rollingDest}) }; } @@ -94,7 +94,7 @@ export const buildDestinationFile = (level: LogLevel | false, options: FileDesti return { level: level, - stream: build({...prettyFile, ...rest, destination: dest}) + stream: build({...PRETTY_OPTS_FILE, ...rest, destination: dest}) }; } catch (e: any) { throw new ErrorWithCause('WILL NOT write to file due to an error while trying to access the specified directory', {cause: e as Error}); @@ -109,7 +109,7 @@ export const buildDestinationFile = (level: LogLevel | false, options: FileDesti export const buildDestinationStream = (level: LogLevel, options: StreamDestination): LogLevelStreamEntry => { return { level: level, - stream: build({...prettyConsole, ...options}) + stream: build({...PRETTY_OPTS_CONSOLE, ...options}) } } diff --git a/src/factory.ts b/src/factory.ts index b1796ad..9481c6f 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -1,11 +1,8 @@ import { - prettyConsole, - prettyFile, - PRETTY_COLORS, - PRETTY_COLORS_STR, + PRETTY_OPTS_CONSOLE, + PRETTY_OPTS_FILE, //PRETTY_LEVELS, //PRETTY_LEVELS_STR, - PRETTY_ISO8601, prettyOptsFactory } from "./pretty.js"; import { @@ -16,11 +13,11 @@ import { buildDestinationFile } from "./destinations.js"; import {buildLogger} from './loggers.js'; -import {FileDestination, StreamDestination} from './types.js' +import {FileDestination, PRETTY_COLORS, PRETTY_COLORS_STR, PRETTY_ISO8601, StreamDestination} from './types.js' export { - prettyConsole, - prettyFile, + PRETTY_OPTS_CONSOLE, + PRETTY_OPTS_FILE, PRETTY_COLORS, PRETTY_COLORS_STR, //PRETTY_LEVELS, diff --git a/src/funcs.ts b/src/funcs.ts index 0165d1f..4f5623f 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -1,8 +1,38 @@ import process from "process"; -import {FileLogOptionsParsed, isLogOptions, LogLevel, LogOptions, LogOptionsParsed} from "./types.js"; +import {FileLogOptions, FileLogOptionsParsed, LOG_LEVELS, LogLevel, LogOptions, LogOptionsParsed} from "./types.js"; import {logPath, projectDir} from "./constants.js"; import {isAbsolute, resolve} from 'node:path'; +export const isLogOptions = (obj: object = {}): obj is LogOptions => { + return Object.entries(obj).every(([key, val]) => { + if (val === undefined) { + return true; + } + const t = typeof val; + if (key === 'file') { + if (t === 'object') { + return isFileLogOptions(t); + } + return t === 'string' || val === false; + } + if (t !== 'string') { + return false; + } + return LOG_LEVELS.includes(val.toLocaleLowerCase()); + }); +} +const isFileLogOptions = (obj: any): obj is FileLogOptions => { + if (obj === null || typeof obj !== 'object') { + return false; + } + const levelOk = obj.level === undefined || ('level' in obj && obj.level === false || LOG_LEVELS.includes(obj.level.toLocaleLowerCase())); + const pathOk = obj.path === undefined || ('path' in obj && typeof obj.path === 'string' || typeof obj.path === 'function'); + const frequencyOk = obj.frequency === undefined || ('frequency' in obj && typeof obj.frequency === 'string' || typeof obj.frequency === 'number'); + const sizeOk = obj.size === undefined || ('size' in obj && typeof obj.size === 'string' || typeof obj.size === 'number'); + const tsOk = obj.timestamp === undefined || ('timestamp' in obj && typeof obj.timestamp === 'string'); + + return levelOk && pathOk && frequencyOk && sizeOk && tsOk; +} /** * Takes an object and parses it into a fully-populated LogOptions object based on opinionated defaults * */ diff --git a/src/index.ts b/src/index.ts index 92b32c5..50da2a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import { LogLevelStreamEntry, LogOptions, FileLogOptions, - isLogOptions, LogData, LogLevel, LOG_LEVELS, @@ -11,6 +10,7 @@ import { } from './types.js' import { + isLogOptions, parseLogOptions, } from './funcs.js' diff --git a/src/pretty.ts b/src/pretty.ts index 43c1ed7..167d48f 100644 --- a/src/pretty.ts +++ b/src/pretty.ts @@ -1,40 +1,6 @@ import {PrettyOptions} from "pino-pretty"; import {CWD} from "./util.js"; -import {CUSTOM_LEVELS} from "./types.js"; - -/** - * Additional levels included in @foxxmd/logging as an object - * - * These are always applied when using `prettyOptsFactory` but can be overridden - * */ -const PRETTY_LEVELS: Extract = CUSTOM_LEVELS; -/** - * Additional levels included in @foxxmd/logging as a string - * - * These are always applied when using `prettyOptsFactory` but can be overridden - * */ -const PRETTY_LEVELS_STR: Extract = 'verbose:25,log:21'; - -/** - * Additional level colors included in @foxxmd/logging as an object - * - * These are always applied when using `prettyOptsFactory` but can be overridden - * */ -export const PRETTY_COLORS_STR: Extract = 'verbose:magenta,log:greenBright'; -/** - * Additional level colors included in @foxxmd/logging as a string - * - * These are always applied when using `prettyOptsFactory` but can be overridden - * */ -export const PRETTY_COLORS: Extract = { - 'verbose': 'magenta', - 'log': 'greenBright' -} - -/** - * Use on `translateTime` pino-pretty option to print timestamps in ISO8601 format - * */ -export const PRETTY_ISO8601 = 'SYS:yyyy-mm-dd"T"HH:MM:ssp'; +import {PRETTY_COLORS, PRETTY_COLORS_STR, PRETTY_LEVELS, PRETTY_LEVELS_STR} from "./types.js"; /** * Builds the opinionated `@foxxmd/logging` defaults for pino-pretty `PrettyOptions` and merges them with an optional user-provided `PrettyOptions` object @@ -108,13 +74,13 @@ const buildColors = (userColors: PrettyOptions['customColors'] = {}): PrettyOpti * * @source * */ -export const prettyConsole: PrettyOptions = prettyOptsFactory({sync: true}) +export const PRETTY_OPTS_CONSOLE: PrettyOptions = prettyOptsFactory({sync: true}) /** * Pre-defined pino-pretty `PrettyOptions` for use with file output * * @source * */ -export const prettyFile: PrettyOptions = prettyOptsFactory({ +export const PRETTY_OPTS_FILE: PrettyOptions = prettyOptsFactory({ colorize: false, sync: false, }); diff --git a/src/types.ts b/src/types.ts index 90ed6f4..85ba28a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import {DestinationStream, Level, Logger as PinoLogger, LoggerOptions, StreamEntry} from 'pino'; +import {DestinationStream, Logger as PinoLogger, LoggerOptions, StreamEntry} from 'pino'; import {ErrorWithCause} from "pony-cause"; import {PrettyOptions} from "pino-pretty"; import {MarkRequired} from "ts-essentials"; @@ -25,25 +25,6 @@ export interface LogOptions { console?: LogLevel } -export const isLogOptions = (obj: object = {}): obj is LogOptions => { - return Object.entries(obj).every(([key, val]) => { - if(val === undefined) { - return true; - } - const t = typeof val; - if(key === 'file') { - if(t === 'object') { - return isFileLogOptions(t); - } - return t === 'string' || val === false; - } - if(t !== 'string') { - return false; - } - return LOG_LEVELS.includes(val.toLocaleLowerCase()); - }); -} - export type Logger = PinoLogger & { labels: any[] addLabel: (value: any) => void @@ -143,19 +124,6 @@ export interface FileLogOptionsStrong extends FileLogOptions { export type FileLogOptionsParsed = (Omit & {level: false}) | FileLogOptionsStrong -const isFileLogOptions = (obj: any): obj is FileLogOptions => { - if (obj === null || typeof obj !== 'object') { - return false; - } - const levelOk = obj.level === undefined || ('level' in obj && obj.level === false || LOG_LEVELS.includes(obj.level.toLocaleLowerCase())); - const pathOk = obj.path === undefined || ('path' in obj && typeof obj.path === 'string' || typeof obj.path === 'function'); - const frequencyOk = obj.frequency === undefined || ('frequency' in obj && typeof obj.frequency === 'string' || typeof obj.frequency === 'number'); - const sizeOk = obj.size === undefined || ('size' in obj && typeof obj.size === 'string' || typeof obj.size === 'number'); - const tsOk = obj.timestamp === undefined || ('timestamp' in obj && typeof obj.timestamp === 'string'); - - return levelOk && pathOk && frequencyOk && sizeOk && tsOk; -} - export type FileDestination = Omit & FileOptionsParsed; export type StreamDestination = Omit & {destination: number | DestinationStream | NodeJS.WritableStream}; @@ -190,3 +158,35 @@ export const CUSTOM_LEVELS: LoggerOptions<"verbose" | "log">['customLevels'] = { * Additional [Pino Log options](https://getpino.io/#/docs/api?id=options) that are passed to `pino()` on logger creation * */ export type PinoLoggerOptions = Omit + +/** + * Additional levels included in @foxxmd/logging as an object + * + * These are always applied when using `prettyOptsFactory` but can be overridden + * */ +export const PRETTY_LEVELS: Extract = CUSTOM_LEVELS; +/** + * Additional levels included in @foxxmd/logging as a string + * + * These are always applied when using `prettyOptsFactory` but can be overridden + * */ +export const PRETTY_LEVELS_STR: Extract = 'verbose:25,log:21'; +/** + * Additional level colors included in @foxxmd/logging as an object + * + * These are always applied when using `prettyOptsFactory` but can be overridden + * */ +export const PRETTY_COLORS_STR: Extract = 'verbose:magenta,log:greenBright'; +/** + * Additional level colors included in @foxxmd/logging as a string + * + * These are always applied when using `prettyOptsFactory` but can be overridden + * */ +export const PRETTY_COLORS: Extract = { + 'verbose': 'magenta', + 'log': 'greenBright' +} +/** + * Use on `translateTime` pino-pretty option to print timestamps in ISO8601 format + * */ +export const PRETTY_ISO8601 = 'SYS:yyyy-mm-dd"T"HH:MM:ssp'; -- 2.51.2