RabsRincon | 6ef87b5 | 2018-02-27 14:58:21 +0100 | [diff] [blame] | 1 | // Copyright (c) 2016, Matt Godbolt |
Matt Godbolt | 5a6de74 | 2016-10-27 20:13:30 -0500 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer in the |
| 11 | // documentation and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | // POSSIBILITY OF SUCH DAMAGE. |
| 24 | |
Matt Godbolt | ef26016 | 2019-05-14 17:38:05 -0500 | [diff] [blame] | 25 | const winston = require('winston'), |
| 26 | TransportStream = require('winston-transport'), |
| 27 | {Papertrail} = require('winston-papertrail'), |
| 28 | os = require('os'), |
| 29 | {LEVEL, MESSAGE} = require('triple-beam'); |
Matt Godbolt | 5a6de74 | 2016-10-27 20:13:30 -0500 | [diff] [blame] | 30 | |
Matt Godbolt | ef26016 | 2019-05-14 17:38:05 -0500 | [diff] [blame] | 31 | const consoleTransportInstance = new (winston.transports.Console)(); |
| 32 | const logger = winston.createLogger({ |
| 33 | format: winston.format.combine( |
| 34 | winston.format.colorize(), |
Matt Godbolt | 1f6e4c8 | 2019-05-16 17:30:15 -0500 | [diff] [blame] | 35 | winston.format.splat(), |
Matt Godbolt | ef26016 | 2019-05-14 17:38:05 -0500 | [diff] [blame] | 36 | winston.format.simple()), |
| 37 | transports: [consoleTransportInstance] |
Matt Godbolt | 5a6de74 | 2016-10-27 20:13:30 -0500 | [diff] [blame] | 38 | }); |
| 39 | |
| 40 | logger.stream = { |
RabsRincon | 670afd8 | 2018-02-13 14:49:07 +0100 | [diff] [blame] | 41 | write: message => { |
Matt Godbolt | 5a6de74 | 2016-10-27 20:13:30 -0500 | [diff] [blame] | 42 | logger.info(message.trim()); |
| 43 | } |
| 44 | }; |
| 45 | |
RabsRincon | 389c691 | 2018-06-28 00:45:53 +0200 | [diff] [blame] | 46 | logger.warnStream = { |
| 47 | write: message => { |
| 48 | logger.warn(message.trim()); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | logger.errStream = { |
| 53 | write: message => { |
| 54 | logger.error(message.trim()); |
| 55 | } |
| 56 | }; |
| 57 | |
Matt Godbolt | ef26016 | 2019-05-14 17:38:05 -0500 | [diff] [blame] | 58 | // Our own transport which uses Papertrail under the hood but better adapts it to work |
| 59 | // in winston 3.0 |
| 60 | class MyPapertrailTransport extends TransportStream { |
| 61 | constructor(opts) { |
| 62 | super(opts); |
| 63 | |
| 64 | this.hostname = os.hostname(); |
| 65 | this.program = opts.identifier; |
| 66 | |
| 67 | this.transport = new Papertrail( |
| 68 | { |
| 69 | host: opts.host, |
| 70 | port: opts.port, |
| 71 | logFormat: (level, message) => message |
| 72 | } |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | log(info, callback) { |
| 77 | setImmediate(() => { |
| 78 | this.emit('logged', info); |
| 79 | }); |
| 80 | |
| 81 | // We can't use callback here as winston-papertrail is a bit lax in calling it back |
| 82 | this.transport.sendMessage(this.hostname, this.program, info[LEVEL], info[MESSAGE], (x) => x); |
| 83 | callback(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | exports.logToPapertrail = (host, port, identifier) => { |
| 88 | const transport = new MyPapertrailTransport({ |
| 89 | host: host, port: port, identifier: identifier |
| 90 | }); |
| 91 | transport.transport.on('error', (err) => { |
| 92 | logger.error(err); |
| 93 | }); |
| 94 | |
| 95 | transport.transport.on('connect', (message) => { |
| 96 | logger.info(message); |
| 97 | }); |
| 98 | logger.add(transport); |
| 99 | logger.info("Configured papertrail"); |
| 100 | }; |
| 101 | |
| 102 | exports.suppressConsoleLog = () => { |
Austin Morton | 1b7fe24 | 2020-02-18 00:03:15 -0500 | [diff] [blame] | 103 | consoleTransportInstance.silent = true; |
Matt Godbolt | ef26016 | 2019-05-14 17:38:05 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
RabsRincon | 670afd8 | 2018-02-13 14:49:07 +0100 | [diff] [blame] | 106 | exports.logger = logger; |