blob: 71cbaa79d63bfe07565dcdc6a91e636d2b81bc13 [file] [log] [blame] [raw]
RabsRincon6ef87b52018-02-27 14:58:21 +01001// Copyright (c) 2016, Matt Godbolt
Matt Godbolt5a6de742016-10-27 20:13:30 -05002// 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 Godboltef260162019-05-14 17:38:05 -050025const winston = require('winston'),
26 TransportStream = require('winston-transport'),
27 {Papertrail} = require('winston-papertrail'),
28 os = require('os'),
29 {LEVEL, MESSAGE} = require('triple-beam');
Matt Godbolt5a6de742016-10-27 20:13:30 -050030
Matt Godboltef260162019-05-14 17:38:05 -050031const consoleTransportInstance = new (winston.transports.Console)();
32const logger = winston.createLogger({
33 format: winston.format.combine(
34 winston.format.colorize(),
Matt Godbolt1f6e4c82019-05-16 17:30:15 -050035 winston.format.splat(),
Matt Godboltef260162019-05-14 17:38:05 -050036 winston.format.simple()),
37 transports: [consoleTransportInstance]
Matt Godbolt5a6de742016-10-27 20:13:30 -050038});
39
40logger.stream = {
RabsRincon670afd82018-02-13 14:49:07 +010041 write: message => {
Matt Godbolt5a6de742016-10-27 20:13:30 -050042 logger.info(message.trim());
43 }
44};
45
RabsRincon389c6912018-06-28 00:45:53 +020046logger.warnStream = {
47 write: message => {
48 logger.warn(message.trim());
49 }
50};
51
52logger.errStream = {
53 write: message => {
54 logger.error(message.trim());
55 }
56};
57
Matt Godboltef260162019-05-14 17:38:05 -050058// Our own transport which uses Papertrail under the hood but better adapts it to work
59// in winston 3.0
60class 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
87exports.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
102exports.suppressConsoleLog = () => {
Austin Morton1b7fe242020-02-18 00:03:15 -0500103 consoleTransportInstance.silent = true;
Matt Godboltef260162019-05-14 17:38:05 -0500104};
105
RabsRincon670afd82018-02-13 14:49:07 +0100106exports.logger = logger;