From 02c6ebbb2d000e2ac4c7b801725108fca7d4076c Mon Sep 17 00:00:00 2001 From: taoqf Date: Mon, 16 Oct 2017 13:43:41 +0800 Subject: [PATCH] fix: add typescript definition --- README.md | 16 +++++++++- types/log4js.d.ts | 61 ++++++++++++++++++++++++++++-------- types/test.ts | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 997a8af..70ae719 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Output (in `cheese.log`): ```bash [2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe! [2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria. -``` +``` ## Note for library makers @@ -74,6 +74,20 @@ Available [here](https://nomiddlename.github.io/log4js-node/). There's also [an example application](https://github.com/nomiddlename/log4js-example). +## TypeScript +```ts +import { configure, getLogger } from './log4js'; +configure('./filename'); +const logger = getLogger(); +logger.level = 'debug'; +logger.debug("Some debug messages"); + +configure({ + appenders: { cheese: { type: 'file', filename: 'cheese.log' } }, + categories: { default: { appenders: ['cheese'], level: 'error' } } +}); +``` + ## Contributing Contributions welcome, but take a look at the [rules](https://github.com/nomiddlename/log4js-node/wiki/Contributing) first. diff --git a/types/log4js.d.ts b/types/log4js.d.ts index 66f827c..1b4fe04 100644 --- a/types/log4js.d.ts +++ b/types/log4js.d.ts @@ -5,6 +5,12 @@ export function getLogger(category?: string): Logger; export function configure(filename: string): void; export function configure(config: Configuration): void; +export function addLayout(name: string, config: (a: any) => (logEvent: LoggingEvent) => string); + +export function connectLogger(logger: Logger, options: { format?: string; level?: string; nolog?: any; }): any; // express.Handler; + +export function shutdown(cb?: (error: Error) => void); + export interface BaseLayout { type: 'basic'; } @@ -21,9 +27,18 @@ export interface DummyLayout { type: 'dummy'; } +export interface Level { + isEqualTo(other: string): boolean; + isEqualTo(otherLevel: Level): boolean; + isLessThanOrEqualTo(other: string): boolean; + isLessThanOrEqualTo(otherLevel: Level): boolean; + isGreaterThanOrEqualTo(other: string): boolean; + isGreaterThanOrEqualTo(otherLevel: Level): boolean; +} + export interface LoggingEvent { categoryName: string; // name of category - level: string; // level of message + level: Level; // level of message data: any[]; // objects to log startTime: Date; pid: number; @@ -44,7 +59,12 @@ export interface PatternLayout { tokens?: { [name: string]: Token }; } -export type Layout = BaseLayout | ColoredLayout | MessagePassThroughLayout | DummyLayout | PatternLayout; +export interface CustomLayout { + [key: string]: any; + type: string; +} + +export type Layout = BaseLayout | ColoredLayout | MessagePassThroughLayout | DummyLayout | PatternLayout | CustomLayout; /** * Category Filter @@ -80,6 +100,11 @@ export interface FileAppender { backups?: number; // defaults to basic layout layout?: Layout; + numBackups?: number; + compress?: boolean; // compress the backups + encoding?: string; + mode?: number; + flags?: string; } export interface SyncfileAppender { @@ -99,7 +124,7 @@ export interface DateFileAppender { // the path of the file where you want your logs written. filename: string; // defaults to basic layout - layout: Layout; + layout?: Layout; // defaults to .yyyy-MM-dd - the pattern to use to determine when to roll the logs. /** * The following strings are recognised in the pattern: @@ -301,7 +326,7 @@ export interface SmtpAppender { plugin?: string; // configuration for the transport plugin options?: any; - }; + } | string; // send logs as email attachment attachment?: { // (defaults to false) @@ -324,7 +349,7 @@ export interface SmtpAppender { // (defaults to false) - send the email as HTML instead of plain text html?: boolean; // (defaults to basicLayout) - layout: Layout; + layout?: Layout; } export interface StandardErrorAppender { @@ -359,22 +384,32 @@ export type Appender = CategoryFilterAppender | RecordingAppender | SmtpAppender | StandardErrorAppender - | StandardOutputAppender + | StandardOutputAppender; export interface Configuration { appenders: { [name: string]: Appender; }; categories: { [name: string]: { appenders: string[]; level: string; } }; + pm2?: boolean; + pm2InstanceVar?: string; } export interface Logger { + setLevel(level: string): void; + setLevel(level: Level): void; new(dispatch: Function, name: string): Logger; level: string; log(...args: any[]): void; - isLevelEnabled(level: string): boolean; + isLevelEnabled(level?: string): boolean; + + isDebugEnabled(): boolean; + isInfoEnabled(): boolean; + isWarnEnabled(): boolean; + isErrorEnabled(): boolean; + isFatalEnabled(): boolean; _log(level: string, data: any): void; @@ -384,15 +419,15 @@ export interface Logger { clearContext(): void; - trace(...args: any[]): void; + trace(message: string, ...args: any[]): void; - debug(...args: any[]): void; + debug(message: string, ...args: any[]): void; - info(...args: any[]): void; + info(message: string, ...args: any[]): void; - warn(...args: any[]): void; + warn(message: string, ...args: any[]): void; - error(...args: any[]): void; + error(message: string, ...args: any[]): void; - fatal(...args: any[]): void; + fatal(message: string, ...args: any[]): void; } diff --git a/types/test.ts b/types/test.ts index fb66f88..91e1b84 100644 --- a/types/test.ts +++ b/types/test.ts @@ -18,6 +18,86 @@ log4js.configure({ categories: { default: { appenders: ['cheese'], level: 'error' } } }); +log4js.configure({ + appenders: { + out: { type: 'file', filename: 'pm2logs.log' } + }, + categories: { + default: { appenders: ['out'], level: 'info' } + }, + pm2: true, + pm2InstanceVar: 'INSTANCE_ID' +}); + +log4js.addLayout('json', config => function (logEvent) { + return JSON.stringify(logEvent) + config.separator; +}); + +log4js.configure({ + appenders: { + out: { type: 'stdout', layout: { type: 'json', separator: ',' } } + }, + categories: { + default: { appenders: ['out'], level: 'info' } + } +}); + +log4js.configure({ + appenders: { + file: { type: 'dateFile', filename: 'thing.log', pattern: '.mm' } + }, + categories: { + default: { appenders: ['file'], level: 'debug' } + } +}); + +const logger4 = log4js.getLogger('thing'); + +const logger5 = log4js.getLogger('json-test'); +logger5.info('this is just a test'); +logger5.error('of a custom appender'); +logger5.warn('that outputs json'); +log4js.shutdown(() => { }); + +log4js.configure({ + appenders: { + cheeseLogs: { type: 'file', filename: 'cheese.log' }, + console: { type: 'console' } + }, + categories: { + cheese: { appenders: ['cheeseLogs'], level: 'error' }, + another: { appenders: ['console'], level: 'trace' }, + default: { appenders: ['console', 'cheeseLogs'], level: 'trace' } + } +}); + +const logger6 = log4js.getLogger('cheese'); +// only errors and above get logged. +const otherLogger = log4js.getLogger(); + +// this will get coloured output on console, and appear in cheese.log +otherLogger.error('AAArgh! Something went wrong', { some: 'otherObject', useful_for: 'debug purposes' }); +otherLogger.log('This should appear as info output'); + +// these will not appear (logging level beneath error) +logger6.trace('Entering cheese testing'); +logger6.debug('Got cheese.'); +logger6.info('Cheese is Gouda.'); +logger6.log('Something funny about cheese.'); +logger6.warn('Cheese is quite smelly.'); +// these end up only in cheese.log +logger6.error('Cheese %s is too ripe!', 'gouda'); +logger6.fatal('Cheese was breeding ground for listeria.'); + +// these don't end up in cheese.log, but will appear on the console +const anotherLogger = log4js.getLogger('another'); +anotherLogger.debug('Just checking'); + +// will also go to console and cheese.log, since that's configured for all categories +const pantsLog = log4js.getLogger('pants'); +pantsLog.debug('Something for pants'); + + import { configure, getLogger } from './log4js'; configure('./filename'); const logger2 = getLogger();