/
get-log.ts
66 lines (57 loc) · 2.09 KB
/
get-log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* A logger backed by [pino](https://getpino.io/)
*
* The default log level is `info`, but you can change it passing a level
* string set to one of: `"trace"`, `"debug"`, `"info"`, `"warn"`,
* `"error"`, or `"fatal"`.
*
* ```js
* app.log.debug("…so is this");
* app.log.trace("Now we're talking");
* app.log.info("I thought you should know…");
* app.log.warn("Woah there");
* app.log.error("ETOOMANYLOGS");
* app.log.fatal("Goodbye, cruel world!");
* ```
*/
import pino, { LoggerOptions } from "pino";
import { getTransformStream, Options, LogLevel } from "@probot/pino";
type GetLogOptions = { level?: LogLevel } & Options;
export function getLog(options: GetLogOptions = {}) {
const deprecated = [];
if (process.env.LOG_FORMAT && !options.logFormat) {
deprecated.push('"LOG_FORMAT"');
options.logFormat = process.env.LOG_FORMAT as Options["logFormat"];
}
if (process.env.LOG_LEVEL_IN_STRING && !options.logLevelInString) {
deprecated.push('"LOG_LEVEL_IN_STRING"');
options.logLevelInString = process.env.LOG_LEVEL_IN_STRING === "true";
}
if (process.env.SENTRY_DSN && !options.sentryDsn) {
deprecated.push('"SENTRY_DSN"');
options.sentryDsn = process.env.SENTRY_DSN;
}
const { level, ...getTransformStreamOptions } = options;
const pinoOptions: LoggerOptions = { level: level || "info", name: "probot" };
const transform = getTransformStream(getTransformStreamOptions);
transform.pipe(pino.destination(1));
const log = pino(pinoOptions, transform);
if (deprecated.length) {
log.warn(`[probot] Using the following environment variable(s) with the Probot constructor is deprecated: ${deprecated.join(
", "
)}. Pass a custom log instance instead:
import { Probot } from "probot";
import pino from "pino";
import { getTransformStream } from "@probot/pino";
const transform = getTransformStream({
level: "info",
logFormat: "pretty",
logLevelInString: false,
sentryDsn: "https://1234abcd@sentry.io/12345
});
transform.pipe(pino.destination(1));
const log = pino(pinoOptions, transform);
new Probot({ log });`);
}
return log;
}