---
schema_version: "1"
id: c521833f3954976f615c2ae48c6e6886e52b4c0ca648b3c8b7ee8f4539619c0c
title: Logger
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lightning-base-components/lightning-design-system/guides/lwc/components/lightning/logger/lightning-logger.md
canonical_url: https://developer.salesforce.com/docs/platform/lightning-component-reference/guide/lightning-logger.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: e07887a9188d79bb2349a8684ffa9e089f8ef774d67ef5e98a796c4117294b42
products:
  - Lightning Component Reference
---
# Logger

**`lightning-logger`**

Logs a string as the message. The module also validates and stringifies other types. It truncates messages over a certain length and logs a blank string for an empty input. Messages are tokenized and can't be seen outside of the customer's org.

**For Use In**

Lightning Experience

::::::tabset{tabs='[ "Develop", "Specifications"]'}

:::::tab

The `lightning/logger` module provides a function to add observability to your Lightning web components using the Custom Component Instrumentation API. Use the `log()` function to log custom messages to Event Monitoring from your components. In your component's Javascript file, import the function from the `lightning/logger` module.

```javascript
import { log } from "lightning/logger";
```

`lightning/logger` is available only in Lightning Experience and not in the Salesforce mobile app. Event Monitoring must be enabled in your org to use `lightning/logger`.

The browser sends event logs for `lightning/logger` to the server in batches when the user interacts with the page and when the page closes or refreshes. If the user's connection is interrupted or the login session ends due to inactivity, any pending event logs for `lightning/logger` on the browser are deleted. There isn’t a way to retrieve these logs.

## `log(message)`

Logs the event data from the component.

| Parameter | Type             | Description                                                                                                                                                                                        |
| --------- | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| message   | string or object | The message to be logged to the Lightning Logger Event Type in Event Monitoring. The maximum string length is 4096 characters. The `log()` function automatically stringifies an object passed in. |

## Usage

The following example logs a click from within a button component. The `log()` function can be used for any event.

Create the HTML template for your component.

<!-- prettier-ignore-start -->
```html
<template>
    <lightning-button label="Approve" onclick={handleClick}>
    </lightning-button>
</template>
```
<!-- prettier-ignore-end -->

In the JavaScript, import `log` from `lightning/logger` and use it in the `handleClick` function. In this example, clicking on the button creates an object that is passed to Event Monitoring.

```js
import { LightningElement } from "lwc";
import { log } from "lightning/logger";

export default class HelloWorld extends LightningElement {
  handleClick() {
    let msg = {
      type: "click",
      action: "Approve",
    };
    log(msg);
  }
}
```

## See Also

[_Object Reference for the Salesforce Platform_: Lightning Logger Event Type](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_eventlogfile_lightninglogger.htm)


:::::

:::::tab
::component-specification{component="logger" model="lwc" namespace="lightning"}

:::::

::::::
