Skip to content

Commit

Permalink
Add logger recipe (Esteve Graells code) (#957)
Browse files Browse the repository at this point in the history
* Add logger recipe

* Add event monitoring setup warning

* Add component to page and clarify usage

* Revert platformWorkspaceAPi change
  • Loading branch information
albarivas authored Oct 22, 2024
1 parent 80449e0 commit c01495c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
<identifier>miscDomQuery</identifier>
</componentInstance>
</itemInstances>
<itemInstances>
<componentInstance>
<componentName>miscLogger</componentName>
<identifier>c_miscLogger</identifier>
</componentInstance>
</itemInstances>
<name>region3</name>
<type>Region</type>
</flexiPageRegions>
Expand Down
29 changes: 29 additions & 0 deletions force-app/main/default/lwc/miscLogger/__tests__/miscLogger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createElement } from 'lwc';
import MiscLogger from 'c/miscLogger';
import { log } from 'lightning/logger';

describe('c-misc-logger', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});

it('When log to event monitoring is clicked, the lightning log function is called', () => {
// Create component
const element = createElement('c-misc-logger', {
is: MiscLogger
});
document.body.appendChild(element);

// Click button
const buttonEl = element.shadowRoot.querySelector(
'div.event-monitoring lightning-button'
);
buttonEl.click();

// Check log function has been called
expect(log).toHaveBeenCalled();
});
});
27 changes: 27 additions & 0 deletions force-app/main/default/lwc/miscLogger/miscLogger.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<template>
<lightning-card title="MiscLogger" icon-name="custom:custom63">
<div class="slds-var-m-around_medium">
<p class="slds-var-m-bottom_small">
<lightning-button
label="Log a message to the console"
onclick={logMessageConsole}
></lightning-button>
</p>
</div>
<div class="slds-var-m-around_medium event-monitoring">
<p class="slds-var-m-bottom_small">
<lightning-button
label="Log a message to the console and Event Monitoring"
onclick={logMessageEventMonitoring}
></lightning-button>
</p>
</div>

<c-view-source source="lwc/misclogger" slot="footer">
Log custom messages to Event Monitoring, adding observability to
your LWCs. To use this feature, enable Lightning Logger Events from
Setup, under Event Monitoring Settings. View the logger output in
the console by launching the developer tools.
</c-view-source>
</lightning-card>
</template>
15 changes: 15 additions & 0 deletions force-app/main/default/lwc/miscLogger/miscLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LightningElement } from 'lwc';
import { log } from 'lightning/logger';

export default class MiscLogger extends LightningElement {
logMessageEventMonitoring() {
let msg = {
type: 'click',
action: 'Log'
};
log(msg);
}
logMessageConsole() {
console.log('This message is logged to the console');
}
}
9 changes: 9 additions & 0 deletions force-app/main/default/lwc/miscLogger/miscLogger.js-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
1 change: 1 addition & 0 deletions force-app/test/jest-mocks/lightning/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const log = jest.fn();
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module.exports = {
'^lightning/modal$':
'<rootDir>/force-app/test/jest-mocks/lightning/modal',
'^lightning/refresh$':
'<rootDir>/force-app/test/jest-mocks/lightning/refresh'
'<rootDir>/force-app/test/jest-mocks/lightning/refresh',
'^lightning/logger$':
'<rootDir>/force-app/test/jest-mocks/lightning/logger'
},
setupFiles: ['jest-canvas-mock'],
setupFilesAfterEnv,
Expand Down

0 comments on commit c01495c

Please sign in to comment.