Skip to content

Commit 43becf1

Browse files
authored
feat: Implement logger (#21)
Fixes #12
1 parent 85398cb commit 43becf1

File tree

8 files changed

+296
-15
lines changed

8 files changed

+296
-15
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"plugin:unicorn/recommended"
1111
],
1212
"rules": {
13+
"no-console": "error",
1314
"@typescript-eslint/no-unused-vars": 0
1415
},
1516
"overrides": [

package-lock.json

Lines changed: 244 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"@apache-arrow/esnext-esm": "^12.0.1",
7474
"@cloudquery/plugin-pb-javascript": "^0.0.6",
7575
"boolean": "^3.2.0",
76+
"winston": "^3.10.0",
7677
"yargs": "^17.7.2"
7778
}
7879
}

src/grpc/discovery.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import winston from 'winston';
12
import grpc = require('@grpc/grpc-js');
23
import { discovery1 } from '@cloudquery/plugin-pb-javascript';
34

5+
const SUPPORTED_VERSIONS = [3];
6+
47
export class DiscoveryServer extends discovery1.cloudquery.discovery.v1.UnimplementedDiscoveryService {
58
GetVersions(
69
call: grpc.ServerUnaryCall<
@@ -9,6 +12,9 @@ export class DiscoveryServer extends discovery1.cloudquery.discovery.v1.Unimplem
912
>,
1013
callback: grpc.sendUnaryData<discovery1.cloudquery.discovery.v1.GetVersions.Response>,
1114
): void {
12-
return callback(null, new discovery1.cloudquery.discovery.v1.GetVersions.Response({ versions: [3] }));
15+
return callback(
16+
null,
17+
new discovery1.cloudquery.discovery.v1.GetVersions.Response({ versions: SUPPORTED_VERSIONS }),
18+
);
1319
}
1420
}

src/grpc/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import winston from 'winston';
12
import grpc = require('@grpc/grpc-js');
23
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';
34

src/grpc/server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import winston from 'winston';
12
import grpc = require('@grpc/grpc-js');
23
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';
34
import { discovery1 } from '@cloudquery/plugin-pb-javascript';
@@ -12,10 +13,14 @@ export const getServer = () => {
1213
return server;
1314
};
1415

15-
export const startServer = (address: string) => {
16+
export const startServer = (logger: winston.Logger, address: string) => {
1617
const server = getServer();
1718
server.bindAsync(address, grpc.ServerCredentials.createInsecure(), (error, port) => {
19+
if (error) {
20+
logger.error(error);
21+
return;
22+
}
1823
server.start();
19-
console.log('server running on port', port);
24+
logger.info('server running on port', port);
2025
});
2126
};

src/logger/logger.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import winston from 'winston';
2+
3+
export enum LogLevel {
4+
TRACE = 'trace',
5+
DEBUG = 'debug',
6+
INFO = 'info',
7+
WARN = 'warn',
8+
ERROR = 'error',
9+
}
10+
11+
export enum LogFormat {
12+
JSON = 'json',
13+
TEXT = 'text',
14+
}
15+
16+
export const createLogger = (level: LogLevel, format: LogFormat) => {
17+
// Winston doesn't have a TRACE level, so we need to normalize it to DEBUG.
18+
const normalizedLevel = level === LogLevel.TRACE ? LogLevel.DEBUG : level;
19+
const logger = winston.createLogger({
20+
level: normalizedLevel,
21+
format: format == LogFormat.JSON ? winston.format.json() : winston.format.simple(),
22+
transports: [new winston.transports.Console()],
23+
});
24+
25+
return logger;
26+
};

src/serve/serve.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import yargs from 'yargs';
22
import { hideBin } from 'yargs/helpers';
33
import { startServer } from '../grpc/server.js';
4+
import { LogFormat, LogLevel, createLogger } from '../logger/logger.js';
45

56
const NETWORK_CHOICES = ['tcp', 'tcp4', 'tcp6', 'unix', 'unixpacket'] as const;
6-
const LOG_LEVEL_CHOICES = ['trace', 'debug', 'info', 'warn', 'error'] as const;
7-
const LOG_FORMAT_CHOICES = ['json', 'text'] as const;
7+
88
const TELEMETRY_LEVEL_CHOICES = ['none', 'errors', 'stats', 'all'] as const;
99

1010
export type ServeArguments = {
1111
address: string;
1212
network: (typeof NETWORK_CHOICES)[number];
13-
logLevel: (typeof LOG_LEVEL_CHOICES)[number];
14-
logFormat: (typeof LOG_FORMAT_CHOICES)[number];
13+
logLevel: LogLevel;
14+
logFormat: LogFormat;
1515
sentry: boolean;
1616
otelEndpoint: string;
1717
otelEndpointInsecure: boolean;
@@ -24,8 +24,9 @@ export const serve = yargs(hideBin(process.argv))
2424
'start plugin gRPC server',
2525
() => {},
2626
({ address, network, logLevel, logFormat, sentry: sentry, otelEndpoint, telemetryLevel }: ServeArguments) => {
27-
console.log({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel });
28-
startServer(address);
27+
const logger = createLogger(logLevel, logFormat);
28+
logger.info(JSON.stringify({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel }));
29+
startServer(logger, address);
2930
},
3031
)
3132
.options({
@@ -45,14 +46,14 @@ export const serve = yargs(hideBin(process.argv))
4546
'log-level': {
4647
alias: 'l',
4748
type: 'string',
48-
choices: LOG_LEVEL_CHOICES,
49+
choices: Object.values(LogLevel),
4950
description: 'log level',
5051
default: 'info',
5152
},
5253
'log-format': {
5354
alias: 'f',
5455
type: 'string',
55-
choices: LOG_FORMAT_CHOICES,
56+
choices: Object.values(LogFormat),
5657
description: 'log format',
5758
default: 'text',
5859
},

0 commit comments

Comments
 (0)