Skip to content

Makes "logger/compat" more focused on compatibility. Fixes #697 #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
});
```

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

```js
require('firebase-functions/logger/compat');
```

In older runtimes, logger prints to the console, and no structured data is saved.
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.
36 changes: 7 additions & 29 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import { format } from 'util';

// safely preserve unpatched console.* methods in case of compat require
const unpatchedConsole = {
debug: console.debug,
info: console.info,
log: console.log,
warn: console.warn,
error: console.error,
};

// Determine if structured logs are supported (node >= 10). If something goes wrong,
// assume no since unstructured is safer.
const SUPPORTS_STRUCTURED_LOGS =
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;

// Map LogSeverity types to their equivalent `console.*` method.
const CONSOLE_SEVERITY: {
[severity: string]: 'debug' | 'info' | 'warn' | 'error';
} = {
DEBUG: 'debug',
INFO: 'info',
NOTICE: 'info',
WARNING: 'warn',
ERROR: 'error',
CRITICAL: 'error',
ALERT: 'error',
EMERGENCY: 'error',
};
import {
SUPPORTS_STRUCTURED_LOGS,
CONSOLE_SEVERITY,
UNPATCHED_CONSOLE,
} from './logger/common';

/**
* `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.
Expand Down Expand Up @@ -57,7 +35,7 @@ export interface LogEntry {
*/
export function write(entry: LogEntry) {
if (SUPPORTS_STRUCTURED_LOGS) {
unpatchedConsole[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(entry));
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(entry));
return;
}

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

/**
Expand Down
27 changes: 27 additions & 0 deletions src/logger/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Determine if structured logs are supported (node >= 10). If something goes wrong,
// assume no since unstructured is safer.
export const SUPPORTS_STRUCTURED_LOGS =
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;

// Map LogSeverity types to their equivalent `console.*` method.
export const CONSOLE_SEVERITY: {
[severity: string]: 'debug' | 'info' | 'warn' | 'error';
} = {
DEBUG: 'debug',
INFO: 'info',
NOTICE: 'info',
WARNING: 'warn',
ERROR: 'error',
CRITICAL: 'error',
ALERT: 'error',
EMERGENCY: 'error',
};

// safely preserve unpatched console.* methods in case of compat require
export const UNPATCHED_CONSOLE = {
debug: console.debug,
info: console.info,
log: console.log,
warn: console.warn,
error: console.error,
};
30 changes: 24 additions & 6 deletions src/logger/compat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { debug, info, warn, error } from '../logger';
import {
SUPPORTS_STRUCTURED_LOGS,
UNPATCHED_CONSOLE,
CONSOLE_SEVERITY,
} from './common';
import { format } from 'util';

function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
return function(data: any, ...args: any[]): void {
if (SUPPORTS_STRUCTURED_LOGS) {
UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](
JSON.stringify({ severity, message: format(data, ...args) })
);
return;
}

UNPATCHED_CONSOLE[CONSOLE_SEVERITY[severity]](data, ...args);
};
}

// IMPORTANT -- "../logger" must be imported before monkeypatching!
console.debug = debug;
console.info = info;
console.log = info;
console.warn = warn;
console.error = error;
console.debug = patchedConsole('DEBUG');
console.info = patchedConsole('INFO');
console.log = patchedConsole('INFO');
console.warn = patchedConsole('WARNING');
console.error = patchedConsole('ERROR');