This repository was archived by the owner on Mar 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
213 lines (200 loc) · 6.7 KB
/
Copy pathlogger.ts
File metadata and controls
213 lines (200 loc) · 6.7 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
safeJsonStringify,
getErrorMessage,
isError,
isSafeInteger,
} from "@nerdware/ts-type-safety-utils";
import * as Sentry from "@sentry/node";
import chalk, { type ChalkInstance } from "chalk";
import dayjs from "dayjs";
import { ENV } from "@/server/env";
/* eslint-disable no-console */
const jsonStrSpaces = ENV.IS_PROD ? 0 : 2;
/**
* Returns a log message string.
* - Log message format: `"[<label>] <msgPrefix?> <message>"`
*/
const getLogMessage = ({
label,
input,
msgPrefix,
labelColor,
msgColor,
}: GetLogMessageArgsProvidedByLoggerUtil & GetLogMessageArgsProvidedByHandler): string => {
let formattedLabel = `[${label}]`;
if (labelColor) formattedLabel = labelColor(formattedLabel);
let formattedMsg = msgPrefix ? `${msgPrefix} ` : "";
formattedMsg += getErrorMessage(input) || safeJsonStringify(input, null, jsonStrSpaces);
if (msgColor) formattedMsg = msgColor(formattedMsg);
return `${formattedLabel} ${formattedMsg}`;
};
/**
* Returns a log message string with a timestamp.
* - Log message format: `"[<timestamp>][<label>] <msgPrefix?> <message>"`
* - Timestamp format: `"YYYY:MMM:D H:mm:ss.SSS"`
*/
const getLogMessageWithTimestamp = ({
label,
input,
msgPrefix,
labelColor,
msgColor,
}: GetLogMessageArgsProvidedByLoggerUtil & GetLogMessageArgsProvidedByHandler): string => {
let timestamp = `[${dayjs().format("YYYY:MMM:D H:mm:ss.SSS")}]`;
if (labelColor) timestamp = labelColor(timestamp);
return `${timestamp}${getLogMessage({ label, input, msgPrefix, labelColor, msgColor })}`;
};
/**
* This function returns a logging function suited for the operating environment:
*
* - IN DEPLOYED ENVS (PRODUCTION/STAGING):
* - Error logs are always sent to CloudWatch and Sentry
* - Non-error logs:
* - Sent to CloudWatch and Sentry if `isEnabledInDeployedEnvs` is `true`
* - Ignored if `isEnabledInDeployedEnvs` is `false`
*
* - IN NON-DEPLOYED ENVS:
* - Error logs are always logged using `console.error`
* - Non-error logs are colorized and logged using `consoleMethod`
*
* > Errors are always logged in all environments regardless of
* `isEnabledInDeployedEnvs` which only applies to non-error logs.
*/
const getLoggerUtil = ({
label,
isEnabledInDeployedEnvs = false,
consoleMethod = console.log,
msgColor = chalk.white,
labelColor = msgColor.bold,
}: GetLogMessageArgsProvidedByLoggerUtil & {
/** Bool flag to enable logging non-errors in deployed envs: staging, prod */
isEnabledInDeployedEnvs?: boolean;
/** The `console` method to use (default: `console.log`). */
consoleMethod?:
| typeof console.log
| typeof console.info
| typeof console.debug
| typeof console.warn
| typeof console.error;
}): LoggerFn => {
// `handleLogMessage` and `handleLogError` are env-dependent and govern where/how logs are sent
const {
handleLogMessage,
handleLogError,
}: { handleLogError: ErrorLoggerFn; handleLogMessage: LoggerFn } = ENV.IS_DEPLOYED_ENV
? {
handleLogError: (error, msgPrefix) => {
// If error has a `statusCode` and the `statusCode` is under 500, ignore it.
const statusCode = (error as { statusCode?: number }).statusCode;
if (isSafeInteger(statusCode) && statusCode < 500) return;
Sentry.captureException(error);
// stderr goes to CloudWatch in deployed envs
console.error(getLogMessage({ label, input: error, msgPrefix }));
},
handleLogMessage: isEnabledInDeployedEnvs
? (input, msgPrefix) => {
Sentry.captureMessage(getLogMessageWithTimestamp({ label, input, msgPrefix }));
// stdout goes to CloudWatch in deployed envs
consoleMethod(getLogMessage({ label, input, msgPrefix }));
}
: () => {
/* noop, function is disabled */
},
}
: {
handleLogError: (error, msgPrefix) => {
console.error(
getLogMessageWithTimestamp({ label, input: error, msgPrefix, labelColor, msgColor })
);
},
handleLogMessage: (input, msgPrefix) => {
consoleMethod(
getLogMessageWithTimestamp({ label, input, msgPrefix, labelColor, msgColor })
);
},
};
// The returned fn simply checks if input is an Error, and calls handleLogMessage/handleLogError accordingly
return (input, msgPrefix) => {
if (isError(input)) handleLogError(input, msgPrefix);
else handleLogMessage(input, msgPrefix);
};
};
export const logger = {
server: getLoggerUtil({
label: "SERVER",
msgColor: chalk.magenta,
}),
warn: getLoggerUtil({
label: "WARN",
msgColor: chalk.yellow,
}),
security: getLoggerUtil({
label: "SECURITY",
msgColor: chalk.red.bold,
labelColor: chalk.bgRed.black.bold,
isEnabledInDeployedEnvs: true,
}),
info: getLoggerUtil({
label: "INFO",
msgColor: chalk.cyan,
consoleMethod: console.info,
}),
debug: getLoggerUtil({
label: "DEBUG",
msgColor: chalk.cyan,
consoleMethod: console.debug,
}),
test: getLoggerUtil({
label: "TEST",
msgColor: chalk.cyan,
}),
error: getLoggerUtil({
label: "ERROR",
msgColor: chalk.red,
isEnabledInDeployedEnvs: true,
}),
gql: getLoggerUtil({
label: "GQL",
msgColor: chalk.magenta,
}),
stripe: getLoggerUtil({
label: "STRIPE",
msgColor: chalk.green,
}),
dynamodb: getLoggerUtil({
label: "DynamoDB",
msgColor: chalk.blue,
isEnabledInDeployedEnvs: true,
}),
webhook: getLoggerUtil({
label: "WEBHOOK",
msgColor: chalk.green,
isEnabledInDeployedEnvs: true,
}),
};
/** Args provided to `getLogMessage` by `getLoggerUtil`. */
type GetLogMessageArgsProvidedByLoggerUtil = {
/** A purpose-related label used to differentiate log sources. */
label: string;
/** A [chalk](https://www.npmjs.com/package/chalk) color for dev env log labels. */
labelColor?: ChalkInstance | undefined;
/** A [chalk](https://www.npmjs.com/package/chalk) color for dev env logs (default: white). */
msgColor?: ChalkInstance | undefined;
};
/** Args provided to `getLogMessage` by `LoggerFn` invocations. */
type GetLogMessageArgsProvidedByHandler = {
/** The raw input provided to a logger function. */
input: unknown;
/** An optional string to prefix the stringified log `input`. */
msgPrefix?: string | undefined;
};
/** This type reflects the structure of the function returned by `getLoggerUtil`. */
type LoggerFn = (
input: GetLogMessageArgsProvidedByHandler["input"],
msgPrefix?: GetLogMessageArgsProvidedByHandler["msgPrefix"]
) => void;
/** Internal type for `handleLogError` fns used in `getLoggerUtil`. */
type ErrorLoggerFn = (
error: unknown,
msgPrefix?: GetLogMessageArgsProvidedByHandler["msgPrefix"]
) => void;