Skip to content

Commit

Permalink
feat: trace level logging (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
njlie committed Jul 11, 2018
1 parent 86ab379 commit 5292ad4
Show file tree
Hide file tree
Showing 27 changed files with 150 additions and 103 deletions.
33 changes: 25 additions & 8 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion src/backends/ecb-plus-xrp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class ECBAndXRPBackend extends ECBBackend {
}
const body = await rateRes.json()
const rate = Number(body.rate).toFixed(5)
log.debug('loaded EUR/XRP rate. rate=%s', rate)
log.trace('loaded EUR/XRP rate. rate=%s', rate)
return Number(rate)
}
}
16 changes: 8 additions & 8 deletions src/backends/ecb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ export default class ECBBackend implements BackendInstance {
async connect () {
let apiData
if (this.mockData) {
log.debug('connect using mock data.')
log.info('connect using mock data.')
apiData = this.mockData
} else {
log.debug('connect. uri=' + this.ratesApiUrl)
log.info('connect. uri=' + this.ratesApiUrl)
let result = await fetchUri(this.ratesApiUrl)
apiData = await parseXMLResponse(await result.text())
}
this.rates = apiData.rates
this.rates[apiData.base] = 1
this.currencies = Object.keys(this.rates)
this.currencies.sort()
log.debug('data loaded. noCurrencies=' + this.currencies.length)
log.info('data loaded. numCurrencies=' + this.currencies.length)
}

_formatAmount (amount: string) {
Expand All @@ -90,11 +90,11 @@ export default class ECBBackend implements BackendInstance {
const destinationInfo = this.getInfo(destinationAccount)

if (!sourceInfo) {
log.warn('unable to fetch account info for source account. accountId=%s', sourceAccount)
log.error('unable to fetch account info for source account. accountId=%s', sourceAccount)
throw new Error('unable to fetch account info for source account. accountId=' + sourceAccount)
}
if (!destinationInfo) {
log.warn('unable to fetch account info for destination account. accountId=%s', destinationAccount)
log.error('unable to fetch account info for destination account. accountId=%s', destinationAccount)
throw new Error('unable to fetch account info for destination account. accountId=' + destinationAccount)
}

Expand All @@ -106,12 +106,12 @@ export default class ECBBackend implements BackendInstance {
const destinationRate = this.rates[destinationCurrency]

if (!sourceRate) {
log.warn('no rate available for source currency. currency=%s', sourceCurrency)
log.error('no rate available for source currency. currency=%s', sourceCurrency)
throw new Error('no rate available. currency=' + sourceCurrency)
}

if (!destinationRate) {
log.warn('no rate available for destination currency. currency=%s', destinationCurrency)
log.error('no rate available for destination currency. currency=%s', destinationCurrency)
throw new Error('no rate available. currency=' + destinationCurrency)
}

Expand All @@ -126,7 +126,7 @@ export default class ECBBackend implements BackendInstance {
.times(new BigNumber(1).minus(this.spread))
.toPrecision(15)

log.debug('quoted rate. from=%s to=%s fromCur=%s toCur=%s rate=%s spread=%s', sourceAccount, destinationAccount, sourceCurrency, destinationCurrency, rate, this.spread)
log.trace('quoted rate. from=%s to=%s fromCur=%s toCur=%s rate=%s spread=%s', sourceAccount, destinationAccount, sourceCurrency, destinationCurrency, rate, this.spread)

return Number(rate)
}
Expand Down
4 changes: 2 additions & 2 deletions src/backends/one-to-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export default class OneToOneBackend implements BackendInstance {
const destinationInfo = this.getInfo(destinationAccount)

if (!sourceInfo) {
log.warn('unable to fetch account info for source account. accountId=%s', sourceAccount)
log.error('unable to fetch account info for source account. accountId=%s', sourceAccount)
throw new Error('unable to fetch account info for source account. accountId=' + sourceAccount)
}
if (!destinationInfo) {
log.warn('unable to fetch account info for destination account. accountId=%s', destinationAccount)
log.error('unable to fetch account info for destination account. accountId=%s', destinationAccount)
throw new Error('unable to fetch account info for destination account. accountId=' + destinationAccount)
}

Expand Down
4 changes: 2 additions & 2 deletions src/backends/randomizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export default class RandomizerBackend implements BackendInstance {
const destinationInfo = this.getInfo(destinationAccount)

if (!sourceInfo) {
log.warn('unable to fetch account info for source account. accountId=%s', sourceAccount)
log.error('unable to fetch account info for source account. accountId=%s', sourceAccount)
throw new Error('unable to fetch account info for source account. accountId=' + sourceAccount)
}
if (!destinationInfo) {
log.warn('unable to fetch account info for destination account. accountId=%s', destinationAccount)
log.error('unable to fetch account info for destination account. accountId=%s', destinationAccount)
throw new Error('unable to fetch account info for destination account. accountId=' + destinationAccount)
}

Expand Down
37 changes: 33 additions & 4 deletions src/common/log.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
import * as riverpig from 'riverpig'
import { LoggerConfig } from 'riverpig'
import { Logger } from 'riverpig'
import * as debug from 'debug'

import through2 = require('through2')
const logStream = through2()
logStream.pipe(process.stdout)

// TODO: Not clear why I needed this, but got a
// TypeScript error without it.
export interface ConnectorLogger extends riverpig.Logger { }
export class ConnectorLogger {
river: any
tracer: any

constructor (namespace: string, config0?: LoggerConfig) {
this.river = riverpig(namespace, config0)
this.tracer = this.river.trace || debug(namespace + ':trace')
}

info (msg: any, ...elements: any[]): void {
this.river.info(msg, ...elements)
}

warn (msg: any, ...elements: any[]): void {
this.river.warn(msg, ...elements)
}

error (msg: any, ...elements: any[]): void {
this.river.error(msg, ...elements)
}

debug (msg: any, ...elements: any[]): void {
this.river.debug(msg, ...elements)
}

trace (msg: any, ...elements: any[]): void {
this.tracer(msg, ...elements)
}
}

export const createRaw = (namespace: string): ConnectorLogger => {
return riverpig(namespace, {
return new ConnectorLogger(namespace, {
stream: logStream
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/ccp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class CcpController {

async handleRouteControl (data: Buffer, sourceAccount: string) {
const routeControl = deserializeCcpRouteControlRequest(data)
log.debug('received route control message. sender=%s, tableId=%s epoch=%s features=%s', sourceAccount, routeControl.lastKnownRoutingTableId, routeControl.lastKnownEpoch, routeControl.features.join(','))
log.trace('received route control message. sender=%s, tableId=%s epoch=%s features=%s', sourceAccount, routeControl.lastKnownRoutingTableId, routeControl.lastKnownEpoch, routeControl.features.join(','))

this.routeBroadcaster.handleRouteControl(sourceAccount, routeControl)

Expand All @@ -44,7 +44,7 @@ export default class CcpController {

async handleRouteUpdate (data: Buffer, sourceAccount: string) {
const routeUpdate = deserializeCcpRouteUpdateRequest(data)
log.debug('received routes. sender=%s speaker=%s currentEpoch=%s fromEpoch=%s toEpoch=%s newRoutes=%s withdrawnRoutes=%s', sourceAccount, routeUpdate.speaker, routeUpdate.currentEpochIndex, routeUpdate.fromEpochIndex, routeUpdate.toEpochIndex, routeUpdate.newRoutes.length, routeUpdate.withdrawnRoutes.length)
log.trace('received routes. sender=%s speaker=%s currentEpoch=%s fromEpoch=%s toEpoch=%s newRoutes=%s withdrawnRoutes=%s', sourceAccount, routeUpdate.speaker, routeUpdate.currentEpochIndex, routeUpdate.fromEpochIndex, routeUpdate.toEpochIndex, routeUpdate.newRoutes.length, routeUpdate.withdrawnRoutes.length)

this.routeBroadcaster.handleRouteUpdate(sourceAccount, routeUpdate)

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class EchoController {
if (type === 0) {
const sourceAddress = reader.readVarOctetString().toString('ascii')

log.debug('responding to ping. sourceAccount=%s sourceAddress=%s cond=%s', sourceAccount, sourceAddress, parsedPacket.executionCondition.slice(0, 9).toString('base64'))
log.trace('responding to ping. sourceAccount=%s sourceAddress=%s cond=%s', sourceAccount, sourceAddress, parsedPacket.executionCondition.slice(0, 9).toString('base64'))

const nextHop = this.routeBuilder.getNextHop(sourceAccount, sourceAddress)

Expand All @@ -62,7 +62,7 @@ export default class EchoController {
data: writer.getBuffer()
}), nextHop)
} else {
log.debug('received unexpected ping response. sourceAccount=%s', sourceAccount)
log.error('received unexpected ping response. sourceAccount=%s', sourceAccount)
throw new InvalidPacketError('unexpected ping response.')
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/ildcp-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class IldcpHostController {
async handle (data: Buffer, sourceAccount: string) {
const clientAddress = this.accounts.getChildAddress(sourceAccount)
const info = this.accounts.getInfo(sourceAccount)
log.debug('responding to ILDCP config request. clientAddress=%s', clientAddress)
log.trace('responding to ILDCP config request. clientAddress=%s', clientAddress)

return ILDCP.serve({
requestPacket: data,
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/ilp-prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class IlpPrepareController {
const parsedPacket = IlpPacket.deserializeIlpPrepare(packet)
const { amount, executionCondition, destination, expiresAt } = parsedPacket

log.debug('handling ilp prepare. sourceAccount=%s destination=%s amount=%s condition=%s expiry=%s packet=%s', sourceAccount, destination, amount, executionCondition.toString('base64'), expiresAt.toISOString(), packet.toString('base64'))
log.trace('handling ilp prepare. sourceAccount=%s destination=%s amount=%s condition=%s expiry=%s packet=%s', sourceAccount, destination, amount, executionCondition.toString('base64'), expiresAt.toISOString(), packet.toString('base64'))

if (destination.startsWith(PEER_PROTOCOL_PREFIX)) {
return this.peerProtocolController.handle(packet, sourceAccount, { parsedPacket })
Expand All @@ -44,11 +44,11 @@ export default class IlpPrepareController {

const { nextHop, nextHopPacket } = await this.routeBuilder.getNextHopPacket(sourceAccount, parsedPacket)

log.debug('sending outbound ilp prepare. destination=%s amount=%s', destination, nextHopPacket.amount)
log.trace('sending outbound ilp prepare. destination=%s amount=%s', destination, nextHopPacket.amount)
const result = await outbound(IlpPacket.serializeIlpPrepare(nextHopPacket), nextHop)

if (result[0] === IlpPacket.Type.TYPE_ILP_FULFILL) {
log.debug('got fulfillment. cond=%s nextHop=%s amount=%s', executionCondition.slice(0, 6).toString('base64'), nextHop, nextHopPacket.amount)
log.trace('got fulfillment. cond=%s nextHop=%s amount=%s', executionCondition.slice(0, 6).toString('base64'), nextHop, nextHopPacket.amount)

this.backend.submitPayment({
sourceAccount: sourceAccount,
Expand All @@ -58,7 +58,7 @@ export default class IlpPrepareController {
})
.catch(err => {
const errInfo = (err && typeof err === 'object' && err.stack) ? err.stack : String(err)
log.warn('error while submitting payment to backend. error=%s', errInfo)
log.error('error while submitting payment to backend. error=%s', errInfo)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/ilqp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class IlqpController {
}

async sendData (packet: Buffer, sourceAccount: string) {
log.debug('responding to ILQP quote request. clientName=' + sourceAccount)
log.trace('responding to ILQP quote request. clientName=' + sourceAccount)

switch (packet[0]) {
case IlpPacket.Type.TYPE_ILQP_LIQUIDITY_REQUEST:
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('source-map-support').install()

import createApp from './app'
import { create as createLogger } from './common/log'

const log = createLogger('app')

module.exports = {
Expand Down Expand Up @@ -36,7 +37,7 @@ if (!module.parent) {
process.exit(0)
} catch (err) {
const errInfo = (err && typeof err === 'object' && err.stack) ? err.stack : err
console.error('error while shutting down. error=%s', errInfo)
log.error('error while shutting down. error=%s', errInfo)
process.exit(1)
}
})
Expand Down
14 changes: 7 additions & 7 deletions src/middlewares/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Balance {
const newBalance = this.balance.plus(amount)

if (newBalance.gt(this.maximum)) {
log.info('rejected balance update. oldBalance=%s newBalance=%s amount=%s', this.balance, newBalance, amount)
log.error('rejected balance update. oldBalance=%s newBalance=%s amount=%s', this.balance, newBalance, amount)
throw new InsufficientLiquidityError('exceeded maximum balance.')
}

Expand All @@ -43,7 +43,7 @@ class Balance {
const newBalance = this.balance.minus(amount)

if (newBalance.lt(this.minimum)) {
log.info('rejected balance update. oldBalance=%s newBalance=%s amount=%s', this.balance, newBalance, amount)
log.error('rejected balance update. oldBalance=%s newBalance=%s amount=%s', this.balance, newBalance, amount)
throw new Error(`insufficient funds. oldBalance=${this.balance} proposedBalance=${newBalance}`)
}

Expand Down Expand Up @@ -93,7 +93,7 @@ export default class BalanceMiddleware implements Middleware {
})
this.balances.set(accountId, balance)

log.debug('initializing balance for account. accountId=%s minimumBalance=%s maximumBalance=%s', accountId, minimum, maximum)
log.info('initializing balance for account. accountId=%s minimumBalance=%s maximumBalance=%s', accountId, minimum, maximum)

pipelines.startup.insertLast({
name: 'balance',
Expand All @@ -120,7 +120,7 @@ export default class BalanceMiddleware implements Middleware {

// Increase balance on prepare
balance.add(parsedPacket.amount)
log.debug('balance increased due to incoming ilp prepare. accountId=%s amount=%s newBalance=%s', accountId, parsedPacket.amount, balance.getValue())
log.trace('balance increased due to incoming ilp prepare. accountId=%s amount=%s newBalance=%s', accountId, parsedPacket.amount, balance.getValue())
this.stats.balance.setValue(account, {}, balance.getValue().toNumber())

let result
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class BalanceMiddleware implements Middleware {
name: 'balance',
method: async (amount: string, next: MiddlewareCallback<string, void>) => {
balance.subtract(amount)
log.debug('balance reduced due to incoming settlement. accountId=%s amount=%s newBalance=%s', accountId, amount, balance.getValue())
log.trace('balance reduced due to incoming settlement. accountId=%s amount=%s newBalance=%s', accountId, amount, balance.getValue())
this.stats.balance.setValue(account, {}, balance.getValue().toNumber())
return next(amount)
}
Expand Down Expand Up @@ -192,7 +192,7 @@ export default class BalanceMiddleware implements Middleware {
// Decrease balance on prepare
balance.subtract(parsedPacket.amount)
this.maybeSettle(accountId).catch(log.error)
log.debug('balance decreased due to outgoing ilp fulfill. accountId=%s amount=%s newBalance=%s', accountId, parsedPacket.amount, balance.getValue())
log.trace('balance decreased due to outgoing ilp fulfill. accountId=%s amount=%s newBalance=%s', accountId, parsedPacket.amount, balance.getValue())
this.stats.balance.setValue(account, {}, balance.getValue().toNumber())
this.stats.outgoingDataPacketValue.increment(account, { result : 'fulfilled' }, +parsedPacket.amount)
}
Expand All @@ -208,7 +208,7 @@ export default class BalanceMiddleware implements Middleware {
name: 'balance',
method: async (amount: string, next: MiddlewareCallback<string, void>) => {
balance.add(amount)
log.debug('balance increased due to outgoing settlement. accountId=%s amount=%s newBalance=%s', accountId, amount, balance.getValue())
log.trace('balance increased due to outgoing settlement. accountId=%s amount=%s newBalance=%s', accountId, amount, balance.getValue())
this.stats.balance.setValue(account, {}, balance.getValue().toNumber())

return next(amount)
Expand Down

0 comments on commit 5292ad4

Please sign in to comment.