Skip to content

Commit

Permalink
fix: clarify default logging
Browse files Browse the repository at this point in the history
By default it will log Operations,
except when in CI or in AppEngine.
  • Loading branch information
kirillgroshkov committed Nov 10, 2021
1 parent 5cd5150 commit 335c903
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
5 changes: 1 addition & 4 deletions src/adapter/cachedb/cache.db.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions src/adapter/cachedb/cache.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -28,7 +26,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
constructor(cfg: CacheDBCfg) {
super()
this.cfg = {
logger: isGAE ? undefined : console,
logger: console,
...cfg,
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/adapter/file/file.db.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export interface FileDBCfg {
sortObjects?: boolean

/**
* Defaults to `console` in dev.
* Default to noop in AppEngine.
* Defaults to `console`.
*/
logger?: CommonLogger

Expand Down
4 changes: 1 addition & 3 deletions src/adapter/file/file.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,7 +44,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
this.cfg = {
sortObjects: true,
logFinished: true,
logger: isGAE ? undefined : console,
logger: console,
...cfg,
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/adapter/inmemory/inMemory.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InMemoryDBCfg>) {
this.cfg = {
Expand All @@ -81,7 +78,7 @@ export class InMemoryDB implements CommonDB {
persistenceEnabled: false,
persistZip: true,
persistentStoragePath: './tmp/inmemorydb',
logger: isGAE ? undefined : console,
logger: console,
...cfg,
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -71,10 +80,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, 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

Expand Down
34 changes: 18 additions & 16 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
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,
JoiValidationError,
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'
Expand All @@ -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.
Expand All @@ -62,9 +63,12 @@ export class CommonDao<
> {
constructor(public cfg: CommonDaoCfg<BM, DBM, TM>) {
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(),
Expand Down Expand Up @@ -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()
// }
Expand All @@ -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`
Expand All @@ -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 {
Expand Down

0 comments on commit 335c903

Please sign in to comment.