Update debugging to log to a file (for running as a service)
This commit is contained in:
@@ -1,5 +1,27 @@
|
|||||||
// Debug
|
// Debug
|
||||||
const debug = require('debug');
|
const debug = require('debug');
|
||||||
|
// Read .env file to process.env
|
||||||
|
require('dotenv').config();
|
||||||
|
// Modules
|
||||||
|
const { writeFile } = require('fs');
|
||||||
|
|
||||||
|
const logLocation = process.env.LOG_LOCATION;
|
||||||
|
|
||||||
|
async function writeToLog(logMessage, appName) {
|
||||||
|
logMessage = String(logMessage + "\n");
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
logLocation ?? `./${appName}.log`,
|
||||||
|
logMessage,
|
||||||
|
{ encoding: "utf-8", flag: 'a+' },
|
||||||
|
(err) => {
|
||||||
|
if (err) console.error(err);
|
||||||
|
|
||||||
|
// file written successfully
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the different logging methods for a function
|
* Create the different logging methods for a function
|
||||||
@@ -9,14 +31,38 @@ const debug = require('debug');
|
|||||||
*/
|
*/
|
||||||
exports.DebugBuilder = class DebugBuilder {
|
exports.DebugBuilder = class DebugBuilder {
|
||||||
constructor(appName, fileName) {
|
constructor(appName, fileName) {
|
||||||
this.INFO = debug(`${appName}:${fileName}:INFO`);
|
this.INFO = (...messageParts) => {
|
||||||
this.DEBUG = debug(`${appName}:${fileName}:DEBUG`);
|
const _info = debug(`${appName}:${fileName}:INFO`);
|
||||||
this.WARN = debug(`${appName}:${fileName}:WARNING`);
|
_info(messageParts);
|
||||||
this.VERBOSE = debug(`${appName}:${fileName}:VERBOSE`);
|
writeToLog(`${appName}:${fileName}:INFO\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.DEBUG = (...messageParts) => {
|
||||||
|
const _debug = debug(`${appName}:${fileName}:DEBUG`);
|
||||||
|
_debug(messageParts);
|
||||||
|
writeToLog(`${appName}:${fileName}:DEBUG\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.VERBOSE = (...messageParts) => {
|
||||||
|
const _verbose = debug(`${appName}:${fileName}:VERBOSE`);
|
||||||
|
_verbose(messageParts);
|
||||||
|
writeToLog(`${appName}:${fileName}:VERBOSE\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.WARN = (...messageParts) => {
|
||||||
|
const _warn = debug(`${appName}:${fileName}:WARNING`);
|
||||||
|
_warn(messageParts);
|
||||||
|
writeToLog(`${appName}:${fileName}:WARNING\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
this.ERROR = (...messageParts) => {
|
this.ERROR = (...messageParts) => {
|
||||||
const error = debug(`${appName}:${fileName}:ERROR`);
|
const _error = debug(`${appName}:${fileName}:ERROR`);
|
||||||
error(messageParts);
|
_error(messageParts);
|
||||||
if (process.env.EXIT_ON_ERROR && process.env.EXIT_ON_ERROR > 0) setTimeout(process.exit, process.env.EXIT_ON_ERROR_DELAY ?? 0);
|
writeToLog(`${appName}:${fileName}:ERROR\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
if (process.env.EXIT_ON_ERROR && process.env.EXIT_ON_ERROR > 0) {
|
||||||
|
writeToLog("!--- EXITING ---!", appName);
|
||||||
|
setTimeout(process.exit, process.env.EXIT_ON_ERROR_DELAY ?? 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,26 @@
|
|||||||
const debug = require('debug');
|
const debug = require('debug');
|
||||||
// Read .env file to process.env
|
// Read .env file to process.env
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
// Modules
|
||||||
|
const { writeFile } = require('fs');
|
||||||
|
|
||||||
|
const logLocation = process.env.LOG_LOCATION;
|
||||||
|
|
||||||
|
async function writeToLog(logMessage, appName) {
|
||||||
|
logMessage = String(logMessage + "\n");
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
logLocation ?? `./${appName}.log`,
|
||||||
|
logMessage,
|
||||||
|
{ encoding: "utf-8", flag: 'a+' },
|
||||||
|
(err) => {
|
||||||
|
if (err) console.error(err);
|
||||||
|
|
||||||
|
// file written successfully
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the different logging methods for a function
|
* Create the different logging methods for a function
|
||||||
@@ -11,14 +31,38 @@ require('dotenv').config();
|
|||||||
*/
|
*/
|
||||||
exports.DebugBuilder = class DebugBuilder {
|
exports.DebugBuilder = class DebugBuilder {
|
||||||
constructor(appName, fileName) {
|
constructor(appName, fileName) {
|
||||||
this.INFO = debug(`${appName}:${fileName}:INFO`);
|
this.INFO = (...messageParts) => {
|
||||||
this.DEBUG = debug(`${appName}:${fileName}:DEBUG`);
|
const _info = debug(`${appName}:${fileName}:INFO`);
|
||||||
this.VERBOSE = debug(`${appName}:${fileName}:VERBOSE`);
|
_info(messageParts);
|
||||||
this.WARN = debug(`${appName}:${fileName}:WARNING`);
|
writeToLog(`${appName}:${fileName}:INFO\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.DEBUG = (...messageParts) => {
|
||||||
|
const _debug = debug(`${appName}:${fileName}:DEBUG`);
|
||||||
|
_debug(messageParts);
|
||||||
|
writeToLog(`${appName}:${fileName}:DEBUG\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.VERBOSE = (...messageParts) => {
|
||||||
|
const _verbose = debug(`${appName}:${fileName}:VERBOSE`);
|
||||||
|
_verbose(messageParts);
|
||||||
|
writeToLog(`${appName}:${fileName}:VERBOSE\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.WARN = (...messageParts) => {
|
||||||
|
const _warn = debug(`${appName}:${fileName}:WARNING`);
|
||||||
|
_warn(messageParts);
|
||||||
|
writeToLog(`${appName}:${fileName}:WARNING\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
}
|
||||||
|
|
||||||
this.ERROR = (...messageParts) => {
|
this.ERROR = (...messageParts) => {
|
||||||
const error = debug(`${appName}:${fileName}:ERROR`);
|
const _error = debug(`${appName}:${fileName}:ERROR`);
|
||||||
error(messageParts);
|
_error(messageParts);
|
||||||
if (process.env.EXIT_ON_ERROR && process.env.EXIT_ON_ERROR > 0) setTimeout(process.exit, process.env.EXIT_ON_ERROR_DELAY ?? 0);
|
writeToLog(`${appName}:${fileName}:ERROR\t-\t${messageParts.map((messagePart, index, array) => {return JSON.stringify(messagePart)})}`, appName);
|
||||||
|
if (process.env.EXIT_ON_ERROR && process.env.EXIT_ON_ERROR > 0) {
|
||||||
|
writeToLog("!--- EXITING ---!", appName);
|
||||||
|
setTimeout(process.exit, process.env.EXIT_ON_ERROR_DELAY ?? 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user