change logger

This commit is contained in:
grimhilt 2023-03-28 16:57:44 +02:00
parent 838550b6cc
commit 185f051a63
11 changed files with 41 additions and 49 deletions

View File

@ -1,6 +1,5 @@
const { db, execQueryAsync, execQueryAsyncWithId } = require("./db.js");
const { execQueryAsync, execQueryAsyncWithId } = require("./db.js");
const { queryCcId, queryToId, queryFromId } = require("./utils/addressQueries.js");
const DEBUG = require("../utils/debug").DEBUG;
async function registerAccount(userId, pwd, xoauth, xoauth2, host, port, tls) {
const query = `

View File

@ -1,7 +1,6 @@
const mysql = require("mysql");
const { logger } = require("../system/Logger");
const MYSQL = require("./config.json").mysql;
const DEBUG = require("../utils/debug.js").DEBUG;
const db = mysql.createConnection({
host: MYSQL.host,
@ -12,9 +11,9 @@ const db = mysql.createConnection({
db.connect(function (err) {
if (err) {
DEBUG.log("Impossible de se connecter", err.code);
logger.error(`Unable to connect database ${err.code}`);
} else {
DEBUG.log("Database successfully connected");
logger.log("Database successfully connected");
}
});
@ -45,7 +44,7 @@ function execQueryAsyncWithId(query, values) {
function execQuery(query, values) {
db.query(query, values, (err, results, fields) => {
if (err) {
DEBUG.log(err);
logger.err(err);
throw (err);
}
return results;

View File

@ -1,6 +1,4 @@
const { execQueryAsync, execQueryAsyncWithId } = require("./db.js");
const DEBUG = require("../utils/debug").DEBUG;
async function getAddresseId(email, name) {
const localpart = email.split("@")[0];

View File

@ -1,6 +1,5 @@
const { transformEmojis } = require("../utils/string.js");
const { db, execQuery, execQueryAsync, execQueryAsyncWithId } = require("./db.js");
const DEBUG = require("../utils/debug").DEBUG;
async function registerMessage(timestamp, rfc822size, messageId) {
const query = `

View File

@ -1,7 +1,6 @@
const { transformEmojis } = require("../utils/string.js");
const { db, execQueryAsync, execQueryAsyncWithId, execQuery } = require("./db.js");
const { queryFromId, queryToId, queryCcId } = require("./utils/addressQueries.js");
const DEBUG = require("../utils/debug").DEBUG;
async function createRoom(roomName, ownerId, messageId) {
roomName = transformEmojis(roomName);

View File

@ -1,5 +1,5 @@
const { getMailbox, updateMailbox } = require("../../db/imap/imap");
const { DEBUG } = require("../../utils/debug");
const { logger } = require("../../system/Logger");
const { registerMessageInApp } = require("../saveMessage");
const { saveMessage } = require("../storeMessage");
@ -17,7 +17,7 @@ class Box {
const readOnly = true;
this.imap.openBox(this.boxName, readOnly, (err, box) => {
if (err) DEBUG.log(err);
if (err) logger.error(err);
this.sync(this.box.uidnext, box.uidnext);
});
}
@ -38,7 +38,7 @@ class Box {
});
f.once("error", (err) => {
DEBUG.log("Fetch error: " + err);
logger.error("Fetch error: " + err);
});
f.once("end", async () => {

View File

@ -1,6 +1,6 @@
const Imap = require("imap");
const { getAllMailboxes, registerMailbox } = require("../../db/imap/imap");
const { DEBUG } = require("../../utils/debug");
const { logger } = require("../../system/Logger");
const { Box } = require("./Box");
class ImapInstance {
@ -13,7 +13,7 @@ class ImapInstance {
port: account.port,
tls: account.tls,
});
this.account = account;
this.boxes = [];
@ -21,16 +21,16 @@ class ImapInstance {
* IMAP
*/
this.imap.once("ready", () => {
DEBUG.log("imap connected")
logger.log("Imap connected for " + this.account.user);
this.imapReady();
});
this.imap.once("error", function (err) {
DEBUG.log(err);
logger.error("Imap error for " + this.account.user + ": " + err);
});
this.imap.once("end", function () {
DEBUG.log("Connection ended");
logger.log("Connection ended for " + this.account.user);
});
this.imap.connect();
@ -41,8 +41,8 @@ class ImapInstance {
if (mailboxes.length > 0) {
this.boxes.push(new Box(this.imap, mailboxes[0].mailbox_id, mailboxes[0].mailbox_name));
} else {
this.imap.getBoxes('', (err, boxes) => {
if (err) DEBUG.log(err);
this.imap.getBoxes("", (err, boxes) => {
if (err) logger.error(err);
const allBoxName = this.getAllBox(boxes);
registerMailbox(this.account.id, allBoxName).then((mailboxId) => {
this.boxes.push(new Box(this.imap, mailboxId, allBoxName));
@ -55,19 +55,21 @@ class ImapInstance {
getAllBox(boxes) {
// ideally we should get the all box to get all messages
let allBox;
Object.keys(boxes).forEach(key => {
if (key === 'INBOX') return;
Object.keys(boxes).forEach((key) => {
if (key === "INBOX") return;
if (allBox.includes("/")) return;
allBox = key;
Object.keys(boxes[key].children).forEach((childBoxes) => {
if (boxes[key].children[childBoxes].attribs.includes('\\All')) {
allBox += '/' + childBoxes;
if (boxes[key].children[childBoxes].attribs.includes("\\All")) {
allBox += "/" + childBoxes;
}
});
});
if (!allBox.includes("/")) logger.warn("Did not find 'All' mailbox");
return allBox;
}
}
module.exports = {
ImapInstance
}
ImapInstance,
};

View File

@ -1,5 +1,5 @@
const { getAllAccounts } = require("../../db/imap/imap");
const { DEBUG } = require("../../utils/debug");
const { logger } = require("../../system/Logger");
const { ImapInstance } = require("./ImapInstance");
class ImapSync {
@ -14,7 +14,7 @@ class ImapSync {
this.addInstance(accounts[i]);
}
}).catch((err) => {
DEBUG.log(err);
logger.error(err);
});
}

View File

@ -1,5 +1,4 @@
const { getAddresseId } = require("../db/mail");
const { DEBUG } = require("../utils/debug");
const { simpleParser } = require("mailparser");
const moment = require("moment");
const {
@ -13,6 +12,7 @@ const {
} = require("../db/saveMessage");
const { getFieldId } = require("../db/mail");
const { logger } = require("../system/Logger");
function saveMessage(attrs, mailboxId, imap) {
const envelope = attrs.envelope;
@ -54,14 +54,14 @@ function saveMessage(attrs, mailboxId, imap) {
});
});
f.once("error", function (err) {
console.log("Fetch error: " + err);
logger.warn("Fetch error: " + err);
});
f.once("end", function () {
DEBUG.log("Done fetching data of "+messageID);
logger.log("Done fetching data of " + messageID);
});
})
.catch((err) => {
DEBUG.log("Unable to register message: " + err);
logger.warn("Unable to register message: " + err);
reject(err);
});
});
@ -97,8 +97,8 @@ async function saveFromParsedData(parsed, messageId) {
messageId,
fieldId,
bodypartId,
undefined, // todo ?
undefined
undefined, // todo ?
undefined,
);
});
});
@ -109,7 +109,7 @@ async function saveFromParsedData(parsed, messageId) {
// other field are not important and can be retrieved in source
return;
} else {
DEBUG.log("doesn't know key: " + key);
logger.warn("doesn't know key: " + key);
return;
}
});
@ -121,8 +121,8 @@ module.exports = {
saveMessage,
};
if (process.env['NODE_DEV'] == 'TEST') {
if (process.env["NODE_DEV"] == "TEST") {
module.exports = {
saveFromParsedData
saveFromParsedData,
};
}
}

View File

@ -1,9 +0,0 @@
const DEBUG = (function () {
const timestamp = function () {};
timestamp.toString = () => "[" + new Date().toLocaleString() + "]";
return { log: console.log.bind(console, "%s", timestamp) };
})();
module.exports = {
DEBUG,
};

View File

@ -0,0 +1,5 @@
export default class Thread {
constructor (roomId, name) {
}
}