|
1 | 1 | const { createLogger, format, transports } = require("winston");
|
2 |
| -const { PapertrailConnection, PapertrailTransport } = require("winston-papertrail"); |
| 2 | +const { |
| 3 | + PapertrailConnection, |
| 4 | + PapertrailTransport, |
| 5 | +} = require("winston-papertrail"); |
3 | 6 |
|
4 | 7 | let logger;
|
5 | 8 |
|
6 |
| -if (process.env.NODE_ENV !== "test") { |
7 |
| - const winstonPapertrail = new PapertrailConnection({ |
8 |
| - host: process.env.PAPERTRAIL_URL, |
9 |
| - port: 10737, |
10 |
| - }); |
11 |
| - |
12 |
| - |
13 |
| - const paperTrailTransport = new PapertrailTransport(winstonPapertrail); |
14 |
| - |
| 9 | +const setupLogger = () => { |
| 10 | + if (process.env.NODE_ENV === "test") return; |
15 | 11 |
|
16 |
| - const errorStackTracerFormat = format(info => { |
| 12 | + const errorStackTracerFormat = format((info) => { |
17 | 13 | if (info.stack) {
|
18 | 14 | info.message = `${info.message} ${info.stack}`;
|
19 | 15 | }
|
20 | 16 | return info;
|
21 | 17 | });
|
22 | 18 |
|
23 |
| - |
24 | 19 | logger = createLogger({
|
25 | 20 | format: format.combine(
|
26 | 21 | format.errors({ stack: true }),
|
27 | 22 | format.splat(),
|
28 | 23 | format.colorize(),
|
29 | 24 | errorStackTracerFormat(),
|
30 |
| - format.simple(), |
| 25 | + format.simple() |
31 | 26 | ),
|
32 |
| - transports: [paperTrailTransport], |
| 27 | + transports: [], |
33 | 28 | });
|
34 | 29 |
|
35 |
| - if (process.env.NODE_ENV === "development") { |
36 |
| - logger.add(new transports.Console({ |
| 30 | + logger.add( |
| 31 | + new transports.Console({ |
37 | 32 | format: format.simple(),
|
38 |
| - })); |
39 |
| - logger.remove(paperTrailTransport); |
40 |
| - } |
41 |
| - |
42 |
| - logger.rejections.handle( |
43 |
| - paperTrailTransport, |
| 33 | + }) |
44 | 34 | );
|
45 | 35 |
|
46 |
| - logger.exceptions.handle( |
47 |
| - paperTrailTransport, |
48 |
| - ); |
| 36 | + if (!process.env.PAPERTRAIL_URL) return; |
49 | 37 |
|
50 |
| - winstonPapertrail.on("connect", function() { |
| 38 | + const winstonPapertrail = new PapertrailConnection({ |
| 39 | + host: process.env.PAPERTRAIL_URL, |
| 40 | + port: 10737, |
| 41 | + }); |
| 42 | + |
| 43 | + const paperTrailTransport = new PapertrailTransport(winstonPapertrail); |
| 44 | + |
| 45 | + logger.add(paperTrailTransport); |
| 46 | + |
| 47 | + logger.rejections.handle(paperTrailTransport); |
| 48 | + |
| 49 | + logger.exceptions.handle(paperTrailTransport); |
| 50 | + |
| 51 | + winstonPapertrail.on("connect", function () { |
51 | 52 | logger.info("Logger connected to Papertrail");
|
52 | 53 | });
|
53 |
| -} |
| 54 | +}; |
| 55 | + |
| 56 | +setupLogger(); |
54 | 57 |
|
55 | 58 | const logError = (error) => {
|
56 |
| - if (logger) { |
57 |
| - logger.error(error); |
58 |
| - } |
| 59 | + logger.error(error); |
59 | 60 | };
|
60 | 61 |
|
61 | 62 | const logInfo = (message) => {
|
62 |
| - if (logger) { |
63 |
| - logger.info(message); |
64 |
| - } |
| 63 | + logger.info(message); |
65 | 64 | };
|
66 | 65 |
|
67 | 66 | const logInteractionError = (error, client, interaction) => {
|
68 |
| - if (logger) { |
69 |
| - const member = client.guild.members.cache.get(interaction.member.user.id); |
70 |
| - const channel = client.guild.channels.cache.get(interaction.channelId); |
71 |
| - const msg = `ERROR DETECTED!\nMember: ${member.displayName}\nCommand: ${interaction.commandName}\nChannel: ${channel.name}}`; |
72 |
| - logger.error(msg); |
73 |
| - logger.error(error); |
74 |
| - } |
| 67 | + const member = client.guild.members.cache.get(interaction.member.user.id); |
| 68 | + const channel = client.guild.channels.cache.get(interaction.channelId); |
| 69 | + const msg = `ERROR DETECTED!\nMember: ${member.displayName}\nCommand: ${interaction.commandName}\nChannel: ${channel.name}}`; |
| 70 | + logger.error(msg); |
| 71 | + logger.error(error); |
75 | 72 | };
|
76 | 73 |
|
77 |
| -const logNoInteractionError = async (telegramId, member, channel, client, error) => { |
78 |
| - if (logger) { |
79 |
| - const msg = `ERROR DETECTED!\nMember: ${member}\nChannel: ${channel}`; |
80 |
| - logger.error(msg); |
81 |
| - logger.error(error); |
82 |
| - } |
| 74 | +const logNoInteractionError = async ( |
| 75 | + telegramId, |
| 76 | + member, |
| 77 | + channel, |
| 78 | + client, |
| 79 | + error |
| 80 | +) => { |
| 81 | + const msg = `ERROR DETECTED!\nMember: ${member}\nChannel: ${channel}`; |
| 82 | + logger.error(msg); |
| 83 | + logger.error(error); |
83 | 84 | };
|
84 | 85 |
|
85 | 86 | module.exports = {
|
86 |
| - logError, logInteractionError, logNoInteractionError, logInfo, |
| 87 | + logError, |
| 88 | + logInteractionError, |
| 89 | + logNoInteractionError, |
| 90 | + logInfo, |
87 | 91 | };
|
0 commit comments