Skip to main content

A lightweight and human-readable logging and CSV utility

Project description

Delgado Logger

A human-readable, lightweight logging utility for Python. Designed for quick integration and debugging ease with time-stamped log entries and error tracking

Features

  • Timestamped logs
  • Human-readable file logging
  • Error logging with file and line reference
  • Automatic script start/end messages
  • Utility to inspect variable values
  • Clean-up utility for old files

Installation

To install via pip (once uploaded to PyPI):

pip install delgado_logger

Usage

Below is a quick reference table of all available functions in the delgado_logger module:

Function Name Description
line_number() Returns the current line number in the script.
now_date() Returns current date in MM/DD/YYYY format.
date_filename() Returns date formatted as YYYY.MM.DD for filenames.
now_time() Returns the current time in HH:MM:SS AM/PM TIMEZONE format.
write_to_logfile(log_text, log_filename=None) Appends a log entry to the specified log file.
error_handler(error, line_num) Logs an error message with file and line number.
engage() Logs the start of the script.
end_of_line() Logs the end of the script.
create_new_output_file(filename) Creates a new file and logs the action.
write_to_file(filename, entry) Appends a row (list) to a file and logs it.
last_line_of_file(filename) Returns the last line from a given file.
print_value_info(value) Prints type, value, line number, and filename of a variable.
remove_old_files(file_extension, directory) Deletes files with the given extension in a directory.

To utilize a function in this module, you must first import it into your project

import delgado_logger

Examples:

line_number()

Use:

print(delgado_logger.line_number())

Output:

3

now_date()

Use:

print(delgado_logger.now_date())

Output:

04/17/2025

now_time()

Use:

print(delgado_logger.now_time())

Output:

06:52:39 PM Pacific Daylight Time

date_filename()

This is intended for use to concatinate a filename for output. In this example, I'm adding .filename.ext to the date_filename() output. Use:

print(f"{delgado_logger.date_filename()}.filename.ext")

Output:

2025.04.17.filename.ext

write_to_logfile()

By default, this will write a specified string to log.txt, however if you specify the log_filename variable, you can output to a specific txt file instead.

Default Use:

delgado_logger.write_to_logfile('Some Text Here')

Output:

[04/17/2025 07:00:21 PM Pacific Daylight Time, tester.py: 11] Some Text Here 

In addition to the console output, it will append the log file, or create the log file and add this as the first contents.

Here is an example log.txt after running my test script twice


[04/17/2025 07:00:21 PM Pacific Daylight Time, tester.py: 11] Some Text Here 
[04/17/2025 07:03:17 PM Pacific Daylight Time, tester.py: 11] Some Text Here 

If you would like to output your text to a different log file, modify the command like this

delgado_logger.write_to_logfile('Some Text Here', log_filename='tester_log.txt')

error_handler()

This requires you to supply the error text and line number. You can use the line_number() function to supply the line number dynamically. This will both print to the terminal and output to the logfile. Use:

delgado_logger.error_handler('Some Error Text', delgado_logger.line_number())

Terminal Output

[04/17/2025 07:10:07 PM Pacific Daylight Time, logger.py: 52]   Error [File: tester.py Line: 15] - Some Error Text 

Example Log.txt contents after an error:

[04/17/2025 07:07:04 PM Pacific Daylight Time, tester.py: 11] Some Text Here 
[04/17/2025 07:07:04 PM Pacific Daylight Time, logger.py: 52]   Error [File: tester.py Line: 15] - Some Error Text 
[04/17/2025 07:08:34 PM Pacific Daylight Time, tester.py: 11] Some Text Here 
[04/17/2025 07:10:07 PM Pacific Daylight Time, tester.py: 11] Some Text Here 
[04/17/2025 07:10:07 PM Pacific Daylight Time, logger.py: 52]   Error [File: tester.py Line: 15] - Some Error Text 

engage()

This will append to the log file that the file running has started. This will not print to the terminal that the file has started.

Use:

delgado_logger.engage()

Output:

[04/17/2025 07:12:10 PM Pacific Daylight Time, logger.py: 60] Starting Script: tester.py 

end_of_line()

Use:

delgado_logger.end_of_line()

Output:

[04/18/2025 12:12:50 AM Pacific Daylight Time, logger.py: 67] Ending Script: tester.py ```

create_new_output_file(filename)

Creates a file with a specified filename. Use:

delgado_logger.create_new_output_file('Test.txt')

Output:

[04/18/2025 12:16:05 AM Pacific Daylight Time, logger.py: 71] Creating File: "Test.txt" 

delgado_logger.write_to_file()

This allows you to write to any txt/csv file (maybe others). You feed it an array, and it will be written as a single line with CSV formatting. Use:

some_content = ['test1', 'test2', 'test3']
delgado_logger.write_to_file('Test.txt', some_content)

Console Output:

[04/18/2025 12:24:03 AM Pacific Daylight Time, logger.py: 79] Writing Data to: "Test.txt" 

Test.txt File Contents:

test1,test2,test3

last_line_of_file()

This will return the last line of a given txt or CSV file (may work on other formats too, but untested)

Use:

print(f'{delgado_logger.last_line_of_file("Test.txt")}')

Output:

test1,test2,test3

print_value_info()

This will print out some helpful information about a specified variable

Use:

dict_example = {'tst': 'test'}
array_example = ['test1', 'test2']
string_example = 'Testing String'
int_example = 1

delgado_logger.print_value_info(dict_example)
delgado_logger.print_value_info(array_example)
delgado_logger.print_value_info(string_example)
delgado_logger.print_value_info(int_example)

Output:

dict_example:
 Type: <class 'dict'>
 Value = {'tst': 'test'}
 Line: 32
 Filename: tester.py
array_example:
 Type: <class 'list'>
 Value = ['test1', 'test2']
 Line: 33
 Filename: tester.py
string_example:
 Type: <class 'str'>
 Value = Testing String
 Line: 34
 Filename: tester.py
int_example:
 Type: <class 'int'>
 Value = 1
 Line: 35
 Filename: tester.py

Process finished with exit code 0


remove_old_files(file_extension, directory)

This will remove all files of a given file extension within a specified directory. No output on console or file for this, it's intended purpose is to help clean up after testing. In this example, we're deleting all the .txt files within the ./old_files/ subdirectory

Use:

delgado_logger.remove_old_files('txt', './old_files/')

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

delgado_logger-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

delgado_logger-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file delgado_logger-0.1.1.tar.gz.

File metadata

  • Download URL: delgado_logger-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for delgado_logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fc5d933a352c5a9560ab09856d786a896ec3a6acc42f79079eb712c7184aa645
MD5 f9922e518224b2a9d2eca6a69a56a843
BLAKE2b-256 dbffe133a7536467f3d110fbd84f5c0b44c4eca8b49b3a832e97f5c00274bc54

See more details on using hashes here.

File details

Details for the file delgado_logger-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: delgado_logger-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for delgado_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49f872cceaa4302399f2b242ed04aefcba45b371ad63cc167d6d0e747f4837ad
MD5 9bb407c0fa27ea68abdbb32d68770a7b
BLAKE2b-256 b20a90119db69d42ee1f61b941ac7f15cf372b91d404bf7e27c9ee761265a7d3

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