log4js.d.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Type definitions for log4js
  2. type Format = string | ((req: any, res: any, formatter: ((str: string) => string)) => string);
  3. export interface Log4js {
  4. getLogger(category?: string): Logger;
  5. configure(filename: string): Log4js;
  6. configure(config: Configuration): Log4js;
  7. addLayout(name: string, config: (a: any) => (logEvent: LoggingEvent) => string): void;
  8. connectLogger(logger: Logger, options: { format?: Format; level?: string; nolog?: any; }): any; // express.Handler;
  9. levels: Levels;
  10. shutdown(cb: (error: Error) => void): void | null;
  11. }
  12. export function getLogger(category?: string): Logger;
  13. export function configure(filename: string): Log4js;
  14. export function configure(config: Configuration): Log4js;
  15. export function addLayout(name: string, config: (a: any) => (logEvent: LoggingEvent) => any): void;
  16. export function connectLogger(logger: Logger, options: { format?: Format; level?: string; nolog?: any; statusRules?: any[], context?: boolean }): any; // express.Handler;
  17. export const levels: Levels;
  18. export function shutdown(cb?: (error: Error) => void): void | null;
  19. export interface BaseLayout {
  20. type: 'basic';
  21. }
  22. export interface ColoredLayout {
  23. type: 'colored' | 'coloured';
  24. }
  25. export interface MessagePassThroughLayout {
  26. type: 'messagePassThrough';
  27. }
  28. export interface DummyLayout {
  29. type: 'dummy';
  30. }
  31. export interface Level {
  32. isEqualTo(other: string): boolean;
  33. isEqualTo(otherLevel: Level): boolean;
  34. isLessThanOrEqualTo(other: string): boolean;
  35. isLessThanOrEqualTo(otherLevel: Level): boolean;
  36. isGreaterThanOrEqualTo(other: string): boolean;
  37. isGreaterThanOrEqualTo(otherLevel: Level): boolean;
  38. colour: string;
  39. level: number;
  40. levelStr: string;
  41. }
  42. export interface LoggingEvent {
  43. categoryName: string; // name of category
  44. level: Level; // level of message
  45. data: any[]; // objects to log
  46. startTime: Date;
  47. pid: number;
  48. context: any;
  49. cluster?: {
  50. workerId: number;
  51. worker: number;
  52. };
  53. }
  54. export type Token = ((logEvent: LoggingEvent) => string) | string;
  55. export interface PatternLayout {
  56. type: 'pattern';
  57. // specifier for the output format, using placeholders as described below
  58. pattern: string;
  59. // user-defined tokens to be used in the pattern
  60. tokens?: { [name: string]: Token };
  61. }
  62. export interface CustomLayout {
  63. [key: string]: any;
  64. type: string;
  65. }
  66. export type Layout = BaseLayout | ColoredLayout | MessagePassThroughLayout | DummyLayout | PatternLayout | CustomLayout;
  67. /**
  68. * Category Filter
  69. *
  70. * @see https://log4js-node.github.io/log4js-node/categoryFilter.html
  71. */
  72. export interface CategoryFilterAppender {
  73. type: "categoryFilter";
  74. // the category (or categories if you provide an array of values) that will be excluded from the appender.
  75. exclude?: string | string[];
  76. // the name of the appender to filter. see https://log4js-node.github.io/log4js-node/layouts.html
  77. appender?: string;
  78. }
  79. /**
  80. * No Log Filter
  81. *
  82. * @see https://log4js-node.github.io/log4js-node/noLogFilter.html
  83. */
  84. export interface NoLogFilterAppender {
  85. type: "noLogFilter";
  86. // the regular expression (or the regular expressions if you provide an array of values)
  87. // will be used for evaluating the events to pass to the appender.
  88. // The events, which will match the regular expression, will be excluded and so not logged.
  89. exclude: string | string[];
  90. // the name of an appender, defined in the same configuration, that you want to filter.
  91. appender: string;
  92. }
  93. /**
  94. * Console Appender
  95. *
  96. * @see https://log4js-node.github.io/log4js-node/console.html
  97. */
  98. export interface ConsoleAppender {
  99. type: 'console';
  100. // defaults to colouredLayout
  101. layout?: Layout;
  102. }
  103. export interface FileAppender {
  104. type: 'file';
  105. // the path of the file where you want your logs written.
  106. filename: string;
  107. // the maximum size (in bytes) for the log file. If not specified, then no log rolling will happen.
  108. maxLogSize?: number | string;
  109. // (default value = 5) - the number of old log files to keep during log rolling.
  110. backups?: number;
  111. // defaults to basic layout
  112. layout?: Layout;
  113. numBackups?: number;
  114. compress?: boolean; // compress the backups
  115. // keep the file extension when rotating logs
  116. keepFileExt?: boolean;
  117. encoding?: string;
  118. mode?: number;
  119. flags?: string;
  120. }
  121. export interface SyncfileAppender {
  122. type: 'fileSync';
  123. // the path of the file where you want your logs written.
  124. filename: string;
  125. // the maximum size (in bytes) for the log file. If not specified, then no log rolling will happen.
  126. maxLogSize?: number | string;
  127. // (default value = 5) - the number of old log files to keep during log rolling.
  128. backups?: number;
  129. // defaults to basic layout
  130. layout?: Layout;
  131. }
  132. export interface DateFileAppender {
  133. type: 'dateFile';
  134. // the path of the file where you want your logs written.
  135. filename: string;
  136. // defaults to basic layout
  137. layout?: Layout;
  138. // defaults to .yyyy-MM-dd - the pattern to use to determine when to roll the logs.
  139. /**
  140. * The following strings are recognised in the pattern:
  141. * - yyyy : the full year, use yy for just the last two digits
  142. * - MM : the month
  143. * - dd : the day of the month
  144. * - hh : the hour of the day (24-hour clock)
  145. * - mm : the minute of the hour
  146. * - ss : seconds
  147. * - SSS : milliseconds (although I'm not sure you'd want to roll your logs every millisecond)
  148. * - O : timezone (capital letter o)
  149. */
  150. pattern?: string;
  151. // default “utf-8”
  152. encoding?: string;
  153. // default 0644
  154. mode?: number;
  155. // default ‘a’
  156. flags?: string;
  157. // compress the backup files during rolling (backup files will have .gz extension)(default false)
  158. compress?: boolean;
  159. // include the pattern in the name of the current log file as well as the backups.(default false)
  160. alwaysIncludePattern?: boolean;
  161. // keep the file extension when rotating logs
  162. keepFileExt?: boolean;
  163. // if this value is greater than zero, then files older than that many days will be deleted during log rolling.(default 0)
  164. daysToKeep?: number;
  165. }
  166. export interface LogLevelFilterAppender {
  167. type: 'logLevelFilter';
  168. // the name of an appender, defined in the same configuration, that you want to filter
  169. appender: string;
  170. // the minimum level of event to allow through the filter
  171. level: string;
  172. // (defaults to FATAL) - the maximum level of event to allow through the filter
  173. maxLevel?: string;
  174. }
  175. export interface MultiFileAppender {
  176. type: 'multiFile';
  177. // the base part of the generated log filename
  178. base: string;
  179. // the value to use to split files (see below).
  180. property: string;
  181. // the suffix for the generated log filename.
  182. extension: string;
  183. }
  184. export interface MultiprocessAppender {
  185. type: 'multiprocess';
  186. // controls whether the appender listens for log events sent over the network, or is responsible for serialising events and sending them to a server.
  187. mode: 'master' | 'worker';
  188. // (only needed if mode == master)- the name of the appender to send the log events to
  189. appender?: string;
  190. // (defaults to 5000) - the port to listen on, or send to
  191. loggerPort?: number;
  192. // (defaults to localhost) - the host/IP address to listen on, or send to
  193. loggerHost?: string;
  194. }
  195. export interface RecordingAppender {
  196. type: 'recording';
  197. }
  198. export interface StandardErrorAppender {
  199. type: 'stderr';
  200. // (defaults to colouredLayout)
  201. layout?: Layout;
  202. }
  203. export interface StandardOutputAppender {
  204. type: 'stdout';
  205. // (defaults to colouredLayout)
  206. layout?: Layout;
  207. }
  208. export interface CustomAppender {
  209. type: string | AppenderModule;
  210. [key: string]: any;
  211. }
  212. export interface AppenderModule {
  213. configure: Function
  214. }
  215. export type Appender = CategoryFilterAppender
  216. | ConsoleAppender
  217. | FileAppender
  218. | SyncfileAppender
  219. | DateFileAppender
  220. | LogLevelFilterAppender
  221. | NoLogFilterAppender
  222. | MultiFileAppender
  223. | MultiprocessAppender
  224. | RecordingAppender
  225. | StandardErrorAppender
  226. | StandardOutputAppender
  227. | CustomAppender;
  228. export interface Levels {
  229. ALL: Level;
  230. MARK: Level;
  231. TRACE: Level;
  232. DEBUG: Level;
  233. INFO: Level;
  234. WARN: Level;
  235. ERROR: Level;
  236. FATAL: Level;
  237. OFF: Level;
  238. levels: Level[];
  239. getLevel(level: string): Level;
  240. addLevels(customLevels: object): void;
  241. }
  242. export interface Configuration {
  243. appenders: { [name: string]: Appender; };
  244. categories: { [name: string]: { appenders: string[]; level: string; enableCallStack?: boolean; } };
  245. pm2?: boolean;
  246. pm2InstanceVar?: string;
  247. levels?: Levels;
  248. disableClustering?: boolean;
  249. }
  250. export class Logger {
  251. new(dispatch: Function, name: string): Logger;
  252. readonly category: string;
  253. level: string;
  254. log(...args: any[]): void;
  255. isLevelEnabled(level?: string): boolean;
  256. isTraceEnabled(): boolean;
  257. isDebugEnabled(): boolean;
  258. isInfoEnabled(): boolean;
  259. isWarnEnabled(): boolean;
  260. isErrorEnabled(): boolean;
  261. isFatalEnabled(): boolean;
  262. _log(level: string, data: any): void;
  263. addContext(key: string, value: any): void;
  264. removeContext(key: string): void;
  265. clearContext(): void;
  266. setParseCallStackFunction(parseFunction: Function): void;
  267. trace(message: any, ...args: any[]): void;
  268. debug(message: any, ...args: any[]): void;
  269. info(message: any, ...args: any[]): void;
  270. warn(message: any, ...args: any[]): void;
  271. error(message: any, ...args: any[]): void;
  272. fatal(message: any, ...args: any[]): void;
  273. mark(message: any, ...args: any[]): void;
  274. }