Skip to content

Commit

Permalink
Merge pull request #186 from benjamin-guibert/feat/customisable-prett…
Browse files Browse the repository at this point in the history
…y-print

feat(logger): add `prettyPrint` in config
  • Loading branch information
schehata committed May 20, 2024
2 parents b305dd9 + 1c5f022 commit bdc5366
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ export NEXT_PUBLIC_AXIOM_LOG_LEVEL=info

You can also disable logging completely by setting the log level to `off`.

#### Custom printing to console

Printing logs to the console can be customized through the `prettyPrint` configuration. It is a function taking a `LogEvent` as parameter.
The following example prints the event message without any formatting:

```typescript
const logger = new Logger({
prettyPrint: (event) => {
console.log(event.message);
},
});
```

## Upgrade to the App Router

next-axiom switched to support the App Router starting with version 1.0. If you are upgrading a Pages Router app with next-axiom v0.x to the App Router, you will need to make the following changes:
Expand Down
4 changes: 3 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type LoggerConfig = {
autoFlush?: boolean;
source?: string;
req?: any;
prettyPrint?: typeof prettyPrint;
};

export class Logger {
Expand All @@ -60,6 +61,7 @@ export class Logger {
public config: LoggerConfig = {
autoFlush: true,
source: 'frontend',
prettyPrint: prettyPrint,
};

constructor(public initConfig: LoggerConfig = {}) {
Expand Down Expand Up @@ -151,7 +153,7 @@ export class Logger {
if (!config.isEnvVarsSet()) {
// if AXIOM ingesting url is not set, fallback to printing to console
// to avoid network errors in development environments
this.logEvents.forEach((ev) => prettyPrint(ev));
this.logEvents.forEach((ev) => (this.config.prettyPrint ? this.config.prettyPrint(ev) : prettyPrint(ev)));
this.logEvents = [];
return;
}
Expand Down

0 comments on commit bdc5366

Please sign in to comment.