errsole-mysql
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -66,12 +66,8 @@ /** | ||
await this.checkConnection(); | ||
await this.setBufferSizes(); | ||
await this.setBufferSize(); | ||
await this.createTables(); | ||
await this.ensureLogsTTL(); | ||
this.emit('ready'); | ||
cron.schedule('0 * * * *', () => { | ||
this.deleteExpiredLogs(); | ||
}); | ||
setInterval(() => { | ||
this.flushLogs(); | ||
}, this.flushInterval); | ||
setInterval(() => this.flushLogs(), this.flushInterval); | ||
cron.schedule('0 * * * *', () => this.deleteExpiredLogs()); | ||
} | ||
@@ -89,8 +85,8 @@ | ||
async setBufferSizes () { | ||
const desiredSize = 8 * 1024 * 1024; // 8 MB in bytes | ||
async setBufferSize () { | ||
const DESIRED_SORT_BUFFER_SIZE = 8 * 1024 * 1024; // 8 MB in bytes | ||
const currentSize = await this.getBufferSize(); | ||
if (currentSize < desiredSize) { | ||
const query = 'SET SESSION sort_buffer_size = 8388608'; // Set for the session | ||
if (currentSize < DESIRED_SORT_BUFFER_SIZE) { | ||
const query = `SET SESSION sort_buffer_size = ${DESIRED_SORT_BUFFER_SIZE}`; // Set for the session | ||
return new Promise((resolve, reject) => { | ||
@@ -122,3 +118,3 @@ this.pool.query(query, err => { | ||
\`source\` VARCHAR(255), | ||
\`timestamp\` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
\`timestamp\` TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3), | ||
\`level\` VARCHAR(255) DEFAULT 'info', | ||
@@ -157,20 +153,8 @@ \`message\` TEXT, | ||
/** | ||
* Ensures that the Time To Live (TTL) configuration for logs is set. | ||
* | ||
* @async | ||
* @function ensureLogsTTL | ||
* @returns {Promise<{}>} - A promise that resolves with an empty object once the TTL configuration is confirmed or updated. | ||
*/ | ||
async ensureLogsTTL () { | ||
const defaultTTL = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds | ||
try { | ||
const configResult = await this.getConfig('logsTTL'); | ||
if (!configResult.item) { | ||
await this.setConfig('logsTTL', defaultTTL.toString()); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
const DEFAULT_LOGS_TTL = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds | ||
const configResult = await this.getConfig('logsTTL'); | ||
if (!configResult.item) { | ||
await this.setConfig('logsTTL', DEFAULT_LOGS_TTL.toString()); | ||
} | ||
return {}; | ||
} | ||
@@ -238,9 +222,9 @@ | ||
/** | ||
* Adds log entries to the pending logs. | ||
* Adds log entries to the pending logs and flushes them if the batch size is reached. | ||
* | ||
* @param {Log[]} logEntries - An array of log entries to be added to the pending logs. | ||
* @returns {{}} - A Promise that resolves with an empty object. | ||
* @returns {Object} - An empty object. | ||
*/ | ||
postLogs (logEntry) { | ||
this.pendingLogs.push(...logEntry); | ||
postLogs (logEntries) { | ||
this.pendingLogs.push(...logEntries); | ||
if (this.pendingLogs.length >= this.batchSize) { | ||
@@ -266,2 +250,6 @@ this.flushLogs(); | ||
const logsToPost = this.pendingLogs.splice(0, this.pendingLogs.length); | ||
if (logsToPost.length === 0) { | ||
return {}; // No logs to post | ||
} | ||
const values = logsToPost.map(logEntry => [ | ||
@@ -376,3 +364,3 @@ new Date(logEntry.timestamp), | ||
* @param {LogFilter} [filters] - Filters to refine the search. | ||
* @returns {Promise<{items: Log[]}>} - A promise that resolves with an object containing an array of log items. | ||
* @returns {Promise<{items: Log[], filters: LogFilter[]}>} - A promise that resolves with an object containing an array of log items and the applied filters. | ||
* @throws {Error} - Throws an error if the operation fails. | ||
@@ -413,3 +401,2 @@ */ | ||
} | ||
if (filters.lt_id) { | ||
@@ -427,3 +414,2 @@ whereClauses.push('id < ?'); | ||
} | ||
if (filters.lte_timestamp || filters.gte_timestamp) { | ||
@@ -502,10 +488,10 @@ if (filters.lte_timestamp) { | ||
const defaultLogsTTL = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds | ||
const DEFAULT_LOGS_TTL = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds | ||
try { | ||
let logsTTL = defaultLogsTTL; | ||
let logsTTL = DEFAULT_LOGS_TTL; | ||
const configResult = await this.getConfig('logsTTL'); | ||
if (configResult.item) { | ||
const parsedTTL = parseInt(configResult.item.value, 10); | ||
logsTTL = isNaN(parsedTTL) ? defaultLogsTTL : parsedTTL; | ||
logsTTL = isNaN(parsedTTL) ? DEFAULT_LOGS_TTL : parsedTTL; | ||
} | ||
@@ -512,0 +498,0 @@ let expirationTime = new Date(Date.now() - logsTTL); |
{ | ||
"name": "errsole-mysql", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "MySQL storage plugin for Errsole", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,4 +0,5 @@ | ||
# errsole-mysql | ||
# errsole-mysql | ||
<a href="https://coveralls.io/github/errsole/errsole-mysql"><img src="https://coveralls.io/repos/github/errsole/errsole-mysql/badge.svg" alt="Coverage Status" /></a> | ||
MySQL storage plugin for Errsole. | ||
MySQL storage plugin for Errsole. | ||
@@ -9,6 +10,6 @@ ## What is Errsole? | ||
If your application uses MySQL as its database or if you prefer storing your application logs in MySQL, you should install both the "errsole" and "errsole-mysql" modules. This setup allows you to store your application logs directly in your MySQL database. | ||
If your application uses MySQL as its database or if you prefer storing your application logs in MySQL, you should install both the `errsole` and `errsole-mysql` modules. This setup allows you to store your application logs directly in your MySQL database. | ||
## Full Documentation | ||
[https://github.com/errsole/errsole.js](https://github.com/errsole/errsole.js) | ||
[https://github.com/errsole/errsole.js](https://github.com/errsole/errsole.js) |
26614
15
660