-
Notifications
You must be signed in to change notification settings - Fork 950
/
logger.ts
69 lines (65 loc) · 1.83 KB
/
logger.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
67
68
69
/**
* A logger backed by [bunyan](https://github.com/trentm/node-bunyan)
*
* The default log level is `info`, but you can change it by setting the
* `LOG_LEVEL` environment variable to `trace`, `debug`, `info`, `warn`,
* `error`, or `fatal`.
*
* By default, logs are formatted for readability in development. If you intend
* to drain logs to a logging service, set `LOG_FORMAT=json`.
*
* **Note**: All exceptions reported with `logger.error` will be forwarded to
* [sentry](https://github.com/getsentry/sentry) if the `SENTRY_DSN` environment
* variable is set.
*
* ```js
* app.log("This is an info message");
* 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 Logger from 'bunyan'
import bunyanFormat from 'bunyan-format'
import supportsColor from 'supports-color'
import { serializers } from './serializers'
function toBunyanLogLevel (level: string) {
switch (level) {
case 'info':
case 'trace':
case 'debug':
case 'warn':
case 'error':
case 'fatal':
case undefined:
return level
default:
throw new Error('Invalid log level')
}
}
function toBunyanFormat (format: string) {
switch (format) {
case 'short':
case 'long':
case 'simple':
case 'json':
case 'bunyan':
case undefined:
return format
default:
throw new Error('Invalid log format')
}
}
export const logger = new Logger({
level: toBunyanLogLevel(process.env.LOG_LEVEL || 'info'),
name: 'probot',
serializers,
stream: new bunyanFormat({
color: supportsColor.stdout,
levelInString: !!process.env.LOG_LEVEL_IN_STRING,
outputMode: toBunyanFormat(process.env.LOG_FORMAT || 'short')
})
})