Skip to content

Commit

Permalink
feat: implement sent/recieved logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 16, 2018
1 parent d0e9c7d commit 624a590
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { EventEmitterInterface } from './types/eventEmitter'
import { BigImageCard } from './types/card'
import { image, bigImageCard, itemsListCard } from './card'
import reply from './reply'
import eventEmitter from './eventEmitter'

import { EVENT_MESSAGE_RECIEVED } from './constants'
import { EVENT_MESSAGE_SENT } from './constants'

export default class Ctx implements CtxInterface {
public req: WebhookRequest
Expand Down Expand Up @@ -81,6 +82,9 @@ export default class Ctx implements CtxInterface {
}

const message = this._createReply(replyMessage)
eventEmitter.dispatch(EVENT_MESSAGE_SENT, {
data: message.response.text, session: this.req.session,
})
return this._sendReply(message)
}

Expand Down Expand Up @@ -123,10 +127,6 @@ export default class Ctx implements CtxInterface {
if (typeof this.sendResponse === 'function') {
return this.sendResponse(replyMessage)
}

this.eventEmitter.dispatch(EVENT_MESSAGE_RECIEVED, {
data: this.message, session: this.req.session,
})
return replyMessage
}
}
12 changes: 11 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ colors.setTheme({
error: 'red',
})

const logTime = (timestamp: string) => {
const event = new Date(timestamp)
return event.toLocaleString('en-US')
}

export default class Logger {
constructor() {
eventEmitter.subscribe(EVENT_MESSAGE_RECIEVED, this.log)
eventEmitter.subscribe(EVENT_MESSAGE_SENT, this.log)
}

public log(event: EventData) {
// tslint:disable-next-line:no-console
console.log(
colors.warn(event.data),
[
colors.info(`[${logTime(event.timestamp)}]:`),
colors.verbose(`(${event.type})`),
colors.warn(event.data),
].join(' '),
)
}
}

0 comments on commit 624a590

Please sign in to comment.