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: enhance debug logging abilities #8201

Merged
merged 1 commit into from
Oct 2, 2022
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ that has not explicitly activated it. To activate it set the appropriate flag to
let app = new EmberApp(defaults, {
emberData: {
debug: {
LOG_PAYLOADS: false, // data store received to update cache with
LOG_OPERATIONS: false, // updates to cache remote state
LOG_MUTATIONS: false, // updates to cache local state
LOG_NOTIFICATIONS: false,
LOG_REQUEST_STATUS: false,
LOG_IDENTIFIERS: false,
LOG_GRAPH: false,
LOG_INSTANCE_CACHE: false,
LOG_GRAPH: false, // relationship storage
LOG_INSTANCE_CACHE: false, // instance creation/deletion
}
}
});
Expand Down
5 changes: 4 additions & 1 deletion packages/-ember-data/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ It provides many of the facilities you'd find in server-side `ORM`s like `Active

EmberData is organized into primitives that compose together via public APIs.

- [@ember-data/store](/ember-data/release/modules/@ember-data%2Fstore is the core and handles coordination
- [@ember-data/store](/ember-data/release/modules/@ember-data%2Fstore) is the core and handles coordination
- [@ember-data/record-data](/ember-data/release/modules/@ember-data%2Frecord-data) is a resource cache for JSON:API structured data. It integrates with the store via the hook `createRecordDataFor`
- [@ember-data/model](/ember-data/release/modules/@ember-data%2Fmodel) is a presentation layer, it integrates with the store via the hooks `instantiateRecord` and `teardownRecord`.
- [@ember-data/adapter](/ember-data/release/modules/@ember-data%2Fadapter) provides various network API integrations for APIS built over specific REST or JSON:API conventions.
Expand Down Expand Up @@ -82,6 +82,9 @@ that has not explicitly activated it. To activate it set the appropriate flag to
let app = new EmberApp(defaults, {
emberData: {
debug: {
LOG_PAYLOADS: false, // data store received to update cache with
LOG_OPERATIONS: false, // updates to cache remote state
LOG_MUTATIONS: false, // updates to cache local state
LOG_NOTIFICATIONS: false,
LOG_REQUEST_STATUS: false,
LOG_IDENTIFIERS: false,
Expand Down
3 changes: 3 additions & 0 deletions packages/-ember-data/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ module.exports = function (defaults) {
let config = {
compatWith,
debug: {
LOG_PAYLOADS: process.env.DEBUG_DATA ? true : false,
LOG_OPERATIONS: process.env.DEBUG_DATA ? true : false,
LOG_MUTATIONS: process.env.DEBUG_DATA ? true : false,
LOG_NOTIFICATIONS: process.env.DEBUG_DATA ? true : false,
LOG_REQUEST_STATUS: process.env.DEBUG_DATA ? true : false,
LOG_IDENTIFIERS: process.env.DEBUG_DATA ? true : false,
Expand Down
3 changes: 3 additions & 0 deletions packages/-ember-data/node-tests/fixtures/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ module.exports = {
'(public) @ember-data/adapter/rest RESTAdapter#sortQueryParams',
'(public) @ember-data/adapter/rest RESTAdapter#updateRecord',
'(public) @ember-data/adapter/rest RESTAdapter#useFetch',
"(public) @ember-data/debug DebugLogging#LOG_PAYLOADS",
"(public) @ember-data/debug DebugLogging#LOG_OPERATIONS",
"(public) @ember-data/debug DebugLogging#LOG_MUTATIONS",
"(public) @ember-data/debug DebugLogging#LOG_GRAPH",
"(public) @ember-data/debug DebugLogging#LOG_IDENTIFIERS",
"(public) @ember-data/debug DebugLogging#LOG_INSTANCE_CACHE",
Expand Down
26 changes: 26 additions & 0 deletions packages/private-build-infra/addon/debugging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ that has not explicitly activated it. To activate it set the appropriate flag to
let app = new EmberApp(defaults, {
emberData: {
debug: {
LOG_PAYLOADS: false, // data store received to update cache with
LOG_OPERATIONS: false, // updates to cache remote state
LOG_MUTATIONS: false, // updates to cache local state
LOG_NOTIFICATIONS: false,
LOG_REQUEST_STATUS: false,
LOG_IDENTIFIERS: false,
Expand All @@ -24,6 +27,29 @@ that has not explicitly activated it. To activate it set the appropriate flag to
@class DebugLogging
@public
*/
/**
* log payloads received by the store
* via `push` or returned from a delete/update/create
* operation.
*
* @property {boolean} LOG_PAYLOADS
* @public
*/
export const LOG_PAYLOADS = false;
/**
* log remote-state updates to the cache
*
* @property {boolean} LOG_OPERATIONS
* @public
*/
export const LOG_OPERATIONS = false;
/**
* log local-state updates to the cache
*
* @property {boolean} LOG_MUTATIONS
* @public
*/
export const LOG_MUTATIONS = false;
/**
* log notifications received by the RecordNotificationManager
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ function addonBuildConfigForDataPackage(PackageName) {
options.emberData.debug = options.emberData.debug || {};
const debugOptions = Object.assign(
{
LOG_PAYLOADS: false,
LOG_OPERATIONS: false,
LOG_MUTATIONS: false,
LOG_NOTIFICATIONS: false,
LOG_REQUEST_STATUS: false,
LOG_IDENTIFIERS: false,
Expand Down
46 changes: 44 additions & 2 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assert } from '@ember/debug';
import { schedule } from '@ember/runloop';
import { isEqual } from '@ember/utils';

import { LOG_MUTATIONS, LOG_OPERATIONS } from '@ember-data/private-build-infra/debugging';
import type {
CollectionResourceRelationship,
SingleResourceRelationship,
Expand Down Expand Up @@ -108,6 +109,17 @@ export default class SingletonRecordData implements RecordData {
let changedKeys: string[] | undefined;
const cached = this.__peek(identifier);

if (LOG_OPERATIONS) {
try {
let _data = JSON.parse(JSON.stringify(data));
// eslint-disable-next-line no-console
console.log('EmberData | Operation - pushData (upsert)', _data);
} catch (e) {
// eslint-disable-next-line no-console
console.log('EmberData | Operation - pushData (upsert)', data);
}
}

if (cached.isNew) {
cached.isNew = false;
this.__storeWrapper.notifyChange(identifier, 'state');
Expand Down Expand Up @@ -136,6 +148,16 @@ export default class SingletonRecordData implements RecordData {
}

sync(op: MergeOperation): void {
if (LOG_OPERATIONS) {
try {
let _data = JSON.parse(JSON.stringify(op));
// eslint-disable-next-line no-console
console.log(`EmberData | Operation - sync ${op.op}`, _data);
} catch (e) {
// eslint-disable-next-line no-console
console.log(`EmberData | Operation - sync ${op.op}`, op);
}
}
if (op.op === 'mergeIdentifiers') {
const cache = this.__cache.get(op.record);
if (cache) {
Expand All @@ -146,11 +168,31 @@ export default class SingletonRecordData implements RecordData {
}
}

update(operation: LocalRelationshipOperation): void {
graphFor(this.__storeWrapper).update(operation, false);
update(op: LocalRelationshipOperation): void {
if (LOG_MUTATIONS) {
try {
let _data = JSON.parse(JSON.stringify(op));
// eslint-disable-next-line no-console
console.log(`EmberData | Mutation - update ${op.op}`, _data);
} catch (e) {
// eslint-disable-next-line no-console
console.log(`EmberData | Mutation - update ${op.op}`, op);
}
}
graphFor(this.__storeWrapper).update(op, false);
}

clientDidCreate(identifier: StableRecordIdentifier, options?: Dict<unknown> | undefined): Dict<unknown> {
if (LOG_MUTATIONS) {
try {
let _data = options ? JSON.parse(JSON.stringify(options)) : options;
// eslint-disable-next-line no-console
console.log(`EmberData | Mutation - clientDidCreate ${identifier.lid}`, _data);
} catch (e) {
// eslint-disable-next-line no-console
console.log(`EmberData | Mutation - clientDidCreate ${identifier.lid}`, options);
}
}
const cached = this.__peek(identifier);
cached.isNew = true;
let createOptions = {};
Expand Down
21 changes: 21 additions & 0 deletions packages/store/addon/-private/store-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { reject, resolve } from 'rsvp';

import type DSModelClass from '@ember-data/model';
import { HAS_MODEL_PACKAGE, HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra';
import { LOG_PAYLOADS } from '@ember-data/private-build-infra/debugging';
import {
DEPRECATE_HAS_RECORD,
DEPRECATE_JSON_API_FALLBACK,
Expand Down Expand Up @@ -2119,6 +2120,16 @@ class Store extends Service {
if (DEBUG) {
assertDestroyingStore(this, '_push');
}
if (LOG_PAYLOADS) {
try {
let data = JSON.parse(JSON.stringify(jsonApiDoc));
// eslint-disable-next-line no-console
console.log('EmberData | Payload - push', data);
} catch (e) {
// eslint-disable-next-line no-console
console.log('EmberData | Payload - push', jsonApiDoc);
}
}
let ret;
this._join(() => {
let included = jsonApiDoc.included;
Expand Down Expand Up @@ -2300,6 +2311,16 @@ class Store extends Service {
let fetchManagerPromise = this._fetchManager.scheduleSave(identifier, saveOptions);
return fetchManagerPromise.then(
(payload) => {
if (LOG_PAYLOADS) {
try {
let data = payload ? JSON.parse(JSON.stringify(payload)) : payload;
// eslint-disable-next-line no-console
console.log(`EmberData | Payload - ${operation}`, data);
} catch (e) {
// eslint-disable-next-line no-console
console.log(`EmberData | Payload - ${operation}`, payload);
}
}
/*
// TODO @runspired re-evaluate the below claim now that
// the save request pipeline is more streamlined.
Expand Down