Skip to content

Commit c2dab96

Browse files
committed
refactor(logging): cleanup, use format specifiers
fix #342
1 parent e03e409 commit c2dab96

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ The `neovim` package provides these functions:
3333
- Best practice in any case is to use the `logger` available from the `NeovimClient` returned by
3434
`attach()`, instead of `console` logging functions.
3535
- Set the `$NVIM_NODE_LOG_FILE` env var to (also) write logs to a file.
36-
- Set the `$ALLOW_CONSOLE` env var to (also) write logs to stdout.
36+
- Set the `$ALLOW_CONSOLE` env var to (also) write logs to stdout. **This will break any (stdio) RPC
37+
channel** because logs written to stdout are invalid RPC messages.
3738

3839
### Quickstart: connect to Nvim
3940

@@ -48,6 +49,9 @@ Following is a complete, working example.
4849
ALLOW_CONSOLE=1 node demo.mjs
4950
```
5051
- `$ALLOW_CONSOLE` env var must be set, because logs are normally not printed to stdout.
52+
- Note: `$ALLOW_CONSOLE` is only for demo purposes. It cannot be used for remote plugins or
53+
whenever stdio is an RPC channel, because writing logs to stdout would break the RPC
54+
channel.
5155
- Script:
5256
```js
5357
import * as child_process from 'node:child_process'

packages/neovim/src/api/client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,26 @@ export class NeovimClient extends Neovim {
6767
resp: any,
6868
...restArgs: any[]
6969
) {
70-
this.logger.info('handleRequest: ', method);
7170
// If neovim API is not generated yet and we are not handle a 'specs' request
7271
// then queue up requests
7372
//
7473
// Otherwise emit as normal
7574
if (!this.isApiReady && method !== 'specs') {
75+
this.logger.info('handleRequest (queued): %s', method);
7676
this.requestQueue.push({
7777
type: 'request',
7878
args: [method, args, resp, ...restArgs],
7979
});
8080
} else {
81+
this.logger.info('handleRequest: %s', method);
8182
this.emit('request', method, args, resp);
8283
}
8384
}
8485

8586
emitNotification(method: string, args: any[]) {
8687
if (method.endsWith('_event')) {
8788
if (!method.startsWith('nvim_buf_')) {
88-
this.logger.error('Unhandled event: ', method);
89+
this.logger.error('Unhandled event: %s', method);
8990
return;
9091
}
9192
const shortName = method.replace(REGEX_BUF_EVENT, '$1');
@@ -112,7 +113,7 @@ export class NeovimClient extends Neovim {
112113
}
113114

114115
handleNotification(method: string, args: VimValue[], ...restArgs: any[]) {
115-
this.logger.info('handleNotification: ', method);
116+
this.logger.info('handleNotification: %s', method);
116117
// If neovim API is not generated yet then queue up requests
117118
//
118119
// Otherwise emit as normal
@@ -198,7 +199,7 @@ export class NeovimClient extends Neovim {
198199

199200
return true;
200201
} catch (err) {
201-
this.logger.error(`Could not dynamically generate neovim API: ${err}`, {
202+
this.logger.error(`Could not dynamically generate neovim API: %s: %O`, err.name, {
202203
error: err,
203204
});
204205
this.logger.error(err.stack);

packages/neovim/src/host/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as util from 'node:util';
21
import { attach } from '../attach';
32
import { loadPlugin, LoadPluginOptions } from './factory';
43
import { NvimPlugin } from './NvimPlugin';
@@ -52,7 +51,7 @@ export class Host {
5251
async handlePlugin(method: string, args: any[]) {
5352
// ignore methods that start with nvim_ prefix (e.g. when attaching to buffer and listening for notifications)
5453
if (method.startsWith('nvim_')) return null;
55-
this.nvim?.logger.debug('host.handlePlugin: ', method);
54+
this.nvim?.logger.debug('host.handlePlugin: %s', method);
5655

5756
// Parse method name
5857
const procInfo = method.split(':');
@@ -90,11 +89,11 @@ export class Host {
9089
const specs = (plugin && plugin.specs) || [];
9190
this.nvim?.logger.debug(JSON.stringify(specs));
9291
res.send(specs);
93-
this.nvim?.logger.debug(`specs: ${util.inspect(specs)}`);
92+
this.nvim?.logger.debug('specs: %O', specs);
9493
}
9594

9695
async handler(method: string, args: any[], res: Response) {
97-
this.nvim?.logger.debug('request received: ', method);
96+
this.nvim?.logger.debug('request received: %s', method);
9897
// 'poll' and 'specs' are requests by neovim,
9998
// otherwise it will
10099
if (method === 'poll') {

0 commit comments

Comments
 (0)