UNPKG

1.42 kBPlain TextView Raw
1import * as riverpig from 'riverpig'
2import * as debug from 'debug'
3
4import through2 = require('through2')
5const logStream = through2()
6logStream.pipe(process.stdout)
7
8export class ConnectorLogger {
9 river: any
10 tracer: any
11
12 constructor (namespace: string, config0?: riverpig.LoggerConfig) {
13 this.river = riverpig(namespace, config0)
14 this.tracer = this.river.trace || debug(namespace + ':trace')
15 }
16
17 // The logging functions being called have the signature function(format, ...args), so they require at least one argument.
18
19 info (msg: any, ...elements: any[]): void {
20 this.river.info(msg, ...elements)
21 }
22
23 warn (msg: any, ...elements: any[]): void {
24 this.river.warn(msg, ...elements)
25 }
26
27 error (msg: any, ...elements: any[]): void {
28 this.river.error(msg, ...elements)
29 }
30
31 debug (msg: any, ...elements: any[]): void {
32 this.river.debug(msg, ...elements)
33 }
34
35 trace (msg: any, ...elements: any[]): void {
36 this.tracer(msg, ...elements)
37 }
38}
39
40export const createRaw = (namespace: string): ConnectorLogger => {
41 return new ConnectorLogger(namespace, {
42 stream: logStream
43 })
44}
45
46export const create = (namespace: string) => createRaw('connector:' + namespace)
47
48let outputStream = process.stdout
49export const setOutputStream = (newOutputStream: NodeJS.WriteStream) => {
50 logStream.unpipe(outputStream)
51 logStream.pipe(newOutputStream)
52 outputStream = newOutputStream
53}