ORM
  • Introduction
  • Basics of Usage
    • Connecting to the Database
    • Creating a Model
    • Simple Queries
  • Configuration
  • Core Features
    • Models
    • CRUD Operations (Create, Read, Update, Delete): examples
    • Relationships
    • Transactions
  • Advanced Usage
    • Data Definition Language
    • Indexes
    • Triggers
    • Caching
  • Examples and templates
  • Security
    • Query parameterization and protection against SQL injections
    • Logging
    • Monitoring
    • Sanitization
  • ORM-CLI
    • Installing
    • Commands and usage
  • System architecture and implementation
    • Support of various databases
      • Implementation using a pattern strategy
      • Implementation using generics
      • Implementation using metaprogramming
    • Decorators
    • Migrations
      • Assignment of id in Tables and Columns
      • Assigning IDs to indexes and triggers
      • Working with migrations in ORM-CLI
        • Implementation of a pattern command for migration
    • QueryBuilder
    • Caching
    • File structure
  • API
    • Decorators
      • Column
      • ComputedColumn
      • Index
      • ForeignKey
      • PrimaryGeneratedColumn
      • Relations
      • Table
      • Trigger
      • Types decorators
        • Integer
        • Numeric
        • Float
        • Boolean
        • String
    • DDL
    • QueryBuilder
    • Additional API
Powered by GitBook
On this page
  1. Security

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.

PreviousQuery parameterization and protection against SQL injectionsNextMonitoring

Last updated 9 months ago