1 | // TypeScript Version: 2.6
|
2 |
|
3 | export namespace Tracer {
|
4 | export interface LogOutput {
|
5 | /**
|
6 | * Current time.
|
7 | */
|
8 | timestamp: string;
|
9 | /**
|
10 | * The result of formating the template and arguments in `args`.
|
11 | */
|
12 | message: string;
|
13 | /**
|
14 | * Method name, default is `log`, `trace`, `debug`, `info`, `warn`, `error`, `fatal`.
|
15 | */
|
16 | title: string;
|
17 | /**
|
18 | * Method level, default is `log`: 0, `trace`: 1, `debug`: 2, `info`: 3, `warn`: 4, `error`: 5, `fatal`: 6.
|
19 | */
|
20 | level: number;
|
21 | /**
|
22 | * Arguments of `Logger` method.
|
23 | */
|
24 | args: any[];
|
25 | /**
|
26 | * Method name of caller.
|
27 | */
|
28 | method: string;
|
29 | /**
|
30 | * File's path.
|
31 | */
|
32 | path: string;
|
33 | /**
|
34 | * Line number.
|
35 | */
|
36 | line: string;
|
37 | /**
|
38 | * Position.
|
39 | */
|
40 | pos: string;
|
41 | /**
|
42 | * folder path.
|
43 | */
|
44 | folder: string;
|
45 | /**
|
46 | * File's name.
|
47 | */
|
48 | file: string;
|
49 | /**
|
50 | * Call stack message.
|
51 | */
|
52 | stack: string;
|
53 | /**
|
54 | * The output to be written
|
55 | */
|
56 | output: string;
|
57 |
|
58 | /**
|
59 | * Any custom keys
|
60 | */
|
61 | [key: string]: any;
|
62 | }
|
63 |
|
64 | export interface LevelOption<T> {
|
65 | log?: T;
|
66 | trace?: T;
|
67 | debug?: T;
|
68 | info?: T;
|
69 | warn?: T;
|
70 | error?: T;
|
71 | fatal?: T;
|
72 | }
|
73 | export type FilterFunction = (data: string) => string | boolean | null | void;
|
74 | export type TransportFunction = (data: LogOutput) => void;
|
75 |
|
76 | export interface LoggerConfig {
|
77 | /**
|
78 | * Output format (Using `tinytim` templating)
|
79 | *
|
80 | * Defaults to: `"{{timestamp}} <{{title}}> {{file}}:{{line}} ({{method}}) {{message}}"`
|
81 | *
|
82 | * Possible values:
|
83 | * - timestamp: current time
|
84 | * - title: method name, default is 'log', 'trace', 'debug', 'info', 'warn', 'error','fatal'
|
85 | * - level: method level, default is 'log':0, 'trace':1, 'debug':2, 'info':3, 'warn':4, 'error':5, 'fatal':6
|
86 | * - message: printf message, support %s string, %d number, %j JSON and auto inspect
|
87 | * - file: file name
|
88 | * - line: line number
|
89 | * - pos: position
|
90 | * - path: file's path
|
91 | * - method: method name of caller
|
92 | * - stack: call stack message
|
93 | */
|
94 | format?: string | [string, LevelOption<string>];
|
95 | /**
|
96 | * rootDir of folder path.
|
97 | */
|
98 | rootDir?: string;
|
99 | /**
|
100 | * Datetime format (Using `Date Format`)
|
101 | */
|
102 | dateformat?: string;
|
103 | filters?: FilterFunction[] | LevelOption<FilterFunction | FilterFunction[]> | Array<FilterFunction | LevelOption<FilterFunction | FilterFunction[]>>;
|
104 | /**
|
105 | * Output the log, if level of log larger than or equal to `level`.
|
106 | */
|
107 | level?: string | number;
|
108 | methods?: string[];
|
109 | /**
|
110 | * Get the specified index of stack as file information. It is useful for development package.
|
111 | */
|
112 | stackIndex?: number;
|
113 | inspectOpt?: {
|
114 | /**
|
115 | * If true then the object's non-enumerable properties will be shown too. Defaults to false.
|
116 | */
|
117 | showHidden: boolean,
|
118 | /**
|
119 | * Tells inspect how many times to recurse while formatting the object.
|
120 | * This is useful for inspecting large complicated objects.
|
121 | * Defaults to 2. To make it recurse indefinitely pass null.
|
122 | */
|
123 | depth: number
|
124 | };
|
125 |
|
126 | /**
|
127 | * Pre-process the log object.
|
128 | */
|
129 | preprocess?(data: LogOutput): void;
|
130 | /**
|
131 | * Transport function (e.g. console.log)
|
132 | */
|
133 | transport?: TransportFunction | TransportFunction[];
|
134 | }
|
135 | export interface DailyFileConfig {
|
136 | /**
|
137 | * All daily log file's dir, default to: `'.'`.
|
138 | */
|
139 | root?: string;
|
140 | /**
|
141 | * Log file path format.
|
142 | *
|
143 | * Default to: `'{{root}}/{{prefix}}.{{date}}.log'`
|
144 | *
|
145 | * Possible values:
|
146 | * - `root`: all daily log file's dir, default to: `'.'`.
|
147 | * - `prefix`: it equal to `allLogsFileName`, if `allLogsFileName` is provided; else it will be the method name.
|
148 | * - `date`: today's date.
|
149 | */
|
150 | logPathFormat?: string;
|
151 | /**
|
152 | * Datetime format (Using `Date Format`)
|
153 | */
|
154 | splitFormat?: string;
|
155 | /**
|
156 | * If `allLogsFileName` is provided then all level logs will be move to one daily log file.
|
157 | */
|
158 | allLogsFileName?: boolean | string;
|
159 | maxLogFiles?: number;
|
160 | }
|
161 |
|
162 | export interface Logger {
|
163 | [method: string]: (...args: any[]) => LogOutput;
|
164 | log(...args: any[]): LogOutput;
|
165 | trace(...args: any[]): LogOutput;
|
166 | debug(...args: any[]): LogOutput;
|
167 | info(...args: any[]): LogOutput;
|
168 | warn(...args: any[]): LogOutput;
|
169 | error(...args: any[]): LogOutput;
|
170 | fatal(...args: any[]): LogOutput;
|
171 | }
|
172 | }
|
173 |
|
174 | /**
|
175 | * Create a console for printing color log.
|
176 | * @param [config] Configurate how logs are printed.
|
177 | */
|
178 | export function colorConsole(config?: Tracer.LoggerConfig): Tracer.Logger;
|
179 | /**
|
180 | * Create a console without color.
|
181 | * @param [config] Configurate how logs are printed.
|
182 | */
|
183 | export function console(config?: Tracer.LoggerConfig): Tracer.Logger;
|
184 | /**
|
185 | * DailyLog will output all types log to diff files every day like log4j.
|
186 | * @param config Configurate how logs are printed & how log files are saved.
|
187 | */
|
188 | export function dailyfile(config?: Tracer.LoggerConfig & Tracer.DailyFileConfig): Tracer.Logger;
|
189 |
|
190 | /**
|
191 | * End all the output.
|
192 | *
|
193 | * Equivalent to: `tracer.setLevel(Number.MAX_VALUE)`.
|
194 | */
|
195 | export function close(): void;
|
196 | /**
|
197 | * Change the log level in run time, for all the output.
|
198 | *
|
199 | * Notice: If you set level in initialize, you can't change more lower level than the initial level.
|
200 | * @param level Output the log, if level of log larger than or equal to `level`.
|
201 | */
|
202 | export function setLevel(level: number | string): void;
|
203 | /**
|
204 | * Get the current log level.
|
205 | */
|
206 | export function getLevel(): number | string;
|