Skip to content

Commit f9b24e0

Browse files
authored
Adding logger SDK to reference, with some edits. (#714)
* Adding logger SDK to reference, with some edits. * Adding link to structured logging details per feedback.
1 parent c23a8c1 commit f9b24e0

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

docgen/content-sources/toc.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ toc:
9393
- title: 'CallableContext'
9494
path: /docs/reference/functions/providers_https_.callablecontext.html
9595

96+
- title: 'functions.logger'
97+
path: /docs/reference/functions/logger_.html
98+
section:
99+
- title: 'LogEntry'
100+
path: /docs/reference/functions/logger_.logentry.html
101+
96102
- title: 'functions.pubsub'
97103
path: /docs/reference/functions/providers_pubsub_.html
98104
section:

src/logger.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from './logger/common';
88

99
/**
10-
* `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.
10+
* `LogSeverity` indicates the detailed severity of the log entry. See [LogSeverity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity).
1111
*/
1212
export type LogSeverity =
1313
| 'DEBUG'
@@ -20,7 +20,8 @@ export type LogSeverity =
2020
| 'EMERGENCY';
2121

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

3233
/**
3334
* Writes a `LogEntry` to `stdout`/`stderr` (depending on severity).
34-
* @param entry The LogEntry including severity, message, and any additional structured metadata.
35+
* @param entry The `LogEntry` including severity, message, and any additional structured metadata.
3536
*/
3637
export function write(entry: LogEntry) {
3738
if (SUPPORTS_STRUCTURED_LOGS) {
@@ -56,7 +57,7 @@ export function write(entry: LogEntry) {
5657

5758
/**
5859
* Writes a `DEBUG` severity log. If the last argument provided is a plain object,
59-
* it will be added to the `jsonPayload` in the Cloud Logging entry.
60+
* it is added to the `jsonPayload` in the Cloud Logging entry.
6061
* @param args Arguments, concatenated into the log message with space separators.
6162
*/
6263
export function debug(...args: any[]) {
@@ -65,7 +66,7 @@ export function debug(...args: any[]) {
6566

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

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

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

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

103+
/** @hidden */
102104
function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
103105
let entry = {};
104106
const lastArg = args[args.length - 1];

src/logger/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Determine if structured logs are supported (node >= 10). If something goes wrong,
22
// assume no since unstructured is safer.
3+
/** @hidden */
34
export const SUPPORTS_STRUCTURED_LOGS =
45
parseInt(process.versions?.node?.split('.')?.[0] || '8', 10) >= 10;
56

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

2022
// safely preserve unpatched console.* methods in case of compat require
23+
/** @hidden */
2124
export const UNPATCHED_CONSOLE = {
2225
debug: console.debug,
2326
info: console.info,

src/logger/compat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from './common';
66
import { format } from 'util';
77

8+
/** @hidden */
89
function patchedConsole(severity: string): (data: any, ...args: any[]) => void {
910
return function(data: any, ...args: any[]): void {
1011
if (SUPPORTS_STRUCTURED_LOGS) {

0 commit comments

Comments
 (0)