Logging

An example of logging Requests

Query logging in my ORM system allows tracking all SQL operations, providing transparency and simplifying the process of diagnostics and debugging. Enabling logging ensures that all queries passing through the system are recorded, as well as any errors occurring during query execution.

Activating Logging

To enable logging in my ORM system, configure the DatabaseManager with the logging: true parameter. This will allow recording all queries and errors to a log file:

export const databaseManager = new DatabaseManager<DatabasesTypes.POSTGRES>({
    logging: true,
});

Example of Using Logging

In my ORM system, two main methods are available for logging:

  • log(message: string, sql?: string, params?: string): void: Used for recording informational messages and successfully executed queries.

  • error(message: string, sql?: string, params?: string): void: Used for recording error messages and failed queries.

Example:

databaseManager.logger.log('Запит успішно виконаний', 'SELECT * FROM users WHERE id = $1;', '[1]');
databaseManager.logger.error('Помилка виконання запиту', 'INSERT INTO users (id, email) VALUES ($1, $2);', '[1, "email@gmail.com"]');

Log Structure

All logs are stored in the app.log file and include information about the time, event type (INFO or ERROR), SQL query, and passed parameters. Example content of the log file:

2024-08-09T10:00:05.176Z - ERROR: {"length":285,"name":"error","severity":"ОШИБКА","code":"23505","detail":"Ключ \"(task_id)=(5)\" уже существует.","schema":"public","table":"tasks","constraint":"tasks_pkey","file":"nbtinsert.c","line":"673","routine":"_bt_check_unique"}, SQL: INSERT INTO tasks (task_id, title, description, is_completed, due_date, price) VALUES ($1, $2, $3, $4, $5, $6);, PARAMS: [5,"Новий таск","Опис нового таску",false,"2024-12-31",100.5]
2024-08-09T10:00:05.178Z - INFO: [{"task_id":"7","title":"Новий таск","status":"Новий таск - Pending"}], SQL: SELECT task_id, title, status FROM tasks WHERE is_completed = $1 AND price > $2 ORDER BY due_date ASC;, PARAMS: [false,50]

Query logging in my ORM system provides an effective way to track and diagnose SQL operations by storing information about queries and errors in a dedicated file. This allows for quick identification and resolution of potential issues, improving system stability and transparency. Logging can be easily activated by configuring the logging parameter in the DatabaseManager configuration.

Last updated