diff --git a/package.json b/package.json index a9ad4b24..4fa90026 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "signales", - "version": "2.0.4", + "version": "2.0.5", "description": "Highly configurable logging utility", "license": "MIT", "repository": "anru/signales", @@ -39,7 +39,7 @@ "devDependencies": { "@types/node": "^11.11.3", "tsd": "^0.14.0", - "typescript": "^4.1.3" + "typescript": "^4.2.3" }, "author": { "name": "Klaus Sinani", diff --git a/src/logger-types.ts b/src/logger-types.ts index 89d84d2d..855181d9 100644 --- a/src/logger-types.ts +++ b/src/logger-types.ts @@ -1,8 +1,8 @@ 'use strict'; import figures from 'figures' -import type { DefaultLogTypes, LoggerTypesConf } from './types' +import type { DefaultLoggerTypes } from './types' -const logTypes: LoggerTypesConf = { +const logTypes: DefaultLoggerTypes = { error: { badge: figures.cross, color: 'red', diff --git a/src/main.ts b/src/main.ts index 761813f0..d1704749 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,7 @@ import type { ScopeFormatter, DefaultLogLevels, } from './types' -export type SignaleEntrypoint = typeof Signale & { Signale: SignaleConstructor} +export type SignaleEntrypoint = typeof Signale & { Signale: SignaleConstructor } const signale: SignaleEntrypoint = Object.assign(new Signale(), { Signale, @@ -25,8 +25,9 @@ export { Signales, } -// utility type -export type SignaleConstructorOptions = ConstructorOptions +// type for backward compartibility +// same as ConstructorOptions +export type SignaleConstructorOptions = ConstructorOptions // re-export types export { @@ -35,7 +36,10 @@ export { InstanceConfiguration, LoggerFunction, LoggerTypesConf, - ScopeFormatter + ScopeFormatter, + DefaultLogTypes, + DefaultLogLevels, + ConstructorOptions, } export default signale diff --git a/src/signale.ts b/src/signale.ts index 0ab51f68..dc995123 100644 --- a/src/signale.ts +++ b/src/signale.ts @@ -14,6 +14,7 @@ import { DefaultLogTypes, InstanceConfiguration, LoggerConfiguration, LoggerFunction, LoggerTypesConf, + DefaultLoggerTypes, DefaultLogLevels, ScopeFormatter, Secrets } from './types' @@ -52,17 +53,17 @@ function barsScopeFormatter(scopes: string[]): string { return scopes.map(scope => `[${scope}]`).join(' ') } -class SignaleImpl { +class SignaleImpl { _interactive: boolean _config: InstanceConfiguration - _customTypes: Partial> - _customLogLevels: Record + _customTypes: LoggerTypesConf & Partial> + _customLogLevels: Partial> & Record _logLevels: Record _disabled: boolean _scopeName: string | string[] _timers: Map _seqTimers: Array - _types: LoggerTypesConf + _types: Record>> & DefaultLoggerTypes _stream: WritableStream | WritableStream[] _longestLabel: string _secrets: Secrets @@ -165,7 +166,7 @@ class SignaleImpl _types[x as T].label) + const labels = Object.keys(_types).map(x => _types[x as T].label || '') return labels.reduce((x, y) => x.length > y.length ? x : y) } @@ -173,8 +174,8 @@ class SignaleImpl, custom: Partial>): LoggerTypesConf { - const types: LoggerTypesConf = Object.assign({}, standard) as LoggerTypesConf + _mergeTypes(standard: DefaultLoggerTypes, custom: LoggerTypesConf): Record>> & DefaultLoggerTypes { + const types: Record>> & DefaultLoggerTypes = Object.assign({}, standard) as Record>> & DefaultLoggerTypes Object.keys(custom).forEach(type => { types[type as T] = Object.assign({}, types[type as T], custom[type as T]) @@ -255,7 +256,7 @@ class SignaleImpl, ...args: any[]): string { + _buildSignale(type: Partial>, ...args: any[]): string { let msg let additional: AdditionalFormatObj = {} @@ -468,11 +469,11 @@ class SignaleImpl = Record & +export type SignaleType = Record & Record & SignaleImpl & - (new (options?: ConstructorOptions) => SignaleType) + (new (options?: ConstructorOptions) => SignaleType) -export type SignaleConstructor = +export type SignaleConstructor = new (options?: ConstructorOptions) => SignaleType export default SignaleImpl as unknown as SignaleType diff --git a/src/types.ts b/src/types.ts index 9d8a43f9..53f33748 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,7 +37,7 @@ export type DefaultLogLevels = 'info' | 'timer' | 'debug' | 'warn' | 'error' // alias for backward-compatibility export type LogLevel = DefaultLogLevels -export interface LoggerConfiguration { +export interface LoggerConfiguration { badge: string, color: ChalkColor | '', label: string, @@ -45,7 +45,8 @@ export interface LoggerConfiguration { stream?: WritableStream | WritableStream[], } -export type LoggerTypesConf = Record> +export type LoggerTypesConf = Record>> +export type DefaultLoggerTypes = Record> export interface InstanceConfiguration { displayBadge?: boolean, @@ -63,15 +64,18 @@ export interface InstanceConfiguration { export type ScopeFormatter = (scopePath: string[]) => string -export interface ConstructorOptions { +export interface ConstructorOptions { config?: InstanceConfiguration, disabled?: boolean, interactive?: boolean, logLevel?: L | DefaultLogLevels, - logLevels?: Record, + logLevels?: Partial> & Record, scope?: string | string[], scopeFormatter?: ScopeFormatter, secrets?: Secrets, stream?: WritableStream | WritableStream[], - types?: Partial>, + // we can't negate DefaultLogTypes from string + // see https://github.com/microsoft/TypeScript/pull/29317 (not merged as for 31 march 2021) + // so we can't distinguish logger configuration between default log types and passed one + types?: LoggerTypesConf & Partial> } diff --git a/test-d/custom-log-levels.ts b/test-d/custom-log-levels.ts index 21a1bae1..def569fa 100644 --- a/test-d/custom-log-levels.ts +++ b/test-d/custom-log-levels.ts @@ -8,6 +8,9 @@ const custom = new Signale({ }, logLevel: 'sherlock', types: { + debug: { + color: 'grey', + }, sherlock: { badge: '🔎', color: 'yellow', diff --git a/yarn.lock b/yarn.lock index 5d2a7955..f7024c8b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1202,10 +1202,10 @@ typedarray-to-buffer@^3.1.5: dependencies: is-typedarray "^1.0.0" -typescript@^4.1.3: - version "4.1.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" - integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg== +typescript@^4.2.3: + version "4.2.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3" + integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw== unique-string@^2.0.0: version "2.0.0"