Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Typescript support for custom log levels #2349

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 39 additions & 54 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ declare namespace winston {
export import transports = Transports;
export import transport = Transport;

type defaultLevels = "error" | "warn" | "info" | "http" | "verbose" | "debug" | "silly";

class ExceptionHandler {
constructor(logger: Logger);
logger: Logger;
Expand Down Expand Up @@ -63,25 +65,25 @@ declare namespace winston {
done(info?: any): boolean;
}

type LogCallback = (
type LogCallback<T extends string = defaultLevels> = (
error?: any,
level?: string,
level?: T,
message?: string,
meta?: any
) => void;

interface LogEntry {
level: string;
interface LogEntry<T extends string = defaultLevels> {
level: T;
message: string;
[optionName: string]: any;
}

interface LogMethod {
(level: string, message: string, callback: LogCallback): Logger;
(level: string, message: string, meta: any, callback: LogCallback): Logger;
(level: string, message: string, ...meta: any[]): Logger;
(entry: LogEntry): Logger;
(level: string, message: any): Logger;
interface LogMethod<T extends string = defaultLevels> {
(level: T, message: string, callback: LogCallback<T>): Logger<T>;
(level: T, message: string, meta: any, callback: LogCallback<T>): Logger<T>;
(level: T, message: string, ...meta: any[]): Logger<T>;
(entry: LogEntry<T>): Logger<T>;
(level: T, message: any): Logger<T>;
}

interface LeveledLogMethod {
Expand All @@ -92,11 +94,11 @@ declare namespace winston {
(infoObject: object): Logger;
}

interface LoggerOptions {
levels?: Config.AbstractConfigSetLevels;
interface LoggerOptions<T extends string = defaultLevels> {
levels?: Config.AbstractConfigSetLevels<T>;
silent?: boolean;
format?: logform.Format;
level?: string;
level?: T;
exitOnError?: Function | boolean;
defaultMeta?: any;
transports?: Transport[] | Transport;
Expand All @@ -106,46 +108,34 @@ declare namespace winston {
rejectionHandlers?: any;
}

class Logger extends NodeJSStream.Transform {
constructor(options?: LoggerOptions);
// Used to dynamically add log levels
type LoggerLogFunctionsMap<T extends string> = {
[key in T]: LeveledLogMethod;
};
type LoggerLogEnabledMap<T extends string> = {
[key in `is${Capitalize<T>}Enabled`]: () => boolean;
};

interface LoggerBase<T extends string> extends NodeJSStream.Transform {
new <T extends string = defaultLevels>(options?: LoggerOptions<T>): Logger<T>;

silent: boolean;
format: logform.Format;
levels: Config.AbstractConfigSetLevels;
level: string;
levels: Config.AbstractConfigSetLevels<T>;
level: T;
transports: Transport[];
exceptions: ExceptionHandler;
rejections: RejectionHandler;
profilers: object;
exitOnError: Function | boolean;
defaultMeta?: any;

log: LogMethod;
log: LogMethod<T>;
add(transport: Transport): this;
remove(transport: Transport): this;
clear(): this;
close(): this;

// for cli and npm levels
error: LeveledLogMethod;
warn: LeveledLogMethod;
help: LeveledLogMethod;
data: LeveledLogMethod;
info: LeveledLogMethod;
debug: LeveledLogMethod;
prompt: LeveledLogMethod;
http: LeveledLogMethod;
verbose: LeveledLogMethod;
input: LeveledLogMethod;
silly: LeveledLogMethod;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  [key in T]: LeveledLogMethod;

should declare the respective methods for immediate use.

Might make sense to have the levels with their respective numbers as Type parameter:

interface LoggerBase<Levels extends AbstractConfigSetLevels> {
  [key in keyof Levels]: LeveledLogMethod;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind that custom log levels still break application-insights instrumentation translating them to the correct appinsights severities.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eschablowski thoughts?


// for syslog levels only
emerg: LeveledLogMethod;
alert: LeveledLogMethod;
crit: LeveledLogMethod;
warning: LeveledLogMethod;
notice: LeveledLogMethod;

query(
options?: QueryOptions,
callback?: (err: Error, results: any) => void
Expand All @@ -155,36 +145,31 @@ declare namespace winston {
startTimer(): Profiler;
profile(id: string | number, meta?: Record<string, any>): this;

configure(options: LoggerOptions): void;
configure(options: LoggerOptions<T>): void;

child(options: Object): this;

isLevelEnabled(level: string): boolean;
isErrorEnabled(): boolean;
isWarnEnabled(): boolean;
isInfoEnabled(): boolean;
isVerboseEnabled(): boolean;
isDebugEnabled(): boolean;
isSillyEnabled(): boolean;
isLevelEnabled(level: T): boolean;
}

export type Logger<T extends string = defaultLevels> = LoggerBase<T> & LoggerLogFunctionsMap<T> & LoggerLogEnabledMap<T>;
export const Logger: Logger;
class Container {
loggers: Map<string, Logger>;
options: LoggerOptions;
options: LoggerOptions<string>;

add(id: string, options?: LoggerOptions): Logger;
get(id: string, options?: LoggerOptions): Logger;
add(id: string, options?: LoggerOptions<string>): Logger;
get(id: string, options?: LoggerOptions<string>): Logger;
has(id: string): boolean;
close(id?: string): void;

constructor(options?: LoggerOptions);
constructor(options?: LoggerOptions<string>);
}

let version: string;
let loggers: Container;

let addColors: (target: Config.AbstractConfigSetColors) => any;
let createLogger: (options?: LoggerOptions) => Logger;
let createLogger: <T extends string>(options?: LoggerOptions<T>) => Logger<T>;

// Pass-through npm level methods routed to the default logger.
let error: LeveledLogMethod;
Expand All @@ -196,7 +181,7 @@ declare namespace winston {
let silly: LeveledLogMethod;

// Other pass-through methods routed to the default logger.
let log: LogMethod;
let log: LogMethod<defaultLevels>;
let query: (
options?: QueryOptions,
callback?: (err: Error, results: any) => void
Expand All @@ -207,7 +192,7 @@ declare namespace winston {
let clear: () => Logger;
let startTimer: () => Profiler;
let profile: (id: string | number) => Logger;
let configure: (options: LoggerOptions) => void;
let configure: <T extends string = defaultLevels>(options: LoggerOptions<T>) => void;
let child: (options: Object) => Logger;
let level: string;
let exceptions: ExceptionHandler;
Expand Down
9 changes: 5 additions & 4 deletions lib/winston/config/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

/// <reference types="node" />


declare namespace winston {
interface AbstractConfigSetLevels {
[key: string]: number;
type AbstractConfigSetLevels<T extends string = string> = {
[key in T]: number;
}

interface AbstractConfigSetColors {
[key: string]: string | string[];
}

interface AbstractConfigSet {
levels: AbstractConfigSetLevels;
interface AbstractConfigSet<T extends string = string> {
levels: AbstractConfigSetLevels<T>;
colors: AbstractConfigSetColors;
}

Expand Down
39 changes: 39 additions & 0 deletions test/typescript-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,42 @@ logger.isInfoEnabled();
logger.isVerboseEnabled();
logger.isDebugEnabled();
logger.isSillyEnabled();

let logger2 = winston.createLogger({
levels: {
"one": 1,
two: 2
},
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console({ level: 'info' }),
],
});

logger2.isOneEnabled();
// @ts-expect-error
logger2.isSillyEnabled();

let loggerFromClass = new winston.Logger({});
loggerFromClass.isInfoEnabled();
loggerFromClass.debug("");
// @ts-expect-error
loggerFromClass.a("");
// @ts-expect-error
loggerFromClass.isAEnabled();


let loggerFromClass2 = new winston.Logger({
levels: {
"a": 2,
"b": 3
}
});
// @ts-expect-error
loggerFromClass2.isInfoEnabled();
// @ts-expect-error
loggerFromClass2.debug("");

loggerFromClass2.a("");
loggerFromClass2.isAEnabled();