Skip to content

Commit

Permalink
fix(typings): add custom logger typings (#3697)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed May 23, 2019
1 parent 35af633 commit 3de31f5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
32 changes: 16 additions & 16 deletions index.d.ts
Expand Up @@ -6,7 +6,7 @@ import { EventEmitter } from 'events'
import { Readable } from 'stream';
import { Socket } from 'net';
import { IncomingMessage, ServerResponse } from 'http';
import { EggLogger, EggLoggers, LoggerLevel as EggLoggerLevel, EggContextLogger } from 'egg-logger';
import { EggLogger, EggLoggers, LoggerLevel as EggLoggerLevel, EggLoggersOptions, EggLoggerOptions, EggContextLogger } from 'egg-logger';
import { HttpClient, RequestOptions2 as RequestOptions } from 'urllib';
import {
EggCoreBase,
Expand Down Expand Up @@ -210,6 +210,16 @@ declare module 'egg' {
type IgnoreItem = string | RegExp | ((ctx: Context) => boolean);
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];

/** logger config of egg */
export interface EggLoggerConfig extends RemoveSpecProp<EggLoggersOptions, 'type'> {
/** custom config of coreLogger */
coreLogger?: Partial<EggLoggerOptions>;
/** allow debug log at prod, defaults to true */
allowDebugAtProd?: boolean;
/** disable logger console after app ready. defaults to `false` on local and unittest env, others is `true`. */
disableConsoleAfterReady?: boolean;
}

/** Custom Loader Configuration */
export interface CustomLoaderConfig extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
/**
Expand Down Expand Up @@ -312,21 +322,11 @@ declare module 'egg' {
* @property {Object} coreLogger - custom config of coreLogger
* @property {Boolean} allowDebugAtProd - allow debug log at prod, defaults to true
*/
logger: {
dir: string;
encoding: string;
env: EggEnvType;
level: LoggerLevel;
consoleLevel: LoggerLevel;
disableConsoleAfterReady: boolean;
outputJSON: boolean;
buffer: boolean;
appLogName: string;
coreLogName: string;
agentLogName: string;
errorLogName: string;
coreLogger: any;
allowDebugAtProd: boolean;
logger: EggLoggerConfig;

/** custom logger of egg */
customLogger: {
[key: string]: EggLoggerOptions;
};

/** Configuration of httpclient in egg. */
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/apps/app-ts-type-check/normal.ts
Expand Up @@ -138,6 +138,36 @@ config.httpclient = {
proxy: 'http://127.0.0.1:8888'
}
config.httpclient = httpclientOption;
config.logger = {
dir: 'logs',
encoding: 'utf8',
env: 'prod',
level: 'INFO',
consoleLevel: 'INFO',
disableConsoleAfterReady: true,
outputJSON: false,
buffer: true,
appLogName: `app-web.log`,
coreLogName: 'egg-web.log',
agentLogName: 'egg-agent.log',
errorLogName: 'common-error.log',
allowDebugAtProd: false,
coreLogger: {},
};
config.customLogger = {
myLogger: {
file: './test.log',
jsonFile: './test.json',
formatter: (meta: any) => (meta.date + ' ' + meta.level + ' ' + meta.pid + ' ' + meta.message),
contextFormatter: meta => JSON.stringify(meta),
buffer: true,
eol: '\r\n',
},

otherLogger: {
file: './other.log',
}
}

// partial config
const config2 = {} as PowerPartial<EggAppConfig>;
Expand All @@ -155,6 +185,16 @@ config2.customLoader = {
config2.security = {
csrf: false,
}
config2.logger = {
dir: 'logs',
encoding: 'utf8',
env: 'prod',
level: 'INFO',
coreLogger: {
file: './test.log',
level: 'ALL',
}
}

// singleton
const redis = {} as Singleton<{ test(): void; }>;
Expand Down

0 comments on commit 3de31f5

Please sign in to comment.