Skip to content
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

feat: Add a logger to embed init options #732

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ var opt = {
| `defaultStyle` | Boolean or String | If set to `true` (default), the embed actions are shown in a menu. Set to `false` to use simple links. Provide a string to set the style sheet. |
| `bind` | String or Element | The element that should contain any input elements bound to signals. |
| `renderer` | String | The renderer to use for the view. One of `"canvas"` (default) or `"svg"`. See [Vega docs](https://vega.github.io/vega/docs/api/view/#view_renderer) for details. May be a custom value if passing your own `viewClass` option. |
| `logger` | Object | Sets a [custom logger handler on the view](https://vega.github.io/vega/docs/api/view/#view_logger). A logger must implement the [Vega logger API](https://github.com/vega/vega/tree/master/packages/vega-util#logger). |
| `logLevel` | Level | Sets the current log level. See [Vega docs](https://vega.github.io/vega/docs/api/view/#view_logLevel) for details. |
| `tooltip` | Handler or Boolean or Object | Provide a [tooltip handler](https://vega.github.io/vega/docs/api/view/#view_tooltip), customize the default [Vega Tooltip](https://github.com/vega/vega-tooltip) handler, or disable the default handler. |
| `loader` | Loader / Object | _Loader_ : Sets a custom Vega loader. _Object_ : Vega loader options for a loader that will be created. <br> See [Vega docs](https://vega.github.io/vega/docs/api/view/#view) for details. |
Expand Down
7 changes: 6 additions & 1 deletion src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
isString,
Loader,
LoaderOptions,
logger as vegaLogger,
LoggerInterface,
mergeConfig,
Renderers,
Spec as VgSpec,
Expand Down Expand Up @@ -68,6 +70,7 @@ export interface EmbedOptions<S = string, R = Renderers> {
mode?: Mode;
theme?: 'excel' | 'ggplot2' | 'quartz' | 'vox' | 'dark';
defaultStyle?: boolean | string;
logger?: LoggerInterface;
logLevel?: number;
loader?: Loader | LoaderOptions;
renderer?: R;
Expand Down Expand Up @@ -275,6 +278,7 @@ async function _embed(
const i18n = {...I18N, ...opts.i18n};

const renderer = opts.renderer ?? 'canvas';
const logger = opts.logger ?? vegaLogger();
const logLevel = opts.logLevel ?? vega.Warn;
const downloadFileName = opts.downloadFileName ?? 'visualization';

Expand All @@ -300,7 +304,7 @@ async function _embed(

const mode = guessMode(spec, opts.mode);

let vgSpec: VgSpec = PREPROCESSOR[mode](spec, config);
let vgSpec: VgSpec = PREPROCESSOR[mode](spec, { ...config, logger });
Copy link
Author

Choose a reason for hiding this comment

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

Can be added directly to the passed config, since the whole arg will be discarded when the mode is not 'vega-lite'


if (mode === 'vega-lite') {
if (vgSpec.$schema) {
Expand Down Expand Up @@ -352,6 +356,7 @@ async function _embed(

const view = new (opts.viewClass || vega.View)(runtime, {
loader,
logger,
logLevel,
renderer,
...(ast ? {expr: (vega as any).expressionInterpreter} : {}),
Expand Down
8 changes: 8 additions & 0 deletions test/embed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,11 @@ test('can set loader via usermeta', async () => {
target: '_blank',
});
});

test('can set logger', async () => {
const el = document.createElement('div');
const logger = vega.logger();
const result = await embed(el, vlSpec, {logger});
expect(result).toBeTruthy();
expect((result.view as any)._log).toEqual(logger);
});