Skip to content

Commit 5f63d08

Browse files
committed
Make the logger forward logs to DD
1 parent 2a27847 commit 5f63d08

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

packages/core/src/helpers/log.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright 2019-Present Datadog, Inc.
44

55
import { HOST_NAME } from '../constants';
6-
import type { GlobalData, LogOptions } from '../types';
6+
import type { GlobalData, DdLogOptions } from '../types';
77

88
import { doRequest } from './request';
99

@@ -12,7 +12,7 @@ export const INTAKE_HOST = 'browser-http-intake.logs.datadoghq.com';
1212

1313
export const getSendLog =
1414
(data: GlobalData) =>
15-
({ message, context }: LogOptions): Promise<void> => {
15+
({ message, context }: DdLogOptions): Promise<void> => {
1616
return doRequest({
1717
// Don't delay the build too much on error.
1818
retries: 2,

packages/core/src/types.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,19 @@ export type TimeLog = (
138138
opts?: { level?: LogLevel; start?: boolean | number; log?: boolean; tags?: LogTags },
139139
) => TimeLogger;
140140
export type GetLogger = (name: string) => Logger;
141+
export type LogOptions = { forward?: boolean };
142+
export type LoggerFn = (text: any, opts?: LogOptions) => void;
141143
export type Logger = {
142144
getLogger: GetLogger;
143145
time: TimeLog;
144-
error: (text: any) => void;
145-
warn: (text: any) => void;
146-
info: (text: any) => void;
147-
debug: (text: any) => void;
146+
error: LoggerFn;
147+
warn: LoggerFn;
148+
info: LoggerFn;
149+
debug: LoggerFn;
148150
};
149151
type RestContext = string | string[] | number | boolean;
150152
export type LogData = Record<string, RestContext | Record<string, RestContext>>;
151-
export type LogOptions = {
153+
export type DdLogOptions = {
152154
message: string;
153155
context?: LogData;
154156
};
@@ -171,7 +173,7 @@ export type GlobalContext = {
171173
pluginNames: string[];
172174
plugins: (PluginOptions | CustomPluginOptions)[];
173175
queue: (promise: Promise<any>) => void;
174-
sendLog: (args: LogOptions) => Promise<void>;
176+
sendLog: (args: DdLogOptions) => Promise<void>;
175177
start: number;
176178
version: GlobalData['version'];
177179
};

packages/factory/src/helpers/logger.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5+
import { getSendLog } from '@dd/core/helpers/log';
56
import { cleanPluginName } from '@dd/core/helpers/plugins';
67
import { formatDuration } from '@dd/core/helpers/strings';
78
import type {
@@ -41,7 +42,7 @@ export const getLogFn = (
4142
): LogFn => {
4243
// Will remove any "datadog-" prefix and "-plugin" suffix in the name string.
4344
const cleanedName = cleanName(name);
44-
return (text: any, type: LogLevel = 'debug') => {
45+
return (text, type = 'debug', { forward } = {}) => {
4546
// By default (debug) we print dimmed.
4647
let color = c.dim;
4748
let logFn = console.log;
@@ -77,6 +78,12 @@ export const getLogFn = (
7778
stores.warnings.push(content);
7879
}
7980

81+
if (forward) {
82+
stores.queue.push(
83+
getSendLog(data)({ message: content, context: { plugin: name, status: type } }),
84+
);
85+
}
86+
8087
// Only log if the log level is high enough.
8188
if (logPriority[type] >= logPriority[logLevel]) {
8289
logFn(`${color(prefix)} ${content}`);
@@ -197,9 +204,9 @@ export const getLoggerFactory =
197204
return logger(`${cleanName(name)}${NAME_SEP}${subName}`);
198205
},
199206
time: getTimeLogger(name, stores.timings, log),
200-
error: (text: any) => log(text, 'error'),
201-
warn: (text: any) => log(text, 'warn'),
202-
info: (text: any) => log(text, 'info'),
203-
debug: (text: any) => log(text, 'debug'),
207+
error: (text: any, opts?: LogOptions) => log(text, 'error', opts),
208+
warn: (text: any, opts?: LogOptions) => log(text, 'warn', opts),
209+
info: (text: any, opts?: LogOptions) => log(text, 'info', opts),
210+
debug: (text: any, opts?: LogOptions) => log(text, 'debug', opts),
204211
};
205212
};

0 commit comments

Comments
 (0)