1 | import { pino } from "pino";
|
2 | import { environment, isProduction } from "./env.js";
|
3 | import { AppError } from "./error.js";
|
4 | import { isNil, isPlainObject, merge } from "./lodash.js";
|
5 | import { loggerWriteGithubActions, loggerWritePretty } from "./log-writers.js";
|
6 | import { _compasSentryExport } from "./sentry.js";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const globalContext = {};
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | let globalDestination = undefined;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | let rootInstance = loggerBuildRootInstance(pino.destination(1));
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export function loggerExtendGlobalContext(context) {
|
47 | if (!isPlainObject(context)) {
|
48 | throw AppError.serverError({
|
49 | message: "First argument should be a plain JS object",
|
50 | });
|
51 | }
|
52 |
|
53 | merge(globalContext, context);
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export function loggerSetGlobalDestination(destination) {
|
69 | globalDestination = destination;
|
70 | rootInstance = loggerBuildRootInstance(destination);
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | export function loggerGetGlobalDestination() {
|
82 |
|
83 | return globalDestination;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export function loggerBuildRootInstance(destination) {
|
93 | return pino(
|
94 | {
|
95 | formatters: {
|
96 | level: (label) => ({ level: label }),
|
97 | bindings: () => ({}),
|
98 | },
|
99 | serializers: {},
|
100 | base: {},
|
101 | },
|
102 | destination,
|
103 | );
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | export function newLogger(options) {
|
116 | const context = options?.ctx
|
117 | ? {
|
118 | ...globalContext,
|
119 | ...options.ctx,
|
120 | }
|
121 | : globalContext;
|
122 |
|
123 | const childLogger = rootInstance.child({
|
124 | context,
|
125 | });
|
126 |
|
127 | if (typeof _compasSentryExport?.addBreadcrumb === "function") {
|
128 | let addedContextAsBreadcrumb = false;
|
129 |
|
130 | return {
|
131 | info: (message) => {
|
132 | if (!addedContextAsBreadcrumb) {
|
133 |
|
134 | _compasSentryExport.addBreadcrumb({
|
135 | category: context.type,
|
136 | data: {
|
137 | ...context,
|
138 | },
|
139 | level: "info",
|
140 | type: "default",
|
141 | });
|
142 | addedContextAsBreadcrumb = true;
|
143 | }
|
144 |
|
145 |
|
146 | _compasSentryExport.addBreadcrumb({
|
147 | category: context.type,
|
148 | data: typeof message === "string" ? undefined : message,
|
149 | message: typeof message === "string" ? message : undefined,
|
150 | level: "info",
|
151 | type: "default",
|
152 | });
|
153 |
|
154 | childLogger.info({ message });
|
155 | },
|
156 | error: (message) => {
|
157 | if (!addedContextAsBreadcrumb) {
|
158 |
|
159 | _compasSentryExport.addBreadcrumb({
|
160 | category: "log",
|
161 | data: {
|
162 | ...context,
|
163 | },
|
164 | level: "info",
|
165 | type: "default",
|
166 | });
|
167 | addedContextAsBreadcrumb = true;
|
168 | }
|
169 |
|
170 |
|
171 | _compasSentryExport.addBreadcrumb({
|
172 | category: "log",
|
173 | data: typeof message === "string" ? undefined : message,
|
174 | message: typeof message === "string" ? message : undefined,
|
175 | level: "error",
|
176 | type: "error",
|
177 | });
|
178 |
|
179 | childLogger.error({ message });
|
180 | },
|
181 | };
|
182 | }
|
183 |
|
184 | return {
|
185 | info: (message) => childLogger.info({ message }),
|
186 | error: (message) => childLogger.error({ message }),
|
187 | };
|
188 | }
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | export function loggerDetermineDefaultDestination() {
|
198 | if (globalDestination) {
|
199 | return;
|
200 | }
|
201 |
|
202 | const isProd = isProduction();
|
203 | const isGitHubActions = environment.GITHUB_ACTIONS === "true";
|
204 | let printer = environment.COMPAS_LOG_PRINTER;
|
205 |
|
206 | if (isNil(printer)) {
|
207 | printer = isGitHubActions ? "github-actions" : isProd ? "ndjson" : "pretty";
|
208 | }
|
209 |
|
210 | if (!["github-actions", "ndjson", "pretty"].includes(printer)) {
|
211 | throw AppError.serverError({
|
212 | message: `process.env.COMPAS_LOG_PRINTER is set to a '${printer}', but only accepts 'ndjson', 'pretty' or 'github-actions'.`,
|
213 | });
|
214 | }
|
215 |
|
216 | if (printer === "ndjson") {
|
217 | return;
|
218 | }
|
219 |
|
220 | loggerSetGlobalDestination(
|
221 | loggerGetPrettyPrinter({
|
222 | addGitHubActionsAnnotations: printer === "github-actions",
|
223 | }),
|
224 | );
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | export function loggerGetPrettyPrinter(options) {
|
235 | function write(line) {
|
236 | const data = JSON.parse(line);
|
237 |
|
238 | if (options.addGitHubActionsAnnotations) {
|
239 | loggerWriteGithubActions(
|
240 | process.stdout,
|
241 | data.level,
|
242 | new Date(data.time),
|
243 | data.context,
|
244 | data.message,
|
245 | );
|
246 | } else {
|
247 | loggerWritePretty(
|
248 | process.stdout,
|
249 | data.level,
|
250 | new Date(data.time),
|
251 | data.context,
|
252 | data.message,
|
253 | );
|
254 | }
|
255 | }
|
256 |
|
257 | return {
|
258 | write,
|
259 | };
|
260 | }
|