change logger

This commit is contained in:
grimhilt 2023-03-28 16:57:44 +02:00
parent bffcdafe7a
commit 94c7a7176b
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 { queryCcId, queryToId, queryFromId } = require("./utils/addressQueries.js");
const DEBUG = require("../utils/debug").DEBUG;
async function registerAccount(userId, pwd, xoauth, xoauth2, host, port, tls) { async function registerAccount(userId, pwd, xoauth, xoauth2, host, port, tls) {
const query = ` const query = `

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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