UNPKG

8.65 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 WriteFn {
225 write: (object: Object) => void;
226 }
227
228 interface Stream {
229 type?: string | undefined;
230 level?: LogLevel | undefined;
231 path?: string | undefined;
232 stream?: NodeJS.WritableStream | WriteFn | undefined;
233 closeOnExit?: boolean | undefined;
234 period?: string | undefined;
235 count?: number | undefined;
236 name?: string | undefined;
237 reemitErrorEvents?: boolean | undefined;
238 }
239
240 interface LoggerOptions {
241 name: string;
242 streams?: Stream[] | undefined;
243 level?: LogLevel | undefined;
244 stream?: NodeJS.WritableStream | undefined;
245 serializers?: Serializers | undefined;
246 src?: boolean | undefined;
247 [custom: string]: any;
248 }
249
250 type Serializer = (input: any) => any;
251
252 interface Serializers {
253 [key: string]: Serializer;
254 }
255
256 interface StdSerializers extends Serializers {
257 err: Serializer;
258 res: Serializer;
259 req: Serializer;
260 }
261
262 interface RingBufferOptions {
263 limit?: number | undefined;
264 }
265
266 class RingBuffer extends EventEmitter {
267 constructor(options: RingBufferOptions);
268
269 writable: boolean;
270 records: any[];
271
272 write(record: any): boolean;
273 end(record?: any): void;
274 destroy(): void;
275 destroySoon(): void;
276 }
277
278 interface RotatingFileStreamOptions {
279 path: string;
280 count?: number | undefined;
281 period?: string | undefined;
282 }
283
284 class RotatingFileStream extends EventEmitter {
285 constructor(options: RotatingFileStreamOptions);
286
287 writable: boolean;
288 periodNum: number;
289 periodScope: string;
290 stream: any;
291 rotQueue: any[];
292 rotating: boolean;
293
294 write(record: any): boolean;
295 end(record?: any): void;
296 destroy(): void;
297 destroySoon(): void;
298 rotate(): void;
299 }
300}
301
302export = Logger;