Skip to content

Commit

Permalink
Support configuring loggers as described in issue actions-on-google#413
Browse files Browse the repository at this point in the history
(actions-on-google#413).

With that change, a client application that uses Firebase cloud functions would simply need to do the following to improve substantially the readability of log entries in the cloud functions logs console:

const app = smarthome({
  debug: true,
  logs: {
    debug: functions.logger.debug,
    info: functions.logger.info,
    // 'warn' maps to 'error' in firebase logs.
    // If set to functions.logger.warn, there is no level associated with the log entry.
    warn: functions.logger.error,
    error: functions.logger.error
  }
});
  • Loading branch information
pierredelisle committed Feb 10, 2021
1 parent eaa9e20 commit b4e7bbe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
24 changes: 23 additions & 1 deletion src/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@ import * as common from './common'
/** @public */
export type AppHandler = OmniHandler & BaseApp

/** @public */
export interface LogFn {
(params: any[]): void;
}

/** @public */
export interface AppLogs {
debug: LogFn,
info: LogFn,
warn: LogFn,
error: LogFn
}

/** @public */
export interface AppOptions {
/** @public */
debug?: boolean
debug?: boolean,
logs?: AppLogs
}

/** @hidden */
Expand All @@ -50,6 +64,7 @@ export interface BaseApp extends ServiceBaseApp {

/** @public */
debug: boolean
logs?: AppLogs
}

/** @hidden */
Expand All @@ -60,6 +75,7 @@ const create = (options?: AppOptions): BaseApp => ({
return plugin(this) || this
},
debug: !!(options && options.debug),
logs: (typeof (options) === 'undefined' || typeof (options.logs) === 'undefined') ? undefined : options.logs
})

/** @hidden */
Expand All @@ -78,6 +94,12 @@ export const attach = <TService>(
return app.handler(args[0], args[1])
}
app = Object.assign(omni, app)

if (app.logs) {
// Redefine debug, warn, error, info according to app definitions of logs.
common.setLogs(app.logs)
}

const handler: typeof app.handler = app.handler.bind(app)
const standard: StandardHandler = async (body, headers, metadata) => {
const log = app.debug ? common.info : common.debug
Expand Down
20 changes: 16 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@

import * as Debug from 'debug'
import * as https from 'https'
import { AppLogs } from './assistant'

const name = 'actions-on-google'

/** @hidden */
export const debug = Debug(`${name}:debug`)
let debug = Debug(`${name}:debug`)
export { debug }

/** @hidden */
export const warn = Debug(`${name}:warn`)
let warn = Debug(`${name}:warn`)
export { warn }

/** @hidden */
// tslint:disable-next-line:no-console Allow console binding
export const error = console.error.bind(console) as typeof console.error
let error = console.error.bind(console) as typeof console.error
export { error }

/** @hidden */
// tslint:disable-next-line:no-console Allow console binding
export const info = console.log.bind(console) as typeof console.log
let info = console.log.bind(console) as typeof console.log
export { info }

warn.log = error
debug.log = info
Expand All @@ -52,6 +57,13 @@ export const values = <T>(o: { [key: string]: T }) => Object.keys(o).map(k => o[
/** @hidden */
export const clone = <T>(o: T): T => JSON.parse(JSON.stringify(o))

export const setLogs = (logs: AppLogs) => {
debug.log = logs.debug
info = logs.info
warn.log = logs.warn
error = logs.error
}

/** @hidden */
// tslint:disable-next-line:no-any root can be anything
export const stringify = (root: any, ...exclude: string[]) => {
Expand Down

0 comments on commit b4e7bbe

Please sign in to comment.