From 25b375d6ac6f359da22f0f82e342b65d123fa377 Mon Sep 17 00:00:00 2001 From: ssube Date: Thu, 13 Aug 2020 23:07:47 -0500 Subject: [PATCH] feat(logger): add bunyan without DI --- .../cautious-journey.remoteoptions.logger.md | 11 +++++++++ docs/api/cautious-journey.remoteoptions.md | 1 + .../cautious-journey.syncoptions.logger.md | 11 +++++++++ docs/api/cautious-journey.syncoptions.md | 1 + src/config/index.ts | 6 +++++ src/logger/BunyanLogger.ts | 20 ++++++++++++++++ src/main.ts | 10 ++++---- src/remote/github.ts | 4 ++-- src/remote/index.ts | 4 ++++ src/sync.ts | 24 +++++++++++-------- 10 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 docs/api/cautious-journey.remoteoptions.logger.md create mode 100644 docs/api/cautious-journey.syncoptions.logger.md create mode 100644 src/logger/BunyanLogger.ts diff --git a/docs/api/cautious-journey.remoteoptions.logger.md b/docs/api/cautious-journey.remoteoptions.logger.md new file mode 100644 index 0000000..77e8748 --- /dev/null +++ b/docs/api/cautious-journey.remoteoptions.logger.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [cautious-journey](./cautious-journey.md) > [RemoteOptions](./cautious-journey.remoteoptions.md) > [logger](./cautious-journey.remoteoptions.logger.md) + +## RemoteOptions.logger property + +Signature: + +```typescript +logger: Logger; +``` diff --git a/docs/api/cautious-journey.remoteoptions.md b/docs/api/cautious-journey.remoteoptions.md index 1a14cdd..82e4104 100644 --- a/docs/api/cautious-journey.remoteoptions.md +++ b/docs/api/cautious-journey.remoteoptions.md @@ -16,5 +16,6 @@ export interface RemoteOptions | --- | --- | --- | | [data](./cautious-journey.remoteoptions.data.md) | Record<string, string> | Arbitrary key-value data for this remote, usually credentials and base URLs. | | [dryrun](./cautious-journey.remoteoptions.dryrun.md) | boolean | If set, do not make any real changes. | +| [logger](./cautious-journey.remoteoptions.logger.md) | Logger | | | [type](./cautious-journey.remoteoptions.type.md) | string | Remote class/type name. | diff --git a/docs/api/cautious-journey.syncoptions.logger.md b/docs/api/cautious-journey.syncoptions.logger.md new file mode 100644 index 0000000..e275254 --- /dev/null +++ b/docs/api/cautious-journey.syncoptions.logger.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [cautious-journey](./cautious-journey.md) > [SyncOptions](./cautious-journey.syncoptions.md) > [logger](./cautious-journey.syncoptions.logger.md) + +## SyncOptions.logger property + +Signature: + +```typescript +logger: Logger; +``` diff --git a/docs/api/cautious-journey.syncoptions.md b/docs/api/cautious-journey.syncoptions.md index caa118b..d6d7900 100644 --- a/docs/api/cautious-journey.syncoptions.md +++ b/docs/api/cautious-journey.syncoptions.md @@ -15,6 +15,7 @@ export interface SyncOptions | Property | Type | Description | | --- | --- | --- | | [flags](./cautious-journey.syncoptions.flags.md) | Array<[FlagLabel](./cautious-journey.flaglabel.md)> | | +| [logger](./cautious-journey.syncoptions.logger.md) | Logger | | | [project](./cautious-journey.syncoptions.project.md) | string | | | [remote](./cautious-journey.syncoptions.remote.md) | [Remote](./cautious-journey.remote.md) | | | [states](./cautious-journey.syncoptions.states.md) | Array<[StateLabel](./cautious-journey.statelabel.md)> | | diff --git a/src/config/index.ts b/src/config/index.ts index 1777258..64206cf 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,3 +1,5 @@ +import { LogLevel } from 'noicejs'; + import { FlagLabel, StateLabel } from '../labels'; import { RemoteOptions } from '../remote'; @@ -5,6 +7,10 @@ import { RemoteOptions } from '../remote'; * Config data for the app, loaded from CLI or DOM. */ export interface ConfigData { + logger: { + level: LogLevel; + name: string; + }; projects: Array<{ /** * Color palette for labels without their own. diff --git a/src/logger/BunyanLogger.ts b/src/logger/BunyanLogger.ts new file mode 100644 index 0000000..b5457f9 --- /dev/null +++ b/src/logger/BunyanLogger.ts @@ -0,0 +1,20 @@ +import { constructorName } from '@apextoaster/js-utils'; +import bunyan from 'bunyan'; +import { Logger } from 'noicejs'; + +/** + * Attach bunyan to the Logger. Does very little, since bunyan matches the Logger interface. + */ +export class BunyanLogger { + public static create(options: bunyan.LoggerOptions): Logger { + return bunyan.createLogger({ + ...options, + serializers: { + ...bunyan.stdSerializers, + container: constructorName, + logger: constructorName, + module: constructorName, + }, + }); + } +} diff --git a/src/main.ts b/src/main.ts index dac3477..32ec15f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,7 @@ import { join } from 'path'; import { ConfigData } from './config'; import { Commands, createParser } from './config/args'; +import { BunyanLogger } from './logger/BunyanLogger'; import { GithubRemote } from './remote/github'; import { syncIssues, syncLabels, SyncOptions } from './sync'; import { VERSION_INFO } from './version'; @@ -46,26 +47,27 @@ export async function main(argv: Array): Promise { const parser = createParser((argMode) => mode = argMode as Commands); const args = parser.parse(argv.slice(SLICE_ARGS)); const config = await loadConfig(args.config); - // TODO: create logger + const logger = BunyanLogger.create(config.logger); - /* eslint-disable-next-line no-console */ - console.log({ + logger.info({ args, config, mode, version: VERSION_INFO, - }); + }, 'startup environment'); for (const project of config.projects) { const remote = new GithubRemote({ ...project.remote, dryrun: args.dryrun, + logger, }); await remote.connect(); // mode switch const options: SyncOptions = { flags: project.flags, + logger, project: project.name, remote, states: project.states, diff --git a/src/remote/github.ts b/src/remote/github.ts index c369473..3d084a9 100644 --- a/src/remote/github.ts +++ b/src/remote/github.ts @@ -18,11 +18,11 @@ export class GithubRemote implements Remote { constructor(options: RemoteOptions) { this.options = options; - /* eslint-disable-next-line */ - console.log('options:', options); + options.logger.debug({ options }, 'github remote'); } public async connect() { + this.options.logger.info('connecting to github'); this.request = new Octokit({ auth: { id: parseInt(mustExist(this.options.data.id), 10), diff --git a/src/remote/index.ts b/src/remote/index.ts index 7dba3b2..00afa65 100644 --- a/src/remote/index.ts +++ b/src/remote/index.ts @@ -1,3 +1,5 @@ +import { Logger } from 'noicejs'; + export interface ProjectQuery { project: string; } @@ -35,6 +37,8 @@ export interface RemoteOptions { */ dryrun: boolean; + logger: Logger; + /** * Remote class/type name. */ diff --git a/src/sync.ts b/src/sync.ts index e95fa41..b9b5f9b 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -1,15 +1,14 @@ import { doesExist, mustExist } from '@apextoaster/js-utils'; +import { Logger } from 'noicejs'; import { FlagLabel, getLabelNames, StateLabel, valueName } from './labels'; import { LabelUpdate, Remote } from './remote'; import { resolveLabels } from './resolve'; import { defaultTo } from './utils'; -// TODO: turn this back on/remove the disable pragma -/* eslint-disable no-console */ - export interface SyncOptions { flags: Array; + logger: Logger; project: string; remote: Remote; states: Array; @@ -21,7 +20,7 @@ export async function syncIssues(options: SyncOptions): Promise { }); for (const issue of issues) { - console.log('issue:', issue); + options.logger.info({ issue }, 'issue'); const resolution = resolveLabels({ flags: options.flags, @@ -31,7 +30,7 @@ export async function syncIssues(options: SyncOptions): Promise { // TODO: prompt user to update this particular issue if (resolution.changes.length > 0) { - console.log('updating issue:', issue, resolution); + options.logger.info({ issue, resolution }, 'updating issue'); await options.remote.updateIssue({ ...issue, labels: resolution.labels, @@ -54,14 +53,19 @@ export async function syncLabels(options: SyncOptions): Promise { for (const label of combined) { const exists = present.has(label); const expected = desired.has(label); - console.log('label:', label, exists, expected); + + options.logger.info({ + exists, + expected, + label, + }, 'label'); if (exists) { if (expected) { const data = mustExist(labels.find((l) => l.name === label)); await syncSingleLabel(options, data); } else { - console.log('remove label:', label); + options.logger.warn({ label }, 'remove label'); await options.remote.deleteLabel({ name: label, project: options.project, @@ -69,7 +73,7 @@ export async function syncLabels(options: SyncOptions): Promise { } } else { if (expected) { - console.log('create label:', label); + options.logger.info({ label }, 'create label'); await createLabel(options, label); } else { // skip @@ -122,11 +126,11 @@ export async function syncLabelDiff(options: SyncOptions, current: LabelUpdate, project: options.project, }; - console.log('update label:', current, expected, body); + options.logger.debug({ body, current, expected }, 'update label'); const resp = await options.remote.updateLabel(body); - console.log('update resp:', resp); + options.logger.debug({ resp }, 'update resp'); } }