Skip to content

Commit

Permalink
docs: logger migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jul 20, 2023
1 parent df0132a commit 6f9451a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions MIGRATING_V4-V5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Migrating `@salesforce/core` from v4 to v5

v5 contains breaking changes to the Logger class

Prior to v5, the Logger class wrapped a fork of Bunyan. v5 uses [Pino](https://getpino.io/)

You could create the Logger and then make modifications to it (adding streams, changing it to in-memory).

The v5 implementation is much simpler. Once you've created it, you cannot modify the logging destination.

This commit contains the breaking changes: https://github.com/forcedotcom/sfdx-core/commit/c52a29bd4162bc14ebaf690876db6589d21929fe

Most of the methods for manipulating streams, filter or changing the settings of an existing Logger are gone. The only change you can make to an existing Logger is to change the level or to add an additional field to all the outputs.

## MemoryLogging

instead of creating a Logger and then calling `useMemoryLogging` you can now pass the option `useMemoryLogger` when creating the Logger

## Output format

JSON is your only option. LogFmt no longer exists

## Debug

`enableDEBUG` is gone. The logger will use DEBUG and prettified output when `DEBUG=*` etc are in the environment.

## Log file destinations

Previously, logger went to sf.log and then rotated to sf.log.0, sf.log.1, etc.

The new Logger will write to dated files like `sf-2023-06-29.log` so that no rotation is necessary.

## Log file cleanup

When a new logger is instantiated, there's a chance that it kicks off a cleanup process on existing logs. By default, they'll be around 7 days. If you don't use the CLI much, it might take extra time before the log files are cleaned up (but they'd be much smaller in that case)

## Notes

the new `getRawLogger` is similar to the previous `getBunyanLogger. It'll return the underlying Pino instance.

This is an improvement in that it has TypeScript types for the pino methods. Example usage

```ts
// Logger (class) which contains the root pino logger. Will be created if one doesn't exist
// child (class) which contains a pino child logger
// return the pino logger from child class
const childLogger = Logger.childFromRoot('myRootChild', { tag: 'whatever' }).getRawLogger();
// same result, but async
const childLogger = await Logger.child('myRootChild', { tag: 'whatever' }).getRawLogger();

childLogger.debug('foo');
```

This skips all that Class instantiation and hierarchy (get the pino instance from the root Logger class and create a child logger off of that)

```ts
// Logger (class) which contains the root pino logger. Will be created if one doesn't exist
// pino child logger created from the root Logger (class) pino instance
const childLogger = Logger.getRoot().getRawLogger().child({ tag: 'whatever', name: `myRootChild` });
// same result, but async
const childLogger = (await Logger.root()).getRawLogger().child({ tag: 'whatever', name: `myRootChild` });

childLogger.debug('foo');
```
1 change: 1 addition & 0 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ export class Logger {
}
}

/** return various streams that the logger could send data to, depending on the options and env */
const getWriteStream = (level = 'warn'): pino.TransportSingleOptions => {
// used when debug mode, writes to stdout (colorized)
if (process.env.DEBUG) {
Expand Down

2 comments on commit 6f9451a

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 6f9451a Previous: c52a29b Ratio
Child logger creation 462880 ops/sec (±0.95%) 451475 ops/sec (±2.86%) 0.98
Logging a string on root logger 524461 ops/sec (±11.10%) 512867 ops/sec (±14.36%) 0.98
Logging an object on root logger 395699 ops/sec (±12.14%) 409567 ops/sec (±10.08%) 1.04
Logging an object with a message on root logger 247373 ops/sec (±12.93%) 257819 ops/sec (±12.47%) 1.04
Logging an object with a redacted prop on root logger 13557 ops/sec (±187.48%) 14423 ops/sec (±187.75%) 1.06
Logging a nested 3-level object on root logger 299208 ops/sec (±14.47%) 242267 ops/sec (±14.12%) 0.81

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 6f9451a Previous: c52a29b Ratio
Child logger creation 339366 ops/sec (±17.22%) 598288 ops/sec (±1.27%) 1.76
Logging a string on root logger 452263 ops/sec (±19.75%) 817851 ops/sec (±14.13%) 1.81
Logging an object on root logger 309332 ops/sec (±20.29%) 479361 ops/sec (±24.56%) 1.55
Logging an object with a message on root logger 179100 ops/sec (±19.05%) 244041 ops/sec (±22.60%) 1.36
Logging an object with a redacted prop on root logger 246463 ops/sec (±20.52%) 4044 ops/sec (±242.35%) 0.01640814239865619
Logging a nested 3-level object on root logger 138432 ops/sec (±22.01%) 275780 ops/sec (±15.74%) 1.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.