UNPKG

8.58 kBTypeScriptView Raw
1// Type definitions for bunyan 1.8
2// Project: https://github.com/trentm/node-bunyan
3// Definitions by: Alex Mikhalev <https://github.com/amikhalev>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.1
6
7/// <reference types="node" />
8
9import { EventEmitter } from 'events';
10
11declare class Logger extends EventEmitter {
12 constructor(options: Logger.LoggerOptions);
13 addStream(stream: Logger.Stream): void;
14 addSerializers(serializers: Logger.Serializers): void;
15 child(options: Object, simple?: boolean): Logger;
16 reopenFileStreams(): void;
17
18 level(): number;
19 level(value: Logger.LogLevel): void;
20 levels(): number[];
21 levels(name: number | string): number;
22 levels(name: number | string, value: Logger.LogLevel): void;
23
24 fields: any;
25 src: boolean;
26
27 /**
28 * Returns a boolean: is the `trace` level enabled?
29 *
30 * This is equivalent to `log.isTraceEnabled()` or `log.isEnabledFor(TRACE)` in log4j.
31 */
32 trace(): boolean;
33
34 /**
35 * Special case to log an `Error` instance to the record.
36 * This adds an `err` field with exception details
37 * (including the stack) and sets `msg` to the exception
38 * message or you can specify the `msg`.
39 */
40 trace(error: Error, ...params: any[]): void;
41
42 /**
43 * The first field can optionally be a "fields" object, which
44 * is merged into the log record.
45 *
46 * To pass in an Error *and* other fields, use the `err`
47 * field name for the Error instance.
48 */
49 trace(obj: Object, ...params: any[]): void;
50
51 /**
52 * Uses `util.format` for msg formatting.
53 */
54 trace(format: any, ...params: any[]): void;
55
56 /**
57 * Returns a boolean: is the `debug` level enabled?
58 *
59 * This is equivalent to `log.isDebugEnabled()` or `log.isEnabledFor(DEBUG)` in log4j.
60 */
61 debug(): boolean;
62
63 /**
64 * Special case to log an `Error` instance to the record.
65 * This adds an `err` field with exception details
66 * (including the stack) and sets `msg` to the exception
67 * message or you can specify the `msg`.
68 */
69 debug(error: Error, ...params: any[]): void;
70
71 /**
72 * The first field can optionally be a "fields" object, which
73 * is merged into the log record.
74 *
75 * To pass in an Error *and* other fields, use the `err`
76 * field name for the Error instance.
77 */
78 debug(obj: Object, ...params: any[]): void;
79
80 /**
81 * Uses `util.format` for msg formatting.
82 */
83 debug(format: any, ...params: any[]): void;
84
85 /**
86 * Returns a boolean: is the `info` level enabled?
87 *
88 * This is equivalent to `log.isInfoEnabled()` or `log.isEnabledFor(INFO)` in log4j.
89 */
90 info(): boolean;
91
92 /**
93 * Special case to log an `Error` instance to the record.
94 * This adds an `err` field with exception details
95 * (including the stack) and sets `msg` to the exception
96 * message or you can specify the `msg`.
97 */
98 info(error: Error, ...params: any[]): void;
99
100 /**
101 * The first field can optionally be a "fields" object, which
102 * is merged into the log record.
103 *
104 * To pass in an Error *and* other fields, use the `err`
105 * field name for the Error instance.
106 */
107 info(obj: Object, ...params: any[]): void;
108
109 /**
110 * Uses `util.format` for msg formatting.
111 */
112 info(format: any, ...params: any[]): void;
113
114 /**
115 * Returns a boolean: is the `warn` level enabled?
116 *
117 * This is equivalent to `log.isWarnEnabled()` or `log.isEnabledFor(WARN)` in log4j.
118 */
119 warn(): boolean;
120
121 /**
122 * Special case to log an `Error` instance to the record.
123 * This adds an `err` field with exception details
124 * (including the stack) and sets `msg` to the exception
125 * message or you can specify the `msg`.
126 */
127 warn(error: Error, ...params: any[]): void;
128
129 /**
130 * The first field can optionally be a "fields" object, which
131 * is merged into the log record.
132 *
133 * To pass in an Error *and* other fields, use the `err`
134 * field name for the Error instance.
135 */
136 warn(obj: Object, ...params: any[]): void;
137
138 /**
139 * Uses `util.format` for msg formatting.
140 */
141 warn(format: any, ...params: any[]): void;
142
143 /**
144 * Returns a boolean: is the `error` level enabled?
145 *
146 * This is equivalent to `log.isErrorEnabled()` or `log.isEnabledFor(ERROR)` in log4j.
147 */
148 error(): boolean;
149
150 /**
151 * Special case to log an `Error` instance to the record.
152 * This adds an `err` field with exception details
153 * (including the stack) and sets `msg` to the exception
154 * message or you can specify the `msg`.
155 */
156 error(error: Error, ...params: any[]): void;
157
158 /**
159 * The first field can optionally be a "fields" object, which
160 * is merged into the log record.
161 *
162 * To pass in an Error *and* other fields, use the `err`
163 * field name for the Error instance.
164 */
165 error(obj: Object, ...params: any[]): void;
166
167 /**
168 * Uses `util.format` for msg formatting.
169 */
170 error(format: any, ...params: any[]): void;
171
172 /**
173 * Returns a boolean: is the `fatal` level enabled?
174 *
175 * This is equivalent to `log.isFatalEnabled()` or `log.isEnabledFor(FATAL)` in log4j.
176 */
177 fatal(): boolean;
178
179 /**
180 * Special case to log an `Error` instance to the record.
181 * This adds an `err` field with exception details
182 * (including the stack) and sets `msg` to the exception
183 * message or you can specify the `msg`.
184 */
185 fatal(error: Error, ...params: any[]): void;
186
187 /**
188 * The first field can optionally be a "fields" object, which
189 * is merged into the log record.
190 *
191 * To pass in an Error *and* other fields, use the `err`
192 * field name for the Error instance.
193 */
194 fatal(obj: Object, ...params: any[]): void;
195
196 /**
197 * Uses `util.format` for msg formatting.
198 */
199 fatal(format: any, ...params: any[]): void;
200}
201
202declare namespace Logger {
203 const TRACE: number;
204 const DEBUG: number;
205 const INFO: number;
206 const WARN: number;
207 const ERROR: number;
208 const FATAL: number;
209
210 type LogLevelString = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
211 type LogLevel = LogLevelString | number;
212
213 const levelFromName: { [name in LogLevelString]: number };
214 const nameFromLevel: { [level: number]: string };
215
216 const stdSerializers: StdSerializers;
217
218 function createLogger(options: LoggerOptions): Logger;
219
220 function safeCycles(): (key: string, value: any) => any;
221
222 function resolveLevel(value: LogLevel): number;
223
224 interface Stream {
225 type?: string | undefined;
226 level?: LogLevel | undefined;
227 path?: string | undefined;
228 stream?: NodeJS.WritableStream | Stream | undefined;
229 closeOnExit?: boolean | undefined;
230 period?: string | undefined;
231 count?: number | undefined;
232 name?: string | undefined;
233 reemitErrorEvents?: boolean | undefined;
234 }
235
236 interface LoggerOptions {
237 name: string;
238 streams?: Stream[] | undefined;
239 level?: LogLevel | undefined;
240 stream?: NodeJS.WritableStream | undefined;
241 serializers?: Serializers | undefined;
242 src?: boolean | undefined;
243 [custom: string]: any;
244 }
245
246 type Serializer = (input: any) => any;
247
248 interface Serializers {
249 [key: string]: Serializer;
250 }
251
252 interface StdSerializers extends Serializers {
253 err: Serializer;
254 res: Serializer;
255 req: Serializer;
256 }
257
258 interface RingBufferOptions {
259 limit?: number | undefined;
260 }
261
262 class RingBuffer extends EventEmitter {
263 constructor(options: RingBufferOptions);
264
265 writable: boolean;
266 records: any[];
267
268 write(record: any): boolean;
269 end(record?: any): void;
270 destroy(): void;
271 destroySoon(): void;
272 }
273
274 interface RotatingFileStreamOptions {
275 path: string;
276 count?: number | undefined;
277 period?: string | undefined;
278 }
279
280 class RotatingFileStream extends EventEmitter {
281 constructor(options: RotatingFileStreamOptions);
282
283 writable: boolean;
284 periodNum: number;
285 periodScope: string;
286 stream: any;
287 rotQueue: any[];
288 rotating: boolean;
289
290 write(record: any): boolean;
291 end(record?: any): void;
292 destroy(): void;
293 destroySoon(): void;
294 rotate(): void;
295 }
296}
297
298export = Logger;