Skip to content

Commit c26ed9e

Browse files
authored
Makes "logger/compat" more focused on compatibility. Fixes #697 (#701)
1 parent e447949 commit c26ed9e

File tree

4 files changed

+60
-37
lines changed

4 files changed

+60
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
});
1010
```
1111

12-
The logger can also override default behavior of `console.*` methods through a special require:
12+
- Adds a special require that mimics Node.js 8 runtime logging in Node.js 10 and later runtimes:
1313

1414
```js
1515
require('firebase-functions/logger/compat');
1616
```
1717

18-
In older runtimes, logger prints to the console, and no structured data is saved.
18+
In newer runtimes, requiring this will emit text logs with multi-line support and appropriate severity. In the Node.js 8 runtime, the `compat` module has no effect.

src/logger.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,10 @@
11
import { format } from 'util';
22

3-
// safely preserve unpatched console.* methods in case of compat require
4-
const unpatchedConsole = {
5-
debug: console.debug,
6-
info: console.info,
7-
log: console.log,
8-
warn: console.warn,
9-
error: console.error,
10-
};
11-
12-
// Determine if structured logs are supported (node >= 10). If something goes wrong,
13-
// assume no since unstructured is safer.
14-
const SUPPORTS_STRUCTURED_LOGS =
15-
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;
16-
17-
// Map LogSeverity types to their equivalent `console.*` method.
18-
const CONSOLE_SEVERITY: {
19-
[severity: string]: 'debug' | 'info' | 'warn' | 'error';
20-
} = {
21-
DEBUG: 'debug',
22-
INFO: 'info',
23-
NOTICE: 'info',
24-
WARNING: 'warn',
25-
ERROR: 'error',
26-
CRITICAL: 'error',
27-
ALERT: 'error',
28-
EMERGENCY: 'error',
29-
};
3+
import {
4+
SUPPORTS_STRUCTURED_LOGS,
5+
CONSOLE_SEVERITY,
6+
UNPATCHED_CONSOLE,
7+
} from './logger/common';
308

319
/**
3210
* `LogSeverity` indicates the detailed severity of the log entry. See [LogSeverity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity) for more.
@@ -57,7 +35,7 @@ export interface LogEntry {
5735
*/
5836
export function write(entry: LogEntry) {
5937
if (SUPPORTS_STRUCTURED_LOGS) {
60-
unpatchedConsole[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(entry));
38+
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(entry));
6139
return;
6240
}
6341

@@ -73,7 +51,7 @@ export function write(entry: LogEntry) {
7351
if (jsonKeyCount > 0) {
7452
message = `${message} ${JSON.stringify(jsonPayload, null, 2)}`;
7553
}
76-
unpatchedConsole[CONSOLE_SEVERITY[entry.severity]](message);
54+
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](message);
7755
}
7856

7957
/**

src/logger/common.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Determine if structured logs are supported (node >= 10). If something goes wrong,
2+
// assume no since unstructured is safer.
3+
export const SUPPORTS_STRUCTURED_LOGS =
4+
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;
5+
6+
// Map LogSeverity types to their equivalent `console.*` method.
7+
export const CONSOLE_SEVERITY: {
8+
[severity: string]: 'debug' | 'info' | 'warn' | 'error';
9+
} = {
10+
DEBUG: 'debug',
11+
INFO: 'info',
12+
NOTICE: 'info',
13+
WARNING: 'warn',
14+
ERROR: 'error',
15+
CRITICAL: 'error',
16+
ALERT: 'error',
17+
EMERGENCY: 'error',
18+
};
19+
20+
// safely preserve unpatched console.* methods in case of compat require
21+
export const UNPATCHED_CONSOLE = {
22+
debug: console.debug,
23+
info: console.info,
24+
log: console.log,
25+
warn: console.warn,
26+
error: console.error,
27+
};

src/logger/compat.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1-
import { debug, info, warn, error } from '../logger';
1+
import {
2+
SUPPORTS_STRUCTURED_LOGS,
3+
UNPATCHED_CONSOLE,
4+
CONSOLE_SEVERITY,
5+
} from './common';
6+
import { format } from 'util';
7+
8+
function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
9+
return function(data: any, ...args: any[]): void {
10+
if (SUPPORTS_STRUCTURED_LOGS) {
11+
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
12+
JSON.stringify({ severity, message: format(data, ...args) })
13+
);
14+
return;
15+
}
16+
17+
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](data, ...args);
18+
};
19+
}
220

321
// IMPORTANT -- "../logger" must be imported before monkeypatching!
4-
console.debug = debug;
5-
console.info = info;
6-
console.log = info;
7-
console.warn = warn;
8-
console.error = error;
22+
console.debug = patchedConsole('DEBUG');
23+
console.info = patchedConsole('INFO');
24+
console.log = patchedConsole('INFO');
25+
console.warn = patchedConsole('WARNING');
26+
console.error = patchedConsole('ERROR');

0 commit comments

Comments
 (0)