1.3.5 • Published 9 months ago
@live2ride/dblog v1.3.5
MS SQL Server Logging Library
Easily log events to a Microsoft SQL Server (MSSQL) database using JavaScript.
Table of Contents
Configuration
Setup
Create all necessary tables and functions in the database.
import {dbLogSetup} from "@live2ride/dbLog";
dbLogSetup();Direct Configuration
Configure the database connection using environment variables and initialize the logging library.
import DB from "@live2ride/db";
import DBLog from "@live2ride/dbLog";
// Database configuration
const dbConfig = {
database: process.env.DB_DATABASE,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
server: process.env.DB_SERVER,
};
// Initialize database and logger
const db = new DB(dbConfig);
const dbLog = new DBLog(db);Usage
Logging Events
Start a log, update it with messages, and finalize with a status.
// Start log
dbLog.start({
title: "Some Cleanup Process",
message: "Cleanup started",
props: {
dir: "/xyz",
options: {
more: "Additional options",
},
},
});
// Update log with progress
dbLog.update("Cleaning directory xyz");
dbLog.update("Finished cleaning xyz/abc");
dbLog.update("Finished cleaning xyz/def");
// Finalize log with status
dbLog.success("Cleanup finished");
// or
dbLog.warning("Missed a few files");
// or
dbLog.error(new Error("Some error occurred"));Examples
Fetching Logs
Retrieve all logs from the database.
const logs = await db.exec('SELECT * FROM dbo.log');
console.log(logs);
/*
Example Output:
[
{
logid: ********,
status: 'success',
title: 'dbLogger',
msg: 'Finished',
start_time: ********,
end_time: ********,
props: { obj: 'Some additional props' },
error: undefined,
heartbeat: ********,
run_time: 1
}
]
*/await dbLog.get.recent()
await dbLog.get.log(logid) /* with additional column **messages** an array of logDetails messages */
await dbLog.get.errors()
await dbLog.get.warnings()Database Schema
Log Table Fields
- logid: Identity column.
- status:
success,warning,error. - title: Title identifying your process.
- msg: Additional messages (all messages are saved in
logDetails). - start_time: Time the process started.
- end_time: Time the process ended.
- props: Any object you want to save for troubleshooting, such as parameters.
- error: Field to hold your error object for easier troubleshooting.
- heartbeat: Indicates that
dbLogis alive by checking in every few seconds. run_time: Duration the process has been running in seconds.