Skip to content

Commit

Permalink
feat: Add configureLogger() API
Browse files Browse the repository at this point in the history
  • Loading branch information
webJose committed May 22, 2024
1 parent d6700eb commit 7321fd9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/components/Errors/Codes/42.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react";

export default function ErrorCode42(props) {
return (
<>
<h1>#42: The given logger does not conform to the ILogger interface</h1>
<p>
The <code>configureLogger()</code> function was called with an argument that does not
conform to the <code>ILogger</code> interface. An <code>ILogger</code> must have the
following members: <code>debug()</code>, <code>info()</code>, <code>warn()</code> and
<code>error()</code>.
</p>
<h2>To fix:</h2>
<p>
There are 3 possible arguments that are acceptable for <code>configureLogger()</code>:
</p>
<ol>
<li>
The value <code>null</code>, that silences all log messages coming from single-spa.
</li>
<li>
The <code>console</code> object, which causes single-spa to log all messages directly to the console.
</li>
<li>
A custom implementation that can be used for pretty much anyting: From selective
silencing of specific errors to forwarding logs to a central logging service.
</li>
</ol>
<h2>Additional references</h2>
<p>
See{" "}
<a href="/docs/api#configurelogger">
the API documentation
</a>{" "}
for information and the details of the <code>ILogger</code> interface.
</p>
</>
);
}
48 changes: 48 additions & 0 deletions versioned_docs/version-6.x/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,54 @@ Sets the global configuration for unload timeouts.

`undefined`

---

## configureLogger

```js
// Silence all logging.
singleSpa.configureLogger(null);

// Restore console logging (single-spa's default behavior).
singleSpa.configureLogger(console);

// Custom logger implementation.
const myLogger = new MyLogger(); // MyLogger must implement ILogger.
singleSpa.configureLogger(myLogger);
```

Controls the destination of logged messages. Use it whenever you want or need to take control
over what is being logged and where. Logging can be silenced, can be restored, or a custom
implementation can be provided for the fancier use cases.

<h3>ILogger</h3>

```typescript
export interface ILogger {
debug(...data: any[]): void;
info(...data: any[]): void;
warn(...data: any[]): void;
error(...data: any[]): void;
}
```

Custom loggers must conform to the `ILogger` interface shown above. The implementation can do
anything it wants with the provided data, but **must never throw** as single-spa does not account
for potential errors from custom implementations.

<h3>arguments</h3>

<dl className="args-list">
<dt>logger: ILogger | null</dt>
<dd>The desired logger object, or <code>null</code> to silence logging.</dd>
</dl>

<h3>returns</h3>

`undefined`

---

## Events

single-spa fires Events to the `window` as a way for your code to hook into URL transitions.
Expand Down

0 comments on commit 7321fd9

Please sign in to comment.