Skip to content

Commit

Permalink
feat: add custom logger examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zeruk committed Mar 7, 2023
1 parent fb77499 commit 11273aa
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import {main} from '../utils';
import {Logger, LogFn, Driver, getCredentialsFromEnv, setDefaultLogger} from 'ydb-sdk';
import {Logger, LogFn, Driver, getCredentialsFromEnv, setupLogger} from 'ydb-sdk';

const logFunction: LogFn = (obj: any, ...args: any[]) => {
console.log('Custom logging!', obj, ...args)
console.log('Custom logging!', obj, ...args);
};
const MyLogger: Logger = {
fatal: logFunction,
error: logFunction,
warn: logFunction,
info: logFunction,
debug: logFunction,
trace: logFunction
}

setDefaultLogger(MyLogger)
trace: logFunction,
};

export async function run(logger: Logger, endpoint: string, database: string) {
await setupLogger(MyLogger);
logger.info('Driver initializing...');
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const timeout = 10000;
if (!await driver.ready(timeout)) {
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
process.exit(1);
}
logger.info('Done');
driver.destroy()

logger.info('Done, destroy driver');
await driver.destroy();
process.exit(0);
}

main(run);
18 changes: 18 additions & 0 deletions examples/custom-logger/fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {main} from '../utils';
import {Logger, Driver, getCredentialsFromEnv} from 'ydb-sdk';

export async function run(logger: Logger, endpoint: string, database: string) {
logger.info('Driver initializing...');
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const timeout = 10000;
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
process.exit(1);
}
logger.info('Done, destroy driver');
await driver.destroy();
process.exit(0);
}

main(run);
21 changes: 21 additions & 0 deletions examples/custom-logger/pino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {mainWithoutLogger} from '../utils';
import {Driver, getCredentialsFromEnv, setupLogger} from 'ydb-sdk';
import pino from 'pino';

export async function run(endpoint: string, database: string) {
const logger = pino({level: 'debug'});
logger.info('Driver initializing...');
setupLogger(logger);
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const timeout = 10000;
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
process.exit(1);
}
logger.info('Done, destroy driver');
await driver.destroy();
process.exit(0);
}

mainWithoutLogger(run);
41 changes: 41 additions & 0 deletions examples/custom-logger/winston.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {mainWithoutLogger} from '../utils';
import {Driver, getCredentialsFromEnv, Logger, setupLogger} from 'ydb-sdk';
import winston from 'winston';

export async function run(endpoint: string, database: string) {
const logger: Logger = winston.createLogger({
level: 'debug',
format: winston.format.json(),
defaultMeta: {service: 'user-service'},
transports: [new winston.transports.Console({format: winston.format.simple()})],
levels: {
...winston.config.npm.levels,
fatal: 0,
trace: 6,
},
}) as unknown as Logger;
/*
Log levels accordance:
YDB-SDK-logger -> winston (npm levels)
fatal -> !!! no such level, set exact as error
error -> error
warn -> warn
info -> info
debug -> debug
trace -> silly(6)
*/
setupLogger(logger);
logger.info('Driver initializing...');
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const timeout = 10000;
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
process.exit(1);
}
logger.info('Done, destroy driver');
await driver.destroy();
process.exit(0);
}

mainWithoutLogger(run);
4 changes: 3 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"url-shortener": "node build/url-shortener/index.js"
},
"dependencies": {
"pino": "^8.11.0",
"winston": "^3.8.2",
"yargs": "^16.1.0",
"ydb-sdk": "file:.."
},
Expand All @@ -31,4 +33,4 @@
"express": "^4.17.1",
"typescript": "^4.6.4"
}
}
}
30 changes: 24 additions & 6 deletions examples/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import yargs from 'yargs';
export interface Runner {
(logger: Logger, endpoint: string, database: string, cliParams?: any): Promise<void>;
}
export interface RunnerWithoutLogger {
(endpoint: string, database: string, cliParams?: any): Promise<void>;
}

export interface Option {
key: string;
name: string;
description: string;
}

export async function main(runner: Runner, options?: Option[]) {
export function getCliParams(options?: Option[]) {
const optionsUsage = options && options.length > 0 ? options.map((option) => ` --${option.name}`).join('') : '';

const argsBuilder = yargs
Expand Down Expand Up @@ -55,15 +58,30 @@ export async function main(runner: Runner, options?: Option[]) {
options?.forEach((option) => {
cliParams[option.key] = args[option.key] as string;
});
return {endpoint, db, cliParams}
}

const logger = getLogger();
logger.info(`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`);

export async function mainWithoutLogger(runner: RunnerWithoutLogger, options?: Option[]) {
const {db, endpoint, cliParams} = getCliParams(options)
console.log(`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`)
try {
await runner(logger, endpoint, db, cliParams);
await runner(endpoint, db, cliParams);
} catch (error) {
logger.error(error as object);
console.error(error as object);
}
}

export async function main(runner: Runner, options?: Option[]) {
const {db, endpoint, cliParams} = getCliParams(options)

const logger = getLogger();
logger.info(`Running basic-example script against endpoint '${endpoint}' and database '${db}'.`);

try {
await runner(logger, endpoint, db, cliParams);
} catch (error) {
logger.error(error as object);
}
}

export const SYNTAX_V1 = '--!syntax_v1';

0 comments on commit 11273aa

Please sign in to comment.