Simple python log helper
Project description
Overview
- Setting up logging sucks, this makes it easier
Features
- Simple logger set up
- Creates log directory and filename as described in
Loginitialization - Prints all log entries to the terminal in color for easier debugging
- Promotes logging during development, not after. Reduce your tech debt.
Use
Standard Use
from logg3r import Log
logger = Log(log_path="./path-to/log-directory", name="test_log", level=1)
logger.debug("test message")
logger.info("test message")
logger.warning("test message")
logger.error("test message")
logger.critical("test message")
Log file example test_log.log
2023-07-29 03:13:27 | DEBUG | test message
2023-07-29 03:13:27 | INFO | test message
2023-07-29 03:13:27 | WARNING | test message
2023-07-29 03:13:27 | ERROR | test message
2023-07-29 03:13:27 | CRITICAL | test message
Terminal Example
Log with line numbers - recommended
from logg3r import Log
logger = Log(log_path="./path-to/log-directory", name="test_log", level=1)
line = logger.line #must be called from the line being logged
logger.debug(line()+"test message")
logger.info(line()+"test message")
logger.warning(line()+"test message")
logger.error(line()+"test message")
logger.critical(line()+"test message")
Log file example test_log.log
2023-07-29 03:30:25 | DEBUG | 7 test message
2023-07-29 03:30:25 | INFO | 8 test message
2023-07-29 03:30:25 | WARNING | 9 test message
2023-07-29 03:30:25 | ERROR | 10 test message
2023-07-29 03:30:25 | CRITICAL | 11 test message
Terminal Example
Initialization Keyword Arguments
Required
log_path (string)
- If the path does not exist (and
create_pathis notFalse) logg3r will create the directory
name (string)
- Sets the name of your log file - file will present as "filename.log" in your log directory
level (integer)
- See log levels
- Sets minimum level of log message that will be stored in the log file (all log function calls print to terminal)
Optional
print_filename (bool)
from logg3r import Log
Logger = Log(log_path="./path-to/log-directory",
name="test_log",
level=1,
print_filename=True)
- Defaults to
False - If enabled, the filename will print in the first section of the log message in the terminal - the filename will not be written to the log file
formatter (string)
from logg3r import Log
fmt='{} | %(levelname)s | %(message)s '.format(datetime.datetime.utcnow().replace(microsecond=0))
Logger = Log(log_path="./path-to/log-directory",
name="test_log",
level=1,
formatter=fmt)
- The formatter can be any string you like
%(levelname)sis required and corresponds to the log level%(message)sis required and is the message passed when calling any of the log functions- In the example above the date and time are represented at
{}and formatted with.format(datetime.datetime.utcnow().replace(microsecond=0)) - Any other string elements can be added to the formatter
rotation (tuple)
from logg3r import Log
rotation_args=filename,when='d',interval=30,backupCount=1,encoding=None,delay=False,utc=True,atTime=datetime.time(4, 0, 0)
Logger = Log(log_path="./path-to/log-directory,
name="test_log",
level=1,
rotation=rotation_args)
- See Python Logging TimedRotatingFileHandler
- Defaults to 30 days of retention with (1) log overflow file (log.1)
create_path (bool)
- Defaults to
True - If set to
Falseinitializing logg3r will not create a new log directory, but will put the file in the directory passed tolog_path
Log Levels
1= DEBUG (green)2= INFO (cyan)3= WARNING (yellow)4= ERROR (magenta)5= CRITICAL (red)
logg3r.Log Functions
.debug
- Creates debug level log message entry to log file
- Prints message in color to terminal
logger.debug("log message")
.info
- Creates info level log message entry to log file
- Prints message in color to terminal
logger.info("log message")
.warning
- Creates warning level log message entry to log file
- Prints message in color to terminal
logger.warning("log message")
.error
- Creates error level log message entry to log file
- Prints message in color to terminal
logger.error("log message")
.critical
- Creates critical level log message entry to log file
- Prints message in color to terminal
logger.critical("log message")
.line
- Returns line number of the code where the function is called - unfortunately this couldn't be integrated automatically because the function must be called from the line being logged
line = logger.line
logger.debug(line()+"log_message")
References
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
logg3r-2.0.2.tar.gz
(5.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logg3r-2.0.2.tar.gz.
File metadata
- Download URL: logg3r-2.0.2.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db6c9e56856246dd09b66b6b61ce1f72a53033e4cc921baacacc38897fab2131
|
|
| MD5 |
1f4c8ed687a009f15cab17e67ae45bd4
|
|
| BLAKE2b-256 |
cdb62891faa77c613d667ecd8a135025a52e35f6972be3bc94b7a02ed7fb9b47
|
File details
Details for the file logg3r-2.0.2-py3-none-any.whl.
File metadata
- Download URL: logg3r-2.0.2-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8dca40fc548d55fcbe2bb1ae5697304f37c25445a9cd6300385e265f4326a1c7
|
|
| MD5 |
0b1413d9a0b8f04456b8ab913c39fc2c
|
|
| BLAKE2b-256 |
43400e498fd51bf94618383098e740ed1921afd86fa076c6ec6445ce083bc67d
|