summaryrefslogtreecommitdiffstats
path: root/logger.py
blob: 77cdad18737ec3bbcf35d4fecbf9beb273dcb418 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import time
from datetime import datetime
import pytz
import os

def _get_datetime_string():
    utc_now = datetime.now(pytz.timezone('UTC'))
    pst_now = utc_now.astimezone(pytz.timezone('US/Pacific'))
    return pst_now.strftime('%Y-%m-%d %H:%M:%S')

def track_task_time(message: str):
    def decorator(func):
        def wrapper(*args, **kwargs):
            print(f"[{_get_datetime_string()}] TASK STARTED: " + message)
            start = time.time()
            result = func(*args, **kwargs)
            end = time.time()
            print(f"[{_get_datetime_string()}] TASK COMPLETED: {message} {round(end - start, 3)} seconds")
            return result
        return wrapper
    return decorator

class Logger:
    def __init__(self, path: str = "logs.txt", max_size_bytes: int = 1000000):
        self.max_size_bytes = max_size_bytes
        self.path = path
    
    def create_log_file(self):
        with open(self.path, "w") as file:
            file.write("")
    
    def log(self, message: str):
        with open(self.path, "a") as file:
            self.check_log_size()
            file.write(f"[{_get_datetime_string()}] {message}\n")
    
    def check_log_size(self):
        if os.path.getsize(self.path) > self.max_size_bytes:
            with open(self.path, "r") as file:
                lines = file.readlines()
            with open(self.path, "w") as file:
                file.writelines(lines[int(len(lines) / 2):])
                file.write("Removed half of the log file due to size\n")

send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage