logpm
Version:
JavaScript semantic logging
116 lines (115 loc) • 3.95 kB
text/typescript
/**
* Available logging levels
*/
export declare const LogLevel: Readonly<{
Error: 1;
Warn: 2;
Info: 3;
Debug: 4;
Trace: 5;
}>;
export type LogLevelValues = (typeof LogLevel)[keyof typeof LogLevel];
interface TokenizedMessage {
message: string;
tokens: any;
}
/**
* Special class with internals. Not accessible to outside world despite declaration.
* Warning: "export" keyword gets removed after tests
*/
export declare class Internals {
#private;
/**
*
* @param message Message to log with argument placeholders to be filled
* @param args Arguments to be inserted in message template
* @returns Evaluated message and tokens.
*/
tokenize(message: string, ...args: any[]): TokenizedMessage;
}
export interface TimeProvider {
/**
* Returns current time
* @returns Current time
*/
get now(): string;
}
/**
* Provides current time in UTC format
*/
export declare class DefaultTimeProvider implements TimeProvider {
get now(): string;
}
export interface LogStream {
/**
* Writes log object to output of choice
* @param obj Log entity to write to log
*/
write(obj: any): void;
}
/**
* Writes logs as JSONs to STDOUT
*/
export declare class ConsoleLogStream implements LogStream {
write(obj: any): void;
}
/**
* Semantic logging class
*/
export declare class Logger {
#private;
/**
* Create Logger instance
* @constructor
* @param context (reqired) Name the context where operations are logged. Usually name of the class
* @param scope (optional) Common scope object for all logged messages
* @param timeProvider (optional/advanced) Leave null for default behavior or provide custom way to assign timestamps
* @param stream (optional/advanced) Leave null for default behavior or pass custom nonblocking stream. Async operations are not supported and not desired
*/
constructor(context: string, scope?: any, timeProvider?: TimeProvider, stream?: LogStream);
/**
* Creates new sub scope from existing logger
* @param context New scope context name
* @param scope Optional scope data
* @returns {Logger}
*/
scopeTo(context: string, scope?: any): Logger;
/**
* Error log
* @param message Message to log. May contain placeholders in format: {name}
* @param args Arguments to fill within placeholders. Order of placeholders matches order of arguments
*/
e(message: string, ...args: any[]): void;
/**
* Warning log
* @param message Message to log. May contain placeholders in format: {name}
* @param args Arguments to fill within placeholders. Order of placeholders matches order of arguments
*/
w(message: string, ...args: any[]): void;
/**
* Information log
* @param message Message to log. May contain placeholders in format: {name}
* @param args Arguments to fill within placeholders. Order of placeholders matches order of arguments
*/
i(message: string, ...args: any[]): void;
/**
* Debug log
* @param message Message to log. May contain placeholders in format: {name}
* @param args Arguments to fill within placeholders. Order of placeholders matches order of arguments
*/
d(message: string, ...args: any[]): void;
/**
* Trace (verbose) log
* @param message Message to log. May contain placeholders in format: {name}
* @param args Arguments to fill within placeholders. Order of placeholders matches order of arguments
*/
t(message: string, ...args: any[]): void;
/**
* Log with level
* @param level Logging level
* @param message Message to log. May contain placeholders in format: {name}
* @param args Arguments to fill within placeholders. Order of placeholders matches order of arguments
*/
ll(level: LogLevelValues, message: string, ...args: any[]): void;
}
export {};