mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
fix logger.
This commit is contained in:
3270
common-server/package-lock.json
generated
3270
common-server/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -10,13 +10,12 @@
|
||||
"license": "ISC",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@elastic/elasticsearch": "^8.1.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.0",
|
||||
"express": "^4.17.3",
|
||||
"mongodb": "^4.4.0",
|
||||
"winston": "^3.4.0",
|
||||
"winston-logstash-transport": "^2.0.0",
|
||||
"winston-slack-webhook-transport": "^2.1.0"
|
||||
"winston": "^3.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.12",
|
||||
|
||||
@@ -1,53 +1,5 @@
|
||||
import winston from 'winston';
|
||||
|
||||
import logstash from 'winston-logstash-transport';
|
||||
import Slack from 'winston-slack-webhook-transport';
|
||||
|
||||
const MESSAGE = Symbol.for('message');
|
||||
const LEVEL = Symbol.for('level');
|
||||
|
||||
const errorToLog = (log: $TSFixMe) => {
|
||||
// convert an instance of the Error class to a formatted log
|
||||
const formatted = {
|
||||
message: null,
|
||||
level: 'error',
|
||||
};
|
||||
|
||||
formatted[LEVEL] = 'error';
|
||||
if (log.message) {
|
||||
formatted.message = `${log.message}: \n${log.stack}`;
|
||||
} else {
|
||||
formatted.message = log.stack;
|
||||
}
|
||||
// formatted.message = log.stack;
|
||||
return formatted;
|
||||
};
|
||||
|
||||
// TODO:
|
||||
// add a handler for normal info logs
|
||||
const logFormatter = (logEntry: $TSFixMe) => {
|
||||
if (logEntry instanceof Error) {
|
||||
// an error object was passed in
|
||||
return errorToLog(logEntry);
|
||||
}
|
||||
if (logEntry.stack) {
|
||||
// an error object was passed in addition to an error message
|
||||
// logEntry.message = `${logEntry.message}: \n${logEntry.stack}`;
|
||||
logEntry.message = logEntry.stack;
|
||||
}
|
||||
if (logEntry.message && typeof logEntry.message === 'object') {
|
||||
if (logEntry.message?.err instanceof Error) {
|
||||
// Ugh. So here we are with a log message that is an instance of the Error class
|
||||
return errorToLog(logEntry.message.err);
|
||||
} else {
|
||||
// here we have an object as the log message but it's not an Error object
|
||||
logEntry.message = JSON.stringify(logEntry.message);
|
||||
}
|
||||
}
|
||||
|
||||
return logEntry;
|
||||
};
|
||||
|
||||
const consoleTransport = new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
@@ -65,49 +17,13 @@ const consoleTransport = new winston.transports.Console({
|
||||
handleExceptions: true,
|
||||
});
|
||||
|
||||
const logstashTransport = new logstash.LogstashTransport({
|
||||
host: process.env.LOGSTASH_HOST,
|
||||
port: process.env.LOGSTASH_PORT,
|
||||
});
|
||||
|
||||
const envTag = (logEntry: $TSFixMe) => {
|
||||
const tag = {
|
||||
env: process.env.NODE_ENV || 'development',
|
||||
containerName: process.env.CONTAINER_NAME,
|
||||
deploymentName: process.env.DEPLOYMENT_NAME,
|
||||
};
|
||||
const taggedLog = Object.assign(tag, logEntry);
|
||||
logEntry[MESSAGE] = JSON.stringify(taggedLog);
|
||||
return logEntry;
|
||||
};
|
||||
|
||||
const transports = [];
|
||||
|
||||
// configure transports (defined above)
|
||||
transports.push(consoleTransport);
|
||||
transports.push(logstashTransport);
|
||||
|
||||
const logger = winston.createLogger({
|
||||
format: winston.format.combine(
|
||||
winston.format(logFormatter)(),
|
||||
winston.format(envTag)()
|
||||
),
|
||||
transports,
|
||||
});
|
||||
|
||||
if (
|
||||
process.env.PORT &&
|
||||
process.env.SLACK_ERROR_LOG_WEBHOOK &&
|
||||
process.env.SLACK_ERROR_LOG_CHANNEL
|
||||
) {
|
||||
winston.add(new Slack({ webhookUrl: process.env.SLACK_ERROR_LOG_WEBHOOK }));
|
||||
}
|
||||
|
||||
logger.stream = {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
write: function(message: $TSFixMe, _encoding: $TSFixMe) {
|
||||
logger.http(message);
|
||||
},
|
||||
};
|
||||
|
||||
export default logger;
|
||||
|
||||
Reference in New Issue
Block a user