From 335c903b22ab38f224406a39252c1f592017a01d Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Wed, 10 Nov 2021 23:30:57 +0100 Subject: [PATCH] fix: clarify default logging By default it will log Operations, except when in CI or in AppEngine. --- src/adapter/cachedb/cache.db.model.ts | 5 +--- src/adapter/cachedb/cache.db.ts | 4 +--- src/adapter/file/file.db.model.ts | 3 +-- src/adapter/file/file.db.ts | 4 +--- src/adapter/inmemory/inMemory.db.ts | 7 ++---- src/commondao/common.dao.model.ts | 14 +++++++---- src/commondao/common.dao.ts | 34 ++++++++++++++------------- 7 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/adapter/cachedb/cache.db.model.ts b/src/adapter/cachedb/cache.db.model.ts index d6df494..c2345fe 100644 --- a/src/adapter/cachedb/cache.db.model.ts +++ b/src/adapter/cachedb/cache.db.model.ts @@ -40,10 +40,7 @@ export interface CacheDBCfg { logDownstream?: boolean /** - * Pass noopLogger (or undefined) to skip logging completely. - * - * Defaults to `console` in dev. - * Default to noop in AppEngine. + * Defaults to `console`. */ logger?: CommonLogger } diff --git a/src/adapter/cachedb/cache.db.ts b/src/adapter/cachedb/cache.db.ts index 43514f3..ee6979b 100644 --- a/src/adapter/cachedb/cache.db.ts +++ b/src/adapter/cachedb/cache.db.ts @@ -16,8 +16,6 @@ import { CacheDBStreamOptions, } from './cache.db.model' -const isGAE = !!process.env['GAE_INSTANCE'] - /** * CommonDB implementation that proxies requests to downstream CommonDB * and does in-memory caching. @@ -28,7 +26,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB { constructor(cfg: CacheDBCfg) { super() this.cfg = { - logger: isGAE ? undefined : console, + logger: console, ...cfg, } } diff --git a/src/adapter/file/file.db.model.ts b/src/adapter/file/file.db.model.ts index aec9312..1877a36 100644 --- a/src/adapter/file/file.db.model.ts +++ b/src/adapter/file/file.db.model.ts @@ -24,8 +24,7 @@ export interface FileDBCfg { sortObjects?: boolean /** - * Defaults to `console` in dev. - * Default to noop in AppEngine. + * Defaults to `console`. */ logger?: CommonLogger diff --git a/src/adapter/file/file.db.ts b/src/adapter/file/file.db.ts index 6d35fcd..c720cdd 100644 --- a/src/adapter/file/file.db.ts +++ b/src/adapter/file/file.db.ts @@ -28,8 +28,6 @@ import { DBQuery } from '../../query/dbQuery' import { DBTransaction } from '../../transaction/dbTransaction' import { FileDBCfg } from './file.db.model' -const isGAE = !!process.env['GAE_INSTANCE'] - /** * Provides barebone implementation for "whole file" based CommonDB. * "whole file" means that the persistence layer doesn't allow any querying, @@ -46,7 +44,7 @@ export class FileDB extends BaseCommonDB implements CommonDB { this.cfg = { sortObjects: true, logFinished: true, - logger: isGAE ? undefined : console, + logger: console, ...cfg, } } diff --git a/src/adapter/inmemory/inMemory.db.ts b/src/adapter/inmemory/inMemory.db.ts index fadd662..14a593b 100644 --- a/src/adapter/inmemory/inMemory.db.ts +++ b/src/adapter/inmemory/inMemory.db.ts @@ -65,14 +65,11 @@ export interface InMemoryDBCfg { persistZip: boolean /** - * Defaults to `console` in dev. - * Default to noop in AppEngine. + * Defaults to `console`. */ logger?: CommonLogger } -const isGAE = !!process.env['GAE_INSTANCE'] - export class InMemoryDB implements CommonDB { constructor(cfg?: Partial) { this.cfg = { @@ -81,7 +78,7 @@ export class InMemoryDB implements CommonDB { persistenceEnabled: false, persistZip: true, persistentStoragePath: './tmp/inmemorydb', - logger: isGAE ? undefined : console, + logger: console, ...cfg, } } diff --git a/src/commondao/common.dao.model.ts b/src/commondao/common.dao.model.ts index 2c9bb0e..a7ba078 100644 --- a/src/commondao/common.dao.model.ts +++ b/src/commondao/common.dao.model.ts @@ -46,8 +46,17 @@ export enum CommonDaoLogLevel { * Same as undefined */ NONE = 0, + /** + * Log operations (e.g "getById returned 1 row"), but not data + */ OPERATIONS = 10, + /** + * Log operations and data for single operations (e.g getById), but not batch operations. + */ DATA_SINGLE = 20, + /** + * Log EVERYTHING - all data passing in and out (max 10 rows). Very verbose! + */ DATA_FULL = 30, } @@ -71,10 +80,7 @@ export interface CommonDaoCfg, DBM extends Obje readOnly?: boolean /** - * Pass undefined (or noopLogger) to disable logging completely. - * - * Defaults to `console` in dev. - * Default to noop in AppEngine. + * Defaults to `console` */ logger?: CommonLogger diff --git a/src/commondao/common.dao.ts b/src/commondao/common.dao.ts index 7c87ac3..c266d86 100644 --- a/src/commondao/common.dao.ts +++ b/src/commondao/common.dao.ts @@ -1,20 +1,21 @@ import { + _filterNullishValues, + _filterUndefinedValues, + _passthroughPredicate, + _since, + _truncate, + _uniqBy, AppError, AsyncMapper, ErrorMode, JsonSchemaObject, - _passthroughPredicate, - _since, - _truncate, - pMap, JsonSchemaRootObject, - Saved, - _uniqBy, ObjectWithId, - _filterUndefinedValues, - _filterNullishValues, + pMap, + Saved, } from '@naturalcycles/js-lib' import { + _pipeline, AjvSchema, AjvValidationError, getValidationResult, @@ -22,14 +23,13 @@ import { ObjectSchemaTyped, ReadableTyped, stringId, + transformBuffer, transformLogProgress, transformMap, transformMapSimple, transformMapSync, transformTap, writableVoid, - _pipeline, - transformBuffer, } from '@naturalcycles/nodejs-lib' import { DBLibError } from '../cnst' import { DBModelType, RunQueryResult } from '../db.model' @@ -47,6 +47,7 @@ import { /* eslint-disable no-dupe-class-members */ const isGAE = !!process.env['GAE_INSTANCE'] +const isCI = !!process.env['CI'] /** * Lowest common denominator API between supported Databases. @@ -62,9 +63,12 @@ export class CommonDao< > { constructor(public cfg: CommonDaoCfg) { this.cfg = { - logLevel: CommonDaoLogLevel.OPERATIONS, + // Default is to NOT log in AppEngine and in CI, + // otherwise to log Operations + // e.g in Dev (local machine), Test - it will log operations (useful for debugging) + logLevel: isGAE || isCI ? CommonDaoLogLevel.NONE : CommonDaoLogLevel.OPERATIONS, createdUpdated: true, - logger: isGAE ? undefined : console, + logger: console, ...cfg, hooks: { createId: () => stringId(), @@ -979,8 +983,6 @@ export class CommonDao< await this.cfg.db.ping() } - // todo: logging - // todo: bmToDBM, etc. How? // transaction(): DBTransaction { // return this.cfg.db.transaction() // } @@ -994,7 +996,7 @@ export class CommonDao< if (Array.isArray(res)) { logRes = `${res.length} row(s)` if (res.length && this.cfg.logLevel >= CommonDaoLogLevel.DATA_FULL) { - args.push('\n', res.slice(0, 10)) // max 10 items + args.push('\n', ...res.slice(0, 10)) // max 10 items } } else if (res) { logRes = `1 row` @@ -1005,7 +1007,7 @@ export class CommonDao< logRes = `undefined` } - this.cfg.logger?.log(...[`<< ${table}.${op}: ${logRes} in ${_since(started)}`].concat(args)) + this.cfg.logger?.log(`<< ${table}.${op}: ${logRes} in ${_since(started)}`, ...args) } protected logSaveResult(started: number, op: string, table: string): void {