Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat(core): logging improvements (#97)
Browse files Browse the repository at this point in the history
Reduced Debug DB logging and formatting SQL queries.
  • Loading branch information
AuHau authored May 15, 2020
1 parent cd5aa77 commit c177ec3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"reflect-metadata": "^0.1.13",
"sequelize": "^5.21.7",
"sequelize-typescript": "^1.1.0",
"sql-formatter": "^2.3.3",
"web3-core-subscriptions": "^1.2.6",
"web3-eth": "^1.2.6",
"web3-eth-contract": "^1.2.6",
Expand All @@ -103,6 +104,7 @@
"@types/node-fetch": "^2.5.7",
"@types/serve-favicon": "^2.5.0",
"@types/sinon": "^9.0.0",
"@types/sql-formatter": "^2.3.0",
"@types/validator": "^13.0.0",
"bignumber.js": "^9.0.0",
"chai": "^4.2.0",
Expand Down
18 changes: 15 additions & 3 deletions src/sequelize.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { Sequelize, SequelizeOptions } from 'sequelize-typescript'
import { Application } from './definitions'
import { loggingFactory } from './logger'
import path from 'path'
import config from 'config'
import sqlFormatter from 'sql-formatter'

import { Application } from './definitions'
import { loggingFactory } from './logger'

const logger = loggingFactory('db')

function formatLogs (msg: string): string {
const result = msg.match(/^Executing \((\w+)\): (.*)/m)

if (!result) {
return msg
}

return `Executing SQL (${result[1]}):\n${sqlFormatter.format(result[2])}`
}

export function sequelizeFactory (): Sequelize {
const dbSettings: SequelizeOptions = {
models: [path.join(__dirname, '/**/*.model.+(ts|js)')],
modelMatch: (filename: string, member: string): boolean => {
return filename.substring(0, filename.indexOf('.model')) === member.toLowerCase()
},
logging: logger.debug
logging: (msg) => logger.debug(formatLogs(msg))
}

return new Sequelize(config.get('db'), dbSettings)
Expand Down

0 comments on commit c177ec3

Please sign in to comment.