Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 1bbfb64

Browse files
ofrobotsJustinBeckwith
authored andcommitted
feat: make logpoint function customizable (#634)
By default we use console.log for printing logpoint messages. This PR adds a config option to allow users to customize the log function. This can be useful in situations where the logs are being sent to 3rd parties or need post-processing. Fixes: #458
1 parent 3063973 commit 1bbfb64

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/agent/config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ export interface ResolvedDebugAgentConfig extends GoogleAuthOptions {
290290
* logging resumes per logpoint.
291291
*/
292292
logDelaySeconds: number;
293+
294+
/**
295+
* By default log-points are printed using console.log. If necessary, one
296+
* can provide a custom log point printing function. This may be useful in
297+
* cases where you want to log to 3rd part logging service.
298+
*/
299+
logFunction: (message: string) => void;
293300
};
294301

295302
/**
@@ -347,7 +354,7 @@ export const defaultConfig: ResolvedDebugAgentConfig = {
347354
maxStringLength: 100
348355
},
349356

350-
log: {maxLogsPerSecond: 50, logDelaySeconds: 1},
357+
log: {maxLogsPerSecond: 50, logDelaySeconds: 1, logFunction: console.log},
351358

352359
internal: {
353360
registerDelayOnFetcherErrorSec: 300, // 5 minutes.

src/agent/debuglet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,8 @@ export class Debuglet extends EventEmitter {
830830
.log(
831831
breakpoint,
832832
(fmt: string, exprs: string[]) => {
833-
console.log('LOGPOINT:', Debuglet.format(fmt, exprs));
833+
that.config.log.logFunction(
834+
`LOGPOINT: ${Debuglet.format(fmt, exprs)}`);
834835
},
835836
() => {
836837
// TODO: Address the case when `breakpoint.id` is `undefined`.

system-test/test-e2e.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ const FILENAME = 'build/test/fixtures/fib.js';
2929

3030
const UUID = uuid.v4();
3131
const LOG_MESSAGE_FORMAT = UUID + ': o is: $0';
32+
33+
// fib.js uses a custom log-point function that lower cases everything.
3234
const REGEX =
33-
new RegExp(`LOGPOINT: ${UUID}: o is: {"a":\\[1,"hi",true\\]}`, 'g');
35+
new RegExp(`logpoint: ${UUID}: o is: {"a":\\[1,"hi",true\\]}`, 'g');
3436

3537
const delay = (delayTimeMS: number): Promise<void> => {
3638
return new Promise(r => setTimeout(r, delayTimeMS));
@@ -259,7 +261,7 @@ describe('@google-cloud/debug end-to-end behavior', () => {
259261
console.log('-- checking log point was hit again');
260262
children.forEach((child) => {
261263
const count = (child.transcript.match(REGEX) || []).length;
262-
assert.ok(count > 60, `expected count ${count} to be > 60`);
264+
assert.ok(count > 20, `expected count ${count} to be > 20`);
263265
});
264266
console.log('-- test passed');
265267
}

test/fixtures/fib.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ const UUID = process.argv[2] || uuid.v4();
3333
var debuglet = require('../../..').start({
3434
debug: {
3535
logLevel: 2,
36-
maxLogsPerSecond: 2,
37-
logDelaySeconds: 5,
36+
log: {
37+
maxLogsPerSecond: 2,
38+
logDelaySeconds: 5,
39+
// Use a custom logpoint function that converts everything lower case.
40+
logFunction: (message) => {
41+
console.log(message.toLowerCase())
42+
}
43+
},
3844
breakpointUpdateIntervalSec: 1,
3945
testMode_: true,
4046
allowExpressions: true,

0 commit comments

Comments
 (0)