Skip to content

Commit

Permalink
feat(attach): allow custom logger for attach neovim proc (#138)
Browse files Browse the repository at this point in the history
* feat(attach): allow custom logger for attach neovim proc

* refactor(base): use typed logger interface
  • Loading branch information
kwonoj authored and rhysd committed Jan 9, 2020
1 parent 57ba042 commit d9bc2ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
9 changes: 5 additions & 4 deletions packages/neovim/src/api/Base.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { EventEmitter } from 'events';

import { Transport } from '../utils/transport';
import { logger as loggerModule } from '../utils/logger';
import { Logger } from '../utils/logger';
import { VimValue } from '../types/VimValue';

export interface BaseConstructorOptions {
transport?: Transport;
logger?: typeof loggerModule;
logger?: Logger;
data?: Buffer;
metadata?: any;
client?: any;
Expand All @@ -31,7 +31,7 @@ export class BaseApi extends EventEmitter {

protected prefix: string;

public logger: typeof loggerModule;
public logger: Logger;

public data: Buffer | number;

Expand All @@ -49,7 +49,8 @@ export class BaseApi extends EventEmitter {

this.setTransport(transport);
this.data = data;
this.logger = logger || loggerModule;
// eslint-disable-next-line global-require
this.logger = logger || require('../utils/logger').logger;
this.client = client;

if (metadata) {
Expand Down
10 changes: 8 additions & 2 deletions packages/neovim/src/attach/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import { createConnection } from 'net';
import * as child from 'child_process';

import { NeovimClient } from '../api/client';
import { logger } from '../utils/logger';
import { Logger } from '../utils/logger';

export interface Attach {
reader?: NodeJS.ReadableStream;
writer?: NodeJS.WritableStream;
proc?: NodeJS.Process | child.ChildProcess;
socket?: string;
options?: {
logger?: Logger;
};
}

export function attach({
reader: _reader,
writer: _writer,
proc,
socket,
options = {},
}: Attach) {
let writer;
let reader;
Expand All @@ -33,7 +37,9 @@ export function attach({
}

if (writer && reader) {
const neovim = new NeovimClient({ logger });
// eslint-disable-next-line global-require
const loggerInstance = options.logger || require('../utils/logger').logger; // lazy load to winston only if needed
const neovim = new NeovimClient({ logger: loggerInstance });
neovim.attach({
writer,
reader,
Expand Down
9 changes: 5 additions & 4 deletions packages/neovim/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as winston from 'winston';

const level = process.env.NVIM_NODE_LOG_LEVEL || 'debug';

const logger = winston.createLogger({
const winstonLogger = winston.createLogger({
level,
});

if (process.env.NVIM_NODE_LOG_FILE) {
logger.add(
winstonLogger.add(
new winston.transports.File({
filename: process.env.NVIM_NODE_LOG_FILE,
level,
Expand All @@ -16,11 +16,12 @@ if (process.env.NVIM_NODE_LOG_FILE) {
}

if (process.env.ALLOW_CONSOLE) {
logger.add(
winstonLogger.add(
new winston.transports.Console({
format: winston.format.simple(),
})
);
}

export { logger };
export type Logger = Pick<winston.Logger, 'info' | 'warn' | 'error' | 'debug'>;
export const logger: Logger = winstonLogger;

0 comments on commit d9bc2ef

Please sign in to comment.