Skip to content

Instantly share code, notes, and snippets.

@andykais
Created January 4, 2019 22:08
Show Gist options
  • Select an option

  • Save andykais/4504501fdd81c78b05d751ffe9771107 to your computer and use it in GitHub Desktop.

Select an option

Save andykais/4504501fdd81c78b05d751ffe9771107 to your computer and use it in GitHub Desktop.

Revisions

  1. andykais created this gist Jan 4, 2019.
    29 changes: 29 additions & 0 deletions example-log-transport.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import * as Rx from 'rxjs'
    import * as ops from 'rxjs/operators'
    import fs from 'fs'
    import VError from 'verror'
    /**
    * for now, there can only be one transport at a time
    * the cli will have to handle an option to directly log to terminal while also including a
    * "terminal ui" output default (progress bars, queued, in progress, completed, etc)
    * this can be accomplished by log options being done directly as command line args
    */
    abstract class LogTransport {
    abstract transport: (data: string) => void
    }
    class StdoutTransport extends LogTransport {
    transport = console.log
    }
    class FileTransport extends LogTransport {
    writeStream: fs.WriteStream
    constructor(filename: string) {
    super()
    this.writeStream = fs.createWriteStream(filename)
    this.writeStream.on('error', error => {
    throw new VError(error, 'logger file transport')
    })
    }
    transport = (data: string) => {
    this.writeStream.write(data)
    }
    }