Skip to main content

A python logger similair to the lavavel logs. Made for internal use in Appcomm.

Project description

Appcomm Python Logger

This package provides a robust logging solution with features for logging to the terminal, files, databases, Elasticsearch, and even uploading log files to an FTP server. It supports advanced features such as custom log levels, colorized console output, and detailed exception handling.

Features

  • Colorized Console Output: Logs are colorized for easy identification based on their severity (e.g., green for INFO, red for ERROR).
  • Log File: All logs are saved in a log file with detailed timestamps, hostnames, and log levels.
  • Database Logging: Logs can be stored in multiple database types, including MySQL, MariaDB, PostgreSQL, SQLite, and Oracle.
  • Elasticsearch Support: Logs can be indexed into an Elasticsearch cluster for easy searching and visualization.
  • FTP Support: Log files can be uploaded to an FTP or FTPS server automatically.
  • Custom Logging Levels: Includes additional levels like NOTICE, ALERT, and EMERGENCY.
  • Exception Handling: Logs detailed stack traces and exception messages for easier debugging.
  • Customizable Directory: The directory for log files can be customized, with a default of ./logs.

Installation

To install the Appcomm_python_Logger package, you can simply copy the logger class into your project or install it via a package manager if it has been uploaded to PyPI.

pip install Appcomm_python_Logger

Required Python Packages

The following Python libraries are required for full functionality. Install them using pip:

pip install sqlalchemy pymysql psycopg2 cx_Oracle
pip install mysql-connector-python psycopg2-binary elasticsearch

Usage

Basic Logger Setup

To use the logger, you can import the Logger class and create an instance of it in your code. You can then log messages at different severity levels using the debug(), info(), warning(), error(), and critical() methods.

To start logging, import the Logger class and initialize it:

from Appcomm_python_Logger import Logger

logger = Logger(log_dir="./logs")

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")
logger.alert("This is an alert message.")
logger.emergency("This is an emergency message.")

Logging with Exceptions

To log an exception with a traceback, use the exc argument:

try:
    # Simulate an operation that raises an exception
    raise Exception("Failed to connect to the SFTP server")
except Exception as e:
    logger.error("An error occurred while connecting to the SFTP server", exc=e)

Logging to Databases and Elasticsearch

You can log directly to supported databases or an Elasticsearch cluster by providing the necessary configuration file.

Configuration File Example Here’s an example JSON configuration file (config.json) for database and Elasticsearch connections:

{
  "mysql": {
    "driver": "mysql",
    "host": "127.0.0.1",
    "port": 3307,
    "username": "root",
    "password": "root",
    "name": "mysql"
  },
  "postgresql": {
    "driver": "postgresql",
    "host": "127.0.0.1",
    "port": 3308,
    "username": "root",
    "password": "root",
    "name": "postgres"
  },
  "mariadb": {
    "driver": "mariadb",
    "host": "127.0.0.1",
    "port": 3309,
    "username": "root",
    "password": "root",
    "name": "mysql"
  },
  "sqlite": {
    "driver": "sqlite",
    "name": "/route/to/PythonLogger.sqlite"
  },
  "elasticsearch": {
    "driver": "elasticsearch",
    "host": "127.0.0.1",
    "port": 9200,
    "username": "root",
    "password": "root",
    "index": "logs_index",
    "scheme": "https"
  },
  "ftp": {
    "host": "127.0.0.1",
    "port": 21,
    "username": "root",
    "password": "root",
    "remote_dir": "/"
  }
}

Command-Line Setup

The logger can also be initialized directly from the command line:

python3 src/AppcommPythonLogger/tester.py --db mysql,postgresql,sqlite,elasticsearch --config config.json --log-dir ./logs

Command-line arguments:

  • --db: Comma-separated list of database types to log to (e.g., mysql,postgresql).
  • --config: Path to the JSON configuration file with connection details.
  • --log-dir: Directory for saving log files (default: ./logs).

Supported Databases

The following databases are supported:

  • MySQL
  • PostgreSQL
  • MariaDB
  • SQLite
  • Oracle
  • Elasticsearch (for indexing and searching logs)

Customizing the Log File Directory

By default, log messages are displayed in the terminal with colorized output and saved to a log file in the ./logs directory. You can customize the log file directory by passing a log_dir argument to the Logger class.

logger = Logger(log_dir="./my_logs")

Author

This logger package was created by Furkan Öztürk (https://furkanozturk.nl/), intern for Appcomm (https://appcomm.nl).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

appcomm_python_logger-2.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Appcomm_python_Logger-2.5-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file appcomm_python_logger-2.5.tar.gz.

File metadata

  • Download URL: appcomm_python_logger-2.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.5

File hashes

Hashes for appcomm_python_logger-2.5.tar.gz
Algorithm Hash digest
SHA256 228052ef4b63a6d3aa8ae3c033ee0a23759db5b90a40cecd5510b6744adce9ed
MD5 334551e0be20144b60ff431b286bf488
BLAKE2b-256 98f5138b654462670a2a99d1f0b5b43aa56f1f3aae1fb6b04989a2bc2ec2db4c

See more details on using hashes here.

File details

Details for the file Appcomm_python_Logger-2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for Appcomm_python_Logger-2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 223dd5a877367eb17a862093cae7ef97a8ef11d429b5d7f0fbf93dd40ac5d2a7
MD5 7fa4cfb8516929658390022f24f1f52e
BLAKE2b-256 4070ffe039b4cfbad2733770020466269c5e4f7f3126ad5ba17250ef62ebf7c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page