Skip to content

Commit a5d678e

Browse files
committed
Convert context.log to be more similar to Node.js console.log
1 parent 6b31dc5 commit a5d678e

File tree

3 files changed

+64
-40
lines changed

3 files changed

+64
-40
lines changed

src/InvocationContext.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
import * as types from '@azure/functions';
5-
import { ContextBindings, Logger, RetryContext, TraceContext, TriggerMetadata } from '@azure/functions';
5+
import { ContextBindings, RetryContext, TraceContext, TriggerMetadata } from '@azure/functions';
66
import { RpcInvocationRequest, RpcLog, RpcParameterBinding } from '@azure/functions-core';
77
import { convertKeysToCamelCase } from './converters/convertKeysToCamelCase';
88
import { fromRpcRetryContext, fromRpcTraceContext, fromTypedData } from './converters/RpcConverters';
@@ -58,9 +58,9 @@ class InvocationContext implements types.InvocationContext {
5858
triggerMetadata: TriggerMetadata;
5959
traceContext?: TraceContext;
6060
retryContext?: RetryContext;
61-
log: Logger;
6261
req?: Request;
6362
res?: Response;
63+
#userLogCallback: UserLogCallback;
6464

6565
constructor(info: FunctionInfo, request: RpcInvocationRequest, userLogCallback: UserLogCallback) {
6666
this.invocationId = <string>request.invocationId;
@@ -72,16 +72,33 @@ class InvocationContext implements types.InvocationContext {
7272
if (request.traceContext) {
7373
this.traceContext = fromRpcTraceContext(request.traceContext);
7474
}
75+
this.#userLogCallback = userLogCallback;
7576

7677
this.bindings = {};
78+
}
79+
80+
log(...args: any[]): void {
81+
this.#userLogCallback(RpcLog.Level.Information, ...args);
82+
}
83+
84+
trace(...args: any[]): void {
85+
this.#userLogCallback(RpcLog.Level.Trace, ...args);
86+
}
87+
88+
debug(...args: any[]): void {
89+
this.#userLogCallback(RpcLog.Level.Debug, ...args);
90+
}
91+
92+
info(...args: any[]): void {
93+
this.#userLogCallback(RpcLog.Level.Information, ...args);
94+
}
95+
96+
warn(...args: any[]): void {
97+
this.#userLogCallback(RpcLog.Level.Warning, ...args);
98+
}
7799

78-
// Log message that is tied to function invocation
79-
this.log = Object.assign((...args: any[]) => userLogCallback(RpcLog.Level.Information, ...args), {
80-
error: (...args: any[]) => userLogCallback(RpcLog.Level.Error, ...args),
81-
warn: (...args: any[]) => userLogCallback(RpcLog.Level.Warning, ...args),
82-
info: (...args: any[]) => userLogCallback(RpcLog.Level.Information, ...args),
83-
verbose: (...args: any[]) => userLogCallback(RpcLog.Level.Trace, ...args),
84-
});
100+
error(...args: any[]): void {
101+
this.#userLogCallback(RpcLog.Level.Error, ...args);
85102
}
86103
}
87104

types/index.d.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,41 @@ declare module '@azure/functions' {
6969
retryContext?: RetryContext;
7070

7171
/**
72-
* Allows you to write streaming function logs. Calling directly allows you to write streaming function logs
73-
* at the default trace level.
72+
* The recommended way to log data during invocation.
73+
* Similar to Node.js's `console.log`, but has integration with Azure features like application insights
74+
* Uses the 'information' log level
7475
*/
75-
log: Logger;
76+
log(...args: any[]): void;
77+
78+
/**
79+
* The recommended way to log trace data (level 0) during invocation.
80+
* Similar to Node.js's `console.trace`, but has integration with Azure features like application insights
81+
*/
82+
trace(...args: any[]): void;
83+
84+
/**
85+
* The recommended way to log debug data (level 1) during invocation.
86+
* Similar to Node.js's `console.debug`, but has integration with Azure features like application insights
87+
*/
88+
debug(...args: any[]): void;
89+
90+
/**
91+
* The recommended way to log information data (level 2) during invocation.
92+
* Similar to Node.js's `console.info`, but has integration with Azure features like application insights
93+
*/
94+
info(...args: any[]): void;
95+
96+
/**
97+
* The recommended way to log warning data (level 3) during invocation.
98+
* Similar to Node.js's `console.warn`, but has integration with Azure features like application insights
99+
*/
100+
warn(...args: any[]): void;
101+
102+
/**
103+
* The recommended way to log error data (level 4) during invocation.
104+
* Similar to Node.js's `console.error`, but has integration with Azure features like application insights
105+
*/
106+
error(...args: any[]): void;
76107

77108
/**
78109
* HTTP request object. Provided to your function when using HTTP Bindings.
@@ -430,31 +461,7 @@ declare module '@azure/functions' {
430461
*/
431462
direction: 'in' | 'out' | 'inout' | undefined;
432463
}
433-
/**
434-
* Allows you to write streaming function logs.
435-
*/
436-
export interface Logger {
437-
/**
438-
* Writes streaming function logs at the default trace level.
439-
*/
440-
(...args: any[]): void;
441-
/**
442-
* Writes to error level logging or lower.
443-
*/
444-
error(...args: any[]): void;
445-
/**
446-
* Writes to warning level logging or lower.
447-
*/
448-
warn(...args: any[]): void;
449-
/**
450-
* Writes to info level logging or lower.
451-
*/
452-
info(...args: any[]): void;
453-
/**
454-
* Writes to verbose level logging.
455-
*/
456-
verbose(...args: any[]): void;
457-
}
464+
458465
/**
459466
* Timer schedule information. Provided to your function when using a timer binding.
460467
*/

types/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export const timerTrigger: AzureFunction = async function (context: InvocationCo
4747

4848
const runServiceBus: AzureFunction = async function (context: InvocationContext, myQueueItem: string) {
4949
context.log('Node.js ServiceBus queue trigger function processed message', myQueueItem);
50-
context.log.verbose('EnqueuedTimeUtc =', context.triggerMetadata.enqueuedTimeUtc);
51-
context.log.verbose('DeliveryCount =', context.triggerMetadata.deliveryCount);
52-
context.log.verbose('MessageId =', context.triggerMetadata.messageId);
50+
context.trace('EnqueuedTimeUtc =', context.triggerMetadata.enqueuedTimeUtc);
51+
context.trace('DeliveryCount =', context.triggerMetadata.deliveryCount);
52+
context.trace('MessageId =', context.triggerMetadata.messageId);
5353
};
5454

5555
// Assumes output binding is named '$return'

0 commit comments

Comments
 (0)