Skip to content

Commit cdc842f

Browse files
authored
Logging, typescript module syntax and dev ergonomics (#1)
* adding pino logging and spec requirements * logging patterns * execution conditions; typescript module syntax * test fix
1 parent e78aa34 commit cdc842f

File tree

37 files changed

+784
-119
lines changed

37 files changed

+784
-119
lines changed

AGENTS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

2+
- this is a monorepo project; packages:
3+
- ./packages/b2c-cli - the command line interface built with oclif
4+
- ./packages/b2c-tooling - the SDK/library for B2C Commerce operations; support the CLI and can be used standalone
5+
26
## Setup/Packaging
37

48
- use `pnpm` over `npm` for package management
59
- the `pnpm run test` commands also run the linter after tests
610
- use `pnpm run -r format` (or individually in packages) to format code with prettier
11+
- use `exports` field in package.json files to define public API surface for packages; use `development` field for nodejs --conditions for development ergonomics (packages/b2c-cli/bin/dev.js will use this condition)
712

813
## Documentation
914

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
- Separate CLI and SDK
77
- Logging
8+
- redaction
89
- Localization Support
10+
- supply chain security
911

1012
## CLI
1113

docs/cli/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ These flags are available on all commands that interact with B2C instances:
3030
- [Sandbox Commands](./sandbox) - Create and manage sandboxes
3131
- [MRT Commands](./mrt) - Manage Managed Runtime environments
3232

33+
## Configuration
34+
35+
- [Logging](./logging) - Log levels, output formats, and environment variables
36+
3337
## Getting Help
3438

3539
Get help for any command:

docs/cli/logging.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Logging
2+
3+
The CLI uses [pino](https://github.com/pinojs/pino) for structured logging with pretty-printed output by default.
4+
5+
## Output Modes
6+
7+
### Pretty Print (Default)
8+
9+
Human-readable output with colors, suitable for interactive terminal use:
10+
11+
```
12+
[18:31:58] INFO: Deploying cartridges...
13+
[18:31:59] INFO: Upload complete
14+
```
15+
16+
### JSON Lines (`--json`)
17+
18+
Machine-readable output for scripting, CI/CD pipelines, and log aggregation:
19+
20+
```bash
21+
b2c code deploy --json
22+
```
23+
24+
```json
25+
{"level":"info","time":1764113529411,"command":"code deploy","msg":"Deploying cartridges..."}
26+
{"level":"info","time":1764113529412,"command":"code deploy","msg":"Upload complete"}
27+
```
28+
29+
## Log Levels
30+
31+
Control verbosity with `--log-level` or `-D` for debug:
32+
33+
| Level | Description |
34+
|-------|-------------|
35+
| `trace` | Maximum verbosity |
36+
| `debug` | Detailed operational info |
37+
| `info` | Normal messages (default) |
38+
| `warn` | Warnings |
39+
| `error` | Errors only |
40+
| `silent` | No output |
41+
42+
```bash
43+
b2c code deploy --log-level debug
44+
b2c code deploy -D # shorthand for --log-level debug
45+
```
46+
47+
In debug/trace mode, context objects are displayed:
48+
49+
```
50+
[18:31:58] INFO: Upload complete
51+
command: "code deploy"
52+
file: "cartridge.zip"
53+
bytes: 45678
54+
duration: 1234
55+
```
56+
57+
## Flags
58+
59+
| Flag | Environment Variable | Description |
60+
|------|---------------------|-------------|
61+
| `--log-level` | `SFCC_LOG_LEVEL` | Set log verbosity |
62+
| `-D, --debug` | `SFCC_DEBUG` | Enable debug logging |
63+
| `--json` | | Output JSON lines |
64+
65+
## Environment Variables
66+
67+
| Variable | Description |
68+
|----------|-------------|
69+
| `SFCC_LOG_LEVEL` | Log level (trace, debug, info, warn, error, silent) |
70+
| `SFCC_DEBUG` | Enable debug logging |
71+
| `SFCC_LOG_TO_STDOUT` | Send logs to stdout instead of stderr |
72+
| `SFCC_LOG_COLORIZE` | Force colors on/off |
73+
| `SFCC_REDACT_SECRETS` | Set to `false` to disable secret redaction |
74+
| `NO_COLOR` | Industry standard to disable colors |
75+
76+
## Output Streams
77+
78+
By default, logs go to **stderr** so that command output (data, IDs, etc.) can be piped cleanly:
79+
80+
```bash
81+
# Logs go to stderr, JSON output goes to stdout
82+
b2c sites list --json 2>/dev/null | jq '.sites[0].id'
83+
```
84+
85+
To send logs to stdout instead:
86+
87+
```bash
88+
SFCC_LOG_TO_STDOUT=1 b2c code deploy
89+
```
90+
91+
## Secret Redaction
92+
93+
Sensitive fields are automatically redacted from log output:
94+
95+
- `password`, `secret`, `token`
96+
- `client_secret`, `access_token`, `refresh_token`
97+
- `api_key`, `authorization`
98+
99+
```
100+
[18:31:58] INFO: Authenticating
101+
client_id: "my-client"
102+
client_secret: "[REDACTED]"
103+
```
104+
105+
To disable redaction (for debugging):
106+
107+
```bash
108+
SFCC_REDACT_SECRETS=false b2c code deploy --debug
109+
```
110+
111+
## CI/CD Usage
112+
113+
For CI/CD pipelines, use JSON output and disable colors:
114+
115+
```bash
116+
NO_COLOR=1 b2c code deploy --json 2>&1 | tee deploy.log
117+
```
118+
119+
Or explicitly set the log level:
120+
121+
```bash
122+
SFCC_LOG_LEVEL=info b2c code deploy --json
123+
```

packages/b2c-cli/bin/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env -S node --import tsx
1+
#!/usr/bin/env -S node --conditions development --import tsx
22

33
import {execute} from '@oclif/core';
44

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {BaseCommand} from '@salesforce/b2c-tooling/cli';
2+
3+
export default class Test extends BaseCommand<typeof Test> {
4+
static description = 'Test logging output';
5+
static hidden = true;
6+
7+
async run(): Promise<void> {
8+
// Test this.log() which now uses pino
9+
this.log('Using this.log() - goes through pino');
10+
this.warn('Using this.warn() - goes through pino');
11+
12+
// Test logger directly at different levels
13+
this.logger.trace('Trace level message');
14+
this.logger.debug('Debug level message');
15+
this.logger.info('Info level message');
16+
this.logger.error('Error level message');
17+
18+
// Context (visible in debug mode)
19+
this.logger.info({operation: 'test', duration: 123}, 'Message with context');
20+
21+
this.logger.debug(
22+
{
23+
file: 'cartridge.zip',
24+
bytes: 45_678,
25+
instance: 'dev01.sandbox.us01.dx.commercecloud.salesforce.com',
26+
},
27+
'Debug with multiple context fields',
28+
);
29+
30+
// Test redaction
31+
this.logger.info(
32+
{
33+
username: 'testuser',
34+
password: 'secret123',
35+
client_secret: 'abc123xyz', // eslint-disable-line camelcase
36+
accessToken: 'eyJhbGciOiJIUzI1NiJ9.test',
37+
},
38+
'This should have redacted fields',
39+
);
40+
41+
// Child logger
42+
const childLogger = this.logger.child({operation: 'upload'});
43+
childLogger.info('Message from child logger');
44+
}
45+
}

packages/b2c-cli/src/commands/hello/index.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/b2c-cli/src/commands/hello/world.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/b2c-cli/src/i18n/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* this.log(t('commands.sites.list.fetching', 'Fetching sites from {{hostname}}...', { hostname }))
1010
*/
1111

12-
import {registerTranslations, t as toolingT, TOptions} from '@salesforce/b2c-tooling';
12+
import {registerTranslations, t as toolingT, type TOptions} from '@salesforce/b2c-tooling';
1313
import {locales} from './locales/index.js';
1414

1515
/** The namespace used by b2c-cli messages */

packages/b2c-cli/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ export {
1010
// Config utilities
1111
loadConfig,
1212
findDwJson,
13-
ResolvedConfig,
14-
LoadConfigOptions,
1513
} from '@salesforce/b2c-tooling/cli';
14+
export type {ResolvedConfig, LoadConfigOptions} from '@salesforce/b2c-tooling/cli';

0 commit comments

Comments
 (0)