Initial commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// ROOT/services/logger/readLogs.js
|
||||
|
||||
// Import the MySQL pools from your database service
|
||||
const pools = require('../database/pools').get();
|
||||
|
||||
// Database name used for logging
|
||||
const dbName = 'logging';
|
||||
|
||||
// Use the Read pool with the "system" role
|
||||
const readPool = pools[dbName].r.s;
|
||||
|
||||
/*
|
||||
* Function: getAllLogs
|
||||
* Description: Retrieves all log entries from a specified table.
|
||||
* @param {string} tableName - Name of the table to query.
|
||||
* @returns {Array|Object} - Returns an array of log entries, or an error object.
|
||||
*/
|
||||
async function getAllLogs(tableName) {
|
||||
if (!tableName) throw new Error('Table name is required');
|
||||
|
||||
try {
|
||||
const [rows] = await readPool.query(`SELECT * FROM ${tableName}`);
|
||||
return rows;
|
||||
} catch (err) {
|
||||
console.error('Error reading logs from DB:', err);
|
||||
return { success: false, error: err };
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: getLogs
|
||||
* Description: Retrieves log entries from a specified table that match optional filter criteria.
|
||||
* @param {string} tableName - Name of the table to query.
|
||||
* @param {Object} filter - Optional key/value pairs representing column filters.
|
||||
* Example: { user: 'andi', success: true }
|
||||
* @returns {Array|Object} - Returns an array of filtered log entries, or an error object.
|
||||
*/
|
||||
async function getLogs(tableName, filter = {}) {
|
||||
if (!tableName) throw new Error('Table name is required');
|
||||
|
||||
// Start building the SQL query
|
||||
let sql = `SELECT * FROM ${tableName}`;
|
||||
const values = [];
|
||||
|
||||
const filterKeys = Object.keys(filter);
|
||||
if (filterKeys.length > 0) {
|
||||
// Build WHERE clauses dynamically
|
||||
const whereClauses = filterKeys.map(key => {
|
||||
values.push(filter[key]);
|
||||
return `${key} = ?`;
|
||||
});
|
||||
sql += ' WHERE ' + whereClauses.join(' AND ');
|
||||
}
|
||||
|
||||
try {
|
||||
const [rows] = await readPool.query(sql, values);
|
||||
return rows;
|
||||
} catch (err) {
|
||||
console.error('Error reading filtered logs from DB:', err);
|
||||
return { success: false, error: err };
|
||||
}
|
||||
}
|
||||
|
||||
// Export both functions for external use
|
||||
module.exports = {
|
||||
getAll: getAllLogs,
|
||||
get: getLogs
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
let online = false;
|
||||
|
||||
module.exports = async function() {
|
||||
try {
|
||||
online = true;
|
||||
console.log('Logger service started!');
|
||||
return { online: online };
|
||||
} catch (err) {
|
||||
console.error('Logger service could not be started:', err);
|
||||
return { online: false, error: err };
|
||||
}
|
||||
};
|
||||
|
||||
// Export online status for other modules
|
||||
module.exports.online = () => online;
|
||||
|
||||
// Setter to change online status from other modules
|
||||
module.exports.setOnline = (value) => { online = value; };
|
||||
@@ -0,0 +1,5 @@
|
||||
const logger = require('./start');
|
||||
|
||||
module.exports = async function() {
|
||||
return { online: logger.online() };
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
const logger = require('./start');
|
||||
|
||||
module.exports = async function() {
|
||||
// Set logger offline via setter in start.js
|
||||
logger.setOnline(false);
|
||||
console.log('Logger service stopped!');
|
||||
return { online: logger.online() };
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
const loggerService = require('./start');
|
||||
|
||||
// Import the MySQL pools from your database service
|
||||
const pools = require('../database/pools').get();
|
||||
|
||||
// Name of the database used for logging
|
||||
const dbName = 'logging';
|
||||
|
||||
// Get the Write pool with the "system" role
|
||||
const writePool = pools[dbName].w.s;
|
||||
|
||||
/*
|
||||
* Generic function to write log data to any table
|
||||
* @param {string} tableName - Name of the table to write to
|
||||
* @param {Object} logData - Key/value pairs representing columns and values
|
||||
* @returns {Object} - Success status and optional error
|
||||
*/
|
||||
async function writeToTable(tableName, logData) {
|
||||
// Check if logger is active
|
||||
if (!loggerService.online()) {
|
||||
return { success: false, error: 'Logger is inactive' };
|
||||
}
|
||||
|
||||
// Validate input
|
||||
if (!tableName || !logData) throw new Error('Table name and log data are required');
|
||||
|
||||
// Extract columns and values dynamically from the logData object
|
||||
const columns = Object.keys(logData).join(', '); // e.g., "user, action, success"
|
||||
const placeholders = Object.keys(logData).map(() => '?').join(', '); // e.g., "?, ?, ?"
|
||||
const values = Object.values(logData); // e.g., ["user", "login", true]
|
||||
|
||||
// Build the INSERT SQL statement
|
||||
const sql = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
||||
|
||||
try {
|
||||
// Execute the query on the write pool
|
||||
await writePool.query(sql, values);
|
||||
|
||||
// Return success
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
// Log the error to the console for debugging
|
||||
console.error('Error writing log to DB:', err);
|
||||
|
||||
// Return failure state
|
||||
return { success: false, error: err };
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic write function for any other log types.
|
||||
* This allows dynamic handling of new log types without modifying the module.
|
||||
*/
|
||||
module.exports.write = async function(logData, tableName) {
|
||||
return writeToTable(tableName, logData);
|
||||
};
|
||||
Reference in New Issue
Block a user