Skip to main content

Event Journal

PyPI version Python Support License: MIT

A simple Python library for logging events to PostgreSQL database with a clean API. Perfect for audit trails, user activity tracking, and system event logging. No Django or web framework required!

🚀 Features

  • Zero Configuration - Pre-configured database connection
  • Simple API - Just import and log events
  • No Framework Required - Works with any Python application
  • No Migrations - Assumes table already exists
  • Utility Functions - Extract IP addresses and user agents
  • Production Ready - Optimized for performance and reliability
  • Context Manager Support - Automatic connection management

Installation

pip install event-journal

Or install from source:

git clone https://github.com/amolsr/event_journal.git
cd event_journal
pip install -e .

Database Configuration

The library comes with pre-configured database settings for immediate use. No additional configuration is required - the library will automatically connect to the database.

Setup

Quick Start

No setup required! Just install and use:

pip install event-journal
from event_journal import log_event
log_event('your_event_type', event_info={...})

Database Setup

  1. Create the event journal table in your PostgreSQL database:
-- Run this SQL script in your database
\i create_event_journal_table.sql
  1. Test the connection:
python quick_start.py

Usage

Basic Usage

The library comes with pre-configured database settings, so you can use it in any Python script without any setup:

from event_journal import log_event

# Just log events - assumes EventJournal table already exists!
log_event(
    event_type='user_login',
    user_id=123,
    event_info={
        'login_method': 'email',
        'success': True
    },
    ip_address='192.168.1.1',
    user_agent='Mozilla/5.0...'
)

Prerequisites: The EventJournal table must already exist in your database.

Advanced Usage with Database Handler

For more control over database connections:

from event_journal import DatabaseHandler, EventJournal

# Create a custom database handler
db_handler = DatabaseHandler(
    host='your-host',
    port=5432,
    database='your-database',
    user='your-user',
    password='your-password'
)

# Use context manager for automatic connection management
with db_handler:
    # Log an event
    event = EventJournal.log_event(
        event_type='user_login',
        user_id=123,
        event_info={'login_method': 'email'},
        db_handler=db_handler
    )

Convenience Functions

The library provides convenience functions for common event types:

from event_journal import log_user_login, log_file_upload, log_api_request

# Log user login
log_user_login(
    user_id=123,
    login_method='email',
    ip_address='192.168.1.1',
    user_agent='Mozilla/5.0...'
)

# Log file upload
log_file_upload(
    user_id=123,
    file_id='file_456',
    filename='document.pdf',
    file_type='application/pdf',
    file_size=2048000,
    ip_address='192.168.1.1'
)

# Log API request
log_api_request(
    endpoint='/api/upload',
    method='POST',
    status_code=200,
    user_id=123,
    response_time_ms=150
)

Utility Functions

The library provides utility functions for extracting client information:

from event_journal.utils import get_client_ip, get_user_agent

# With Flask request headers
ip_address = get_client_ip(dict(request.headers))
user_agent = get_user_agent(dict(request.headers))

# With custom headers dictionary
headers = {'X-Forwarded-For': '192.168.1.1', 'User-Agent': 'Mozilla/5.0...'}
ip_address = get_client_ip(headers)
user_agent = get_user_agent(headers)

API Reference

log_event()

Log an event to the database.

Parameters:

  • event_type (str): Type of event being logged
  • user_id (int, optional): ID of the user associated with this event
  • event_info (dict, optional): Additional event information
  • ip_address (str, optional): Client IP address
  • user_agent (str, optional): Client user agent string
  • timestamp (datetime, optional): Event timestamp (defaults to now)

Returns:

  • EventJournal: The created EventJournal instance

DatabaseHandler

Manage database connections and operations.

Parameters:

  • host (str): Database host
  • port (int): Database port
  • database (str): Database name
  • user (str): Database username
  • password (str): Database password
  • sslmode (str): SSL mode for connection

Methods:

  • test_connection(): Test database connection
  • check_table_exists(): Check if event journal table exists
  • create_event(): Create a new event
  • get_event(event_id): Get event by ID
  • get_events(): Get events with filtering
  • count_events(): Count events with filtering
  • delete_event(event_id): Delete event by ID

EventJournal

Model for event journal entries.

Attributes:

  • id: Database ID
  • event_type: Type of event
  • user_id: User ID (optional)
  • event_info: Event information dictionary
  • ip_address: Client IP address
  • user_agent: Client user agent
  • timestamp: Event timestamp
  • created_at: Creation timestamp

Methods:

  • to_dict(): Convert to dictionary
  • from_dict(data): Create from dictionary
  • log_event(): Class method to log events

Database Schema

The library uses a PostgreSQL table with the following structure:

CREATE TABLE event_journal (
    id SERIAL PRIMARY KEY,
    event_type VARCHAR(100) NOT NULL,
    user_id INTEGER NULL,
    event_info JSONB DEFAULT '{}',
    ip_address INET NULL,
    user_agent TEXT DEFAULT '',
    timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

Fields:

  • id: Primary key (auto-incrementing)
  • event_type: Type of event (VARCHAR, max 100 chars)
  • user_id: User ID (INTEGER, nullable)
  • event_info: JSON field containing event data (JSONB)
  • ip_address: Client IP address (INET type)
  • user_agent: Client user agent (TEXT)
  • timestamp: Event timestamp (TIMESTAMP WITH TIME ZONE)
  • created_at: Record creation timestamp (TIMESTAMP WITH TIME ZONE)

Indexes:

  • Primary key on id
  • Index on event_type
  • Index on user_id
  • Index on timestamp
  • Composite index on event_type, timestamp

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

📊 Stats

GitHub stars GitHub forks GitHub issues GitHub pull requests

Metadata

Release files for event-journal 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for event-journal 1.1.0
File Size Uploaded Provenance
event_journal-1.1.0.tar.gz 15.5 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for event-journal 1.1.0
File Interpreter ABI Platform
event_journal-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.2 kB

Release files / event_journal-1.1.0.tar.gz

Download URL event_journal-1.1.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
88ac4276afbd1bb12a28d379da3fc82e9f2b1b899ce76f405630b0494f8f6fce
BLAKE2b-256 checksum
How to use checksums
303d8cc773872159f49991e8517c0971157d986be5400780cff962d8d90c9cdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2025.

Transparency log

Release files / event_journal-1.1.0-py3-none-any.whl

Download URL event_journal-1.1.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d036963464c9f6d044a902a2eebe068cb049e0f03cfe0079cd153ebfc6cd1a6
BLAKE2b-256 checksum
How to use checksums
8cd6d149f5dd911bf352cfd986f832f020265a765b56ecb4da7e211bec63e854
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2025.

Transparency log

Security for event-journal 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for event_journal-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page