-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
50 lines (39 loc) · 1.47 KB
/
index.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
import { yellow } from 'chalk';
const isBrowser = typeof window === "object"
&& typeof document === 'object'
&& document.nodeType === 9;
declare const require;
let util: { inspect: (input: string, options?: { colors?: boolean; }) => string };
let inBrowserTimezoneWarningShown = false;
let warnedOnError = false;
if (isBrowser) {
util = { inspect: (input, options) => input };
} else {
util = require("util");
}
export const defaults = {
locale: "en-US",
timeZone: "America/Chicago",
}
export function inspect(...args) {
try {
if (isBrowser && !inBrowserTimezoneWarningShown) {
inBrowserTimezoneWarningShown = true;
// Ref: https://stackoverflow.com/q/30377607
console.warn("logspect warning: Timezones are not supported in all browsers. Timestamp will default to the browser's current timezone.");
}
const time = yellow(new Date().toLocaleTimeString(defaults.locale, {
timeZone: isBrowser ? undefined : defaults.timeZone,
timeZoneName: "short"
}));
console.log(time, ...args.map(a => typeof (a) === "string" ? a : util.inspect(a, { colors: true })));
}
catch (e) {
if (!warnedOnError) {
warnedOnError = true;
console.error("Logspect error: could not log arguments. Defaulting to plain console.log.", e);
}
console.log(...args);
}
}
export default inspect;