-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathlog.ts
88 lines (80 loc) · 2.26 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as Sentry from '@sentry/react-native';
// Chalk has both type and class with the same name
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
import type { Chalk } from 'chalk';
/**
* Log system which provides a consistent interface for logging in both production and development,
* exposing the logs as beautiful colorized text in development and as Sentry breadcrumbs in
* production!
*/
// This is a hack to avoid printing undefined when data is not passed
const maybe = (data?: Record<string, unknown> | null) => (data ? [data] : []);
// Load chalk asynchronously to avoid bundling in production
// TODO: make sure chalk is loaded before we log (oops)
export let chalk: Chalk;
if (__DEV__) {
import('chalk').then((c) => (chalk = new c.default.Instance({ level: 3 })));
}
export function log(message: string, data?: Record<string, unknown>) {
if (__DEV__) {
console.log(chalk`{grey.bold [LOG]} ${message}`, ...maybe(data));
} else {
Sentry.addBreadcrumb({
category: 'log',
level: 'log',
message,
data,
});
}
}
export function info(message: string, data?: Record<string, unknown>) {
if (__DEV__) {
console.info(chalk`{blueBright.bold [INFO]} ${message}`, ...maybe(data));
} else {
Sentry.addBreadcrumb({
category: 'log',
level: 'info',
message,
data,
});
}
}
export function debug(message: string, data?: Record<string, unknown>) {
if (__DEV__) {
console.debug(chalk`{green.bold [DEBUG]} ${message}`, ...maybe(data));
} else {
Sentry.addBreadcrumb({
category: 'log',
level: 'debug',
message,
data,
});
}
}
export function warn(message: string, data?: Record<string, unknown>) {
if (__DEV__) {
console.warn(message, ...maybe(data));
} else {
Sentry.addBreadcrumb({
category: 'log',
level: 'warning',
message,
data,
});
Sentry.captureMessage(message, {
level: 'warning',
...data,
});
}
}
export function error(message: unknown, data?: Record<string, unknown>) {
if (__DEV__) {
console.error(message, ...maybe(data));
} else {
if (typeof message === 'string') {
Sentry.captureMessage(message, data);
} else {
Sentry.captureException(message, data);
}
}
}