1 |
|
2 | const winston = require('winston');
|
3 | const Papertrail = require('winston-papertrail').Papertrail;
|
4 |
|
5 | const config = require('./config');
|
6 |
|
7 |
|
8 | const logger = winston.createLogger({
|
9 | format: winston.format.combine(
|
10 | winston.format.timestamp(),
|
11 | winston.format.splat(),
|
12 | winston.format.printf(info => {
|
13 | return `${info.timestamp} ${info.level}: ${info.message}`;
|
14 | })
|
15 | ),
|
16 | transports: []
|
17 | });
|
18 |
|
19 |
|
20 | module.exports = logger;
|
21 |
|
22 |
|
23 | module.exports.init = () => {
|
24 |
|
25 | logger.add(new winston.transports.Console());
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | if (config.env === 'test') {
|
32 | logger.silent = true;
|
33 | }
|
34 |
|
35 | if (process.env.PAPERTRAIL_HOST && config.env !== 'test') {
|
36 | logger.add(new winston.transports.Papertrail({
|
37 | host: 'logs.papertrailapp.com',
|
38 | port: 12345
|
39 | }));
|
40 | }
|
41 |
|
42 | logger.debug('Winston logger initialized');
|
43 |
|
44 | }
|