Skip to content
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
6 changes: 6 additions & 0 deletions docgen/content-sources/toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ toc:
- title: 'CallableContext'
path: /docs/reference/functions/providers_https_.callablecontext.html

- title: 'functions.logger'
path: /docs/reference/functions/logger_.html
section:
- title: 'LogEntry'
path: /docs/reference/functions/logger_.logentry.html

- title: 'functions.pubsub'
path: /docs/reference/functions/providers_pubsub_.html
section:
Expand Down
18 changes: 10 additions & 8 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} 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.
* `LogSeverity` indicates the detailed severity of the log entry. See [LogSeverity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity).
*/
export type LogSeverity =
| 'DEBUG'
Expand All @@ -20,7 +20,8 @@ export type LogSeverity =
| 'EMERGENCY';

/**
* `LogEntry` represents a structured Cloud Logging entry. All keys aside from `severity` and `message` are
* `LogEntry` represents a [structured Cloud Logging](https://cloud.google.com/logging/docs/structured-logging)
* entry. All keys aside from `severity` and `message` are
* included in the `jsonPayload` of the logged entry.
*/
export interface LogEntry {
Expand All @@ -31,7 +32,7 @@ export interface LogEntry {

/**
* Writes a `LogEntry` to `stdout`/`stderr` (depending on severity).
* @param entry The LogEntry including severity, message, and any additional structured metadata.
* @param entry The `LogEntry` including severity, message, and any additional structured metadata.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the LogEntry type show up in the reference? Can we link to it? Or maybe that happens automatically since it's an argument for the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! TypeDoc creates a link for that.

*/
export function write(entry: LogEntry) {
if (SUPPORTS_STRUCTURED_LOGS) {
Expand All @@ -56,7 +57,7 @@ export function write(entry: LogEntry) {

/**
* Writes a `DEBUG` severity log. If the last argument provided is a plain object,
* it will be added to the `jsonPayload` in the Cloud Logging entry.
* it is added to the `jsonPayload` in the Cloud Logging entry.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a link to an explanation of jsonPayload: https://cloud.google.com/logging/docs/structured-logging

Not sure if that's TMI though

If added, this applies to all comments that mention jsonPayload

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's a good reference for the guide material, as well as for a top-level comment for logger -- which I should probably add to the main page here.

* @param args Arguments, concatenated into the log message with space separators.
*/
export function debug(...args: any[]) {
Expand All @@ -65,7 +66,7 @@ export function debug(...args: any[]) {

/**
* Writes an `INFO` severity log. If the last argument provided is a plain object,
* it will be added to the `jsonPayload` in the Cloud Logging entry.
* it is added to the `jsonPayload` in the Cloud Logging entry.
* @param args Arguments, concatenated into the log message with space separators.
*/
export function log(...args: any[]) {
Expand All @@ -74,7 +75,7 @@ export function log(...args: any[]) {

/**
* Writes an `INFO` severity log. If the last argument provided is a plain object,
* it will be added to the `jsonPayload` in the Cloud Logging entry.
* it is added to the `jsonPayload` in the Cloud Logging entry.
* @param args Arguments, concatenated into the log message with space separators.
*/
export function info(...args: any[]) {
Expand All @@ -83,7 +84,7 @@ export function info(...args: any[]) {

/**
* Writes a `WARNING` severity log. If the last argument provided is a plain object,
* it will be added to the `jsonPayload` in the Cloud Logging entry.
* it is added to the `jsonPayload` in the Cloud Logging entry.
* @param args Arguments, concatenated into the log message with space separators.
*/
export function warn(...args: any[]) {
Expand All @@ -92,13 +93,14 @@ export function warn(...args: any[]) {

/**
* Writes an `ERROR` severity log. If the last argument provided is a plain object,
* it will be added to the `jsonPayload` in the Cloud Logging entry.
* it is added to the `jsonPayload` in the Cloud Logging entry.
* @param args Arguments, concatenated into the log message with space separators.
*/
export function error(...args: any[]) {
write(entryFromArgs('ERROR', args));
}

/** @hidden */
function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
let entry = {};
const lastArg = args[args.length - 1];
Expand Down
3 changes: 3 additions & 0 deletions src/logger/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Determine if structured logs are supported (node >= 10). If something goes wrong,
// assume no since unstructured is safer.
/** @hidden */
export const SUPPORTS_STRUCTURED_LOGS =
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;

// Map LogSeverity types to their equivalent `console.*` method.
/** @hidden */
export const CONSOLE_SEVERITY: {
[severity: string]: 'debug' | 'info' | 'warn' | 'error';
} = {
Expand All @@ -18,6 +20,7 @@ export const CONSOLE_SEVERITY: {
};

// safely preserve unpatched console.* methods in case of compat require
/** @hidden */
export const UNPATCHED_CONSOLE = {
debug: console.debug,
info: console.info,
Expand Down
1 change: 1 addition & 0 deletions src/logger/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from './common';
import { format } from 'util';

/** @hidden */
function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
return function(data: any, ...args: any[]): void {
if (SUPPORTS_STRUCTURED_LOGS) {
Expand Down