Skip to content

Commit 06fb607

Browse files
committed
bugfix-314-error-merging-release-branch-after-successful-deployment: Enhance logging functionality by introducing a sanitizeLogMessage function to remove markdown code fences from log messages. This ensures log output remains clear and unbroken when visualized in environments like GitHub Actions. Updated logInfo, logWarn, logError, and logDebugInfo functions to utilize the new sanitization method. Added tests to verify that markdown code fences are stripped from log messages.
1 parent 4ce66f2 commit 06fb607

File tree

4 files changed

+78
-36
lines changed

4 files changed

+78
-36
lines changed

build/cli/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60455,6 +60455,10 @@ let loggerDebug = false;
6045560455
let loggerRemote = false;
6045660456
let structuredLogging = false;
6045760457
const accumulatedLogEntries = [];
60458+
/** Removes markdown code fences from message so log output does not break when visualized (e.g. GitHub Actions). */
60459+
function sanitizeLogMessage(message) {
60460+
return message.replace(/```/g, '');
60461+
}
6045860462
function pushLogEntry(entry) {
6045960463
accumulatedLogEntries.push(entry);
6046060464
}
@@ -60484,73 +60488,77 @@ function formatStructuredLog(entry) {
6048460488
return JSON.stringify(entry);
6048560489
}
6048660490
function logInfo(message, previousWasSingleLine = false, metadata, skipAccumulation) {
60491+
const sanitized = sanitizeLogMessage(message);
6048760492
if (!skipAccumulation) {
60488-
pushLogEntry({ level: 'info', message, timestamp: Date.now(), metadata });
60493+
pushLogEntry({ level: 'info', message: sanitized, timestamp: Date.now(), metadata });
6048960494
}
6049060495
if (previousWasSingleLine && !loggerRemote) {
6049160496
console.log();
6049260497
}
6049360498
if (structuredLogging) {
6049460499
console.log(formatStructuredLog({
6049560500
level: 'info',
60496-
message,
60501+
message: sanitized,
6049760502
timestamp: Date.now(),
6049860503
metadata
6049960504
}));
6050060505
}
6050160506
else {
60502-
console.log(message);
60507+
console.log(sanitized);
6050360508
}
6050460509
}
6050560510
function logWarn(message, metadata) {
60506-
pushLogEntry({ level: 'warn', message, timestamp: Date.now(), metadata });
60511+
const sanitized = sanitizeLogMessage(message);
60512+
pushLogEntry({ level: 'warn', message: sanitized, timestamp: Date.now(), metadata });
6050760513
if (structuredLogging) {
6050860514
console.warn(formatStructuredLog({
6050960515
level: 'warn',
60510-
message,
60516+
message: sanitized,
6051160517
timestamp: Date.now(),
6051260518
metadata
6051360519
}));
6051460520
}
6051560521
else {
60516-
console.warn(message);
60522+
console.warn(sanitized);
6051760523
}
6051860524
}
6051960525
function logWarning(message) {
6052060526
logWarn(message);
6052160527
}
6052260528
function logError(message, metadata) {
6052360529
const errorMessage = message instanceof Error ? message.message : String(message);
60530+
const sanitized = sanitizeLogMessage(errorMessage);
6052460531
const metaWithStack = {
6052560532
...metadata,
6052660533
stack: message instanceof Error ? message.stack : undefined
6052760534
};
60528-
pushLogEntry({ level: 'error', message: errorMessage, timestamp: Date.now(), metadata: metaWithStack });
60535+
pushLogEntry({ level: 'error', message: sanitized, timestamp: Date.now(), metadata: metaWithStack });
6052960536
if (structuredLogging) {
6053060537
console.error(formatStructuredLog({
6053160538
level: 'error',
60532-
message: errorMessage,
60539+
message: sanitized,
6053360540
timestamp: Date.now(),
6053460541
metadata: metaWithStack
6053560542
}));
6053660543
}
6053760544
else {
60538-
console.error(errorMessage);
60545+
console.error(sanitized);
6053960546
}
6054060547
}
6054160548
function logDebugInfo(message, previousWasSingleLine = false, metadata) {
6054260549
if (loggerDebug) {
60543-
pushLogEntry({ level: 'debug', message, timestamp: Date.now(), metadata });
60550+
const sanitized = sanitizeLogMessage(message);
60551+
pushLogEntry({ level: 'debug', message: sanitized, timestamp: Date.now(), metadata });
6054460552
if (structuredLogging) {
6054560553
console.log(formatStructuredLog({
6054660554
level: 'debug',
60547-
message,
60555+
message: sanitized,
6054860556
timestamp: Date.now(),
6054960557
metadata
6055060558
}));
6055160559
}
6055260560
else {
60553-
logInfo(message, previousWasSingleLine, undefined, true);
60561+
logInfo(sanitized, previousWasSingleLine, undefined, true);
6055460562
}
6055560563
}
6055660564
}

build/github_action/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55771,6 +55771,10 @@ let loggerDebug = false;
5577155771
let loggerRemote = false;
5577255772
let structuredLogging = false;
5577355773
const accumulatedLogEntries = [];
55774+
/** Removes markdown code fences from message so log output does not break when visualized (e.g. GitHub Actions). */
55775+
function sanitizeLogMessage(message) {
55776+
return message.replace(/```/g, '');
55777+
}
5577455778
function pushLogEntry(entry) {
5577555779
accumulatedLogEntries.push(entry);
5577655780
}
@@ -55800,73 +55804,77 @@ function formatStructuredLog(entry) {
5580055804
return JSON.stringify(entry);
5580155805
}
5580255806
function logInfo(message, previousWasSingleLine = false, metadata, skipAccumulation) {
55807+
const sanitized = sanitizeLogMessage(message);
5580355808
if (!skipAccumulation) {
55804-
pushLogEntry({ level: 'info', message, timestamp: Date.now(), metadata });
55809+
pushLogEntry({ level: 'info', message: sanitized, timestamp: Date.now(), metadata });
5580555810
}
5580655811
if (previousWasSingleLine && !loggerRemote) {
5580755812
console.log();
5580855813
}
5580955814
if (structuredLogging) {
5581055815
console.log(formatStructuredLog({
5581155816
level: 'info',
55812-
message,
55817+
message: sanitized,
5581355818
timestamp: Date.now(),
5581455819
metadata
5581555820
}));
5581655821
}
5581755822
else {
55818-
console.log(message);
55823+
console.log(sanitized);
5581955824
}
5582055825
}
5582155826
function logWarn(message, metadata) {
55822-
pushLogEntry({ level: 'warn', message, timestamp: Date.now(), metadata });
55827+
const sanitized = sanitizeLogMessage(message);
55828+
pushLogEntry({ level: 'warn', message: sanitized, timestamp: Date.now(), metadata });
5582355829
if (structuredLogging) {
5582455830
console.warn(formatStructuredLog({
5582555831
level: 'warn',
55826-
message,
55832+
message: sanitized,
5582755833
timestamp: Date.now(),
5582855834
metadata
5582955835
}));
5583055836
}
5583155837
else {
55832-
console.warn(message);
55838+
console.warn(sanitized);
5583355839
}
5583455840
}
5583555841
function logWarning(message) {
5583655842
logWarn(message);
5583755843
}
5583855844
function logError(message, metadata) {
5583955845
const errorMessage = message instanceof Error ? message.message : String(message);
55846+
const sanitized = sanitizeLogMessage(errorMessage);
5584055847
const metaWithStack = {
5584155848
...metadata,
5584255849
stack: message instanceof Error ? message.stack : undefined
5584355850
};
55844-
pushLogEntry({ level: 'error', message: errorMessage, timestamp: Date.now(), metadata: metaWithStack });
55851+
pushLogEntry({ level: 'error', message: sanitized, timestamp: Date.now(), metadata: metaWithStack });
5584555852
if (structuredLogging) {
5584655853
console.error(formatStructuredLog({
5584755854
level: 'error',
55848-
message: errorMessage,
55855+
message: sanitized,
5584955856
timestamp: Date.now(),
5585055857
metadata: metaWithStack
5585155858
}));
5585255859
}
5585355860
else {
55854-
console.error(errorMessage);
55861+
console.error(sanitized);
5585555862
}
5585655863
}
5585755864
function logDebugInfo(message, previousWasSingleLine = false, metadata) {
5585855865
if (loggerDebug) {
55859-
pushLogEntry({ level: 'debug', message, timestamp: Date.now(), metadata });
55866+
const sanitized = sanitizeLogMessage(message);
55867+
pushLogEntry({ level: 'debug', message: sanitized, timestamp: Date.now(), metadata });
5586055868
if (structuredLogging) {
5586155869
console.log(formatStructuredLog({
5586255870
level: 'debug',
55863-
message,
55871+
message: sanitized,
5586455872
timestamp: Date.now(),
5586555873
metadata
5586655874
}));
5586755875
}
5586855876
else {
55869-
logInfo(message, previousWasSingleLine, undefined, true);
55877+
logInfo(sanitized, previousWasSingleLine, undefined, true);
5587055878
}
5587155879
}
5587255880
}

src/utils/__tests__/logger.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ describe('logger', () => {
3737
expect(consoleLogSpy).toHaveBeenCalledWith('hello');
3838
});
3939

40+
it('logInfo strips markdown code fences from message so output does not break when visualized', () => {
41+
logInfo('some ```json\n{"x":1}\n``` content');
42+
expect(consoleLogSpy).toHaveBeenCalledWith('some json\n{"x":1}\n content');
43+
const entries = getAccumulatedLogEntries();
44+
expect(entries[0].message).toBe('some json\n{"x":1}\n content');
45+
});
46+
4047
it('logInfo logs JSON when structured logging is on', () => {
4148
setStructuredLogging(true);
4249
logInfo('hello');
@@ -59,6 +66,11 @@ describe('logger', () => {
5966
expect(consoleWarnSpy).toHaveBeenCalledWith('warn msg');
6067
});
6168

69+
it('logWarn strips markdown code fences from message', () => {
70+
logWarn('error ```code``` here');
71+
expect(consoleWarnSpy).toHaveBeenCalledWith('error code here');
72+
});
73+
6274
it('logWarn logs JSON when structured is on', () => {
6375
setStructuredLogging(true);
6476
logWarn('warn msg', { key: 'value' });
@@ -79,6 +91,11 @@ describe('logger', () => {
7991
expect(consoleErrorSpy).toHaveBeenCalledWith('error string');
8092
});
8193

94+
it('strips markdown code fences from error message', () => {
95+
logError('failed with ```output```');
96+
expect(consoleErrorSpy).toHaveBeenCalledWith('failed with output');
97+
});
98+
8299
it('logs Error message when given Error', () => {
83100
logError(new Error('my error'));
84101
expect(consoleErrorSpy).toHaveBeenCalledWith('my error');

src/utils/logger.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export interface LogEntry {
1111

1212
const accumulatedLogEntries: LogEntry[] = [];
1313

14+
/** Removes markdown code fences from message so log output does not break when visualized (e.g. GitHub Actions). */
15+
function sanitizeLogMessage(message: string): string {
16+
return message.replace(/```/g, '');
17+
}
18+
1419
function pushLogEntry(entry: LogEntry): void {
1520
accumulatedLogEntries.push(entry);
1621
}
@@ -47,8 +52,9 @@ function formatStructuredLog(entry: LogEntry): string {
4752
}
4853

4954
export function logInfo(message: string, previousWasSingleLine: boolean = false, metadata?: Record<string, unknown>, skipAccumulation?: boolean) {
55+
const sanitized = sanitizeLogMessage(message);
5056
if (!skipAccumulation) {
51-
pushLogEntry({ level: 'info', message, timestamp: Date.now(), metadata });
57+
pushLogEntry({ level: 'info', message: sanitized, timestamp: Date.now(), metadata });
5258
}
5359
if (previousWasSingleLine && !loggerRemote) {
5460
console.log()
@@ -57,26 +63,27 @@ export function logInfo(message: string, previousWasSingleLine: boolean = false,
5763
if (structuredLogging) {
5864
console.log(formatStructuredLog({
5965
level: 'info',
60-
message,
66+
message: sanitized,
6167
timestamp: Date.now(),
6268
metadata
6369
}));
6470
} else {
65-
console.log(message);
71+
console.log(sanitized);
6672
}
6773
}
6874

6975
export function logWarn(message: string, metadata?: Record<string, unknown>) {
70-
pushLogEntry({ level: 'warn', message, timestamp: Date.now(), metadata });
76+
const sanitized = sanitizeLogMessage(message);
77+
pushLogEntry({ level: 'warn', message: sanitized, timestamp: Date.now(), metadata });
7178
if (structuredLogging) {
7279
console.warn(formatStructuredLog({
7380
level: 'warn',
74-
message,
81+
message: sanitized,
7582
timestamp: Date.now(),
7683
metadata
7784
}));
7885
} else {
79-
console.warn(message);
86+
console.warn(sanitized);
8087
}
8188
}
8289

@@ -86,35 +93,37 @@ export function logWarning(message: string) {
8693

8794
export function logError(message: unknown, metadata?: Record<string, unknown>) {
8895
const errorMessage = message instanceof Error ? message.message : String(message);
96+
const sanitized = sanitizeLogMessage(errorMessage);
8997
const metaWithStack = {
9098
...metadata,
9199
stack: message instanceof Error ? message.stack : undefined
92100
};
93-
pushLogEntry({ level: 'error', message: errorMessage, timestamp: Date.now(), metadata: metaWithStack });
101+
pushLogEntry({ level: 'error', message: sanitized, timestamp: Date.now(), metadata: metaWithStack });
94102
if (structuredLogging) {
95103
console.error(formatStructuredLog({
96104
level: 'error',
97-
message: errorMessage,
105+
message: sanitized,
98106
timestamp: Date.now(),
99107
metadata: metaWithStack
100108
}));
101109
} else {
102-
console.error(errorMessage);
110+
console.error(sanitized);
103111
}
104112
}
105113

106114
export function logDebugInfo(message: string, previousWasSingleLine: boolean = false, metadata?: Record<string, unknown>) {
107115
if (loggerDebug) {
108-
pushLogEntry({ level: 'debug', message, timestamp: Date.now(), metadata });
116+
const sanitized = sanitizeLogMessage(message);
117+
pushLogEntry({ level: 'debug', message: sanitized, timestamp: Date.now(), metadata });
109118
if (structuredLogging) {
110119
console.log(formatStructuredLog({
111120
level: 'debug',
112-
message,
121+
message: sanitized,
113122
timestamp: Date.now(),
114123
metadata
115124
}));
116125
} else {
117-
logInfo(message, previousWasSingleLine, undefined, true);
126+
logInfo(sanitized, previousWasSingleLine, undefined, true);
118127
}
119128
}
120129
}

0 commit comments

Comments
 (0)